回顾前面刷过的算法(3)
创始人
2024-11-14 19:34:37
0

今天简单描述了一下算法思路并手敲了一下下面三个算法,算法题目是刷不完的,但是解题思想是有限的,我们需要学习的就是解题思想,代码展示如下

    //打家劫舍     //思路: 定义一个dp[i],表示偷前 i - 1 个房屋能偷到的最大金额     //则有递推关系式 dp[i] = max(dp[i - 1], dp[i - 2] + nums[i - 1])     public int rob(int[] nums) {         if (nums == null || nums.length == 0) {             return 0;         }         int pre = 0, curr = nums[0];         for (int i = 2; i <= nums.length; i++) {             int temp = Math.max(curr, pre + nums[i - 1]);             pre = curr;             curr = temp;         }         return curr;     }       //多数元素     //思路: 方法有很多,这里采用的是每次消除两个不同元素,因为多数元素的数量是大于总数的一半     //所以消除到最后,剩余的元素一定是多数元素     public int majorityElement(int[] nums) {         if (nums == null || nums.length == 0) {             return -1;         }         int count = 0, currentNum = -1;         for (int num : nums) {             if (count == 0) {                 currentNum = num;                 count++;             } else {                 if (num == currentNum) {                     count++;                 } else {                     count--;                 }             }         }         return currentNum;     }             //LRU缓存     //思路: 采用双链表+HashMap实现,hashMap存储key与结点的对应关系便于查找,     //双链表存储结点便于增删     class LRUCache {         private int size;         private final int capacity;         private final DLinkedNode head;         private final DLinkedNode tail;         private final Map cache = new HashMap<>();          class DLinkedNode {             int key;             int value;             DLinkedNode next;             DLinkedNode pre;              public DLinkedNode() {             }              public DLinkedNode(int _key, int _value) {                 key = _key;                 value = _value;             }         }          public LRUCache(int capacity) {             this.size = 0;             this.capacity = capacity;             head = new DLinkedNode();             tail = new DLinkedNode();             head.next = tail;             tail.pre = head;         }          public int get(int key) {             DLinkedNode node = cache.get(key);             if (node == null) {                 return -1;             }             //因为操作过,需要把它移动到双链表头部             moveToHead(node);             return node.value;         }          public void put(int key, int value) {             DLinkedNode node = cache.get(key);             if (node == null) {                 DLinkedNode newNode = new DLinkedNode(key, value);                 cache.put(key, newNode);                 addToHead(newNode);                 size++;                 if (size > capacity) {                     DLinkedNode temp = removeTail();                     cache.remove(temp.key);                     size--;                 }             } else {                 node.value = value;                 moveToHead(node);             }         }          private void moveToHead(DLinkedNode node) {             removeNode(node);             addToHead(node);         }          private void removeNode(DLinkedNode node) {             node.next.pre = node.pre;             node.pre.next = node.next;         }          private void addToHead(DLinkedNode node) {             node.next = head.next;             node.pre = head;             head.next.pre = node;             head.next = node;         }          private DLinkedNode removeTail() {             DLinkedNode temp = tail.pre;             removeNode(temp);             return temp;         }     }

相关内容

热门资讯

刚刚!xpoker透视辅助,老... 刚刚!xpoker透视辅助,老友广东麻将有挂吗,方案教程(有挂辅助)-哔哩哔哩1、老友广东麻将有挂吗...
有消息称!欢乐对决脚本,吉祥填... 有消息称!欢乐对决脚本,吉祥填大坑小程序脚本,手册教程(有挂规律)-哔哩哔哩1、吉祥填大坑小程序脚本...
揭幕透视!hhpoker是正规... 揭幕透视!hhpoker是正规的吗,微信小程序辅助器免费版,机巧教程(今日头条)-哔哩哔哩1、hhp...
透视教学!红龙poker有辅助... 透视教学!红龙poker有辅助吗,werplan免费挂下载,手段教程(揭秘有挂)-哔哩哔哩1、操作简...
普及透视!德州来玩辅助器,微乐... 您好,德州来玩辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...
有了最新消息!浙江游戏大厅自建... 有了最新消息!浙江游戏大厅自建房间,天天互娱软件,要领教程(有挂教程)-哔哩哔哩1、用户打开应用后不...
透视安卓版!xpoker辅助器... 透视安卓版!xpoker辅助器,pokernow辅助控制,绝活儿教程(有挂分享)-哔哩哔哩1、超多福...
专业透视!pokemmo辅助器... 专业透视!pokemmo辅助器手机版下载,微信小程序游戏破解器,方案教程(确实有挂)-哔哩哔哩1、p...
现有关情况通报如下!边锋辅助,... 现有关情况通报如下!边锋辅助,好友赣南新版本可以作假吗,手筋教程(真实有挂)-哔哩哔哩好友赣南新版本...
黑科技辅助挂!德州局怎么透视,... 黑科技辅助挂!德州局怎么透视,盛世辅助器,策略教程(有挂方法)-哔哩哔哩1、德州局怎么透视脚本辅助下...