CodeTop每日系列三题------------------2021.11.4

LC206.反转链表
存在递归和迭代的两种算法
在这里插入图片描述
迭代以及递归思路;

//迭代
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null,cur = head;
        while(cur != null){
            ListNode tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
}
//递归
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null)
        return head;
        ListNode cur = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return cur;
    }
}

在这里插入图片描述
LC146.LRU缓存机制
核心思想在于:双向链表,位于链尾的可能是即将要被刷新的值,哈希表提高检索效率。
在这里插入图片描述

public class LRUCache {
    //双向链表和hashmap
    class ListNode{
        private int key;
        private int value;
        private ListNode prev;
        private ListNode next;

        public ListNode(){}
        public ListNode(int key,int value){
            this.key = key;
            this.value = value;
        }
    }

    private HashMap<Integer,ListNode> cache = new HashMap();
    private int capacity;
    private int size;
    private ListNode head,tail;

    public LRUCache(int capacity){
        this.size = 0;
        this.capacity = capacity;
        head = new ListNode();
        tail = new ListNode();
        head.next = tail;
        tail.prev = head;
    }

    public int get(int key){
        ListNode node = cache.get(key);
        if(node == null)
        return -1;
        moveToHead(node);
        return node.value;
    }

    public void put(int key,int value){
       ListNode node = cache.get(key);
       if(node == null){
           //还有容量的时候
           ListNode newnode = new ListNode(key,value);
           cache.put(key,newnode);
           addToHead(newnode);
           ++size;
           if(size > capacity){
               //删除cache对应的尾巴的值和双向链表尾巴的
               ListNode tail = removeTail();
               cache.remove(tail.key);
               --size;
           }   
       }else{
           node.value = value;
           moveToHead(node);
       }
    }

    public void moveToHead(ListNode node){
        removeNode(node);
        addToHead(node);
    }

    public void removeNode(ListNode node){
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }

    public void addToHead(ListNode node){
        node.next = head.next;
        node.prev = head;
        head.next.prev = node;
        head.next = node;
    }

    public ListNode removeTail(){
        //先获取值然后删除尾巴.
        ListNode res = tail.prev;
        removeNode(res);
        return res;
    }

}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */


LC3.无重复字符的最长子串
关键在于滑动窗口的思想,什么时候扩大窗口需要做什么条件是什么,什么时候需要缩小窗口条件是什么并且做什么。
在这里插入图片描述

class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character,Integer> window = new HashMap();
        int left = 0,right = 0;
        int res = 0;
        char[] strarr = s.toCharArray();
        while(right < s.length()){
            char c = strarr[right];
            right++;
            window.put(c,window.getOrDefault(c,0) + 1);
            while(window.get(c) > 1){
                char d = strarr[left];
                left++;
                window.put(d,window.getOrDefault(d,0) - 1);
            }
            res = Math.max(res,right - left);
        }
        return res;
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

破晓以胜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值