CodeTop day1

class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character,Integer> map = new HashMap<>();
        int index = 0;
        int left = 0;
        int right = 0;
        int result = 0;
        for (right=0;right<s.length();right++){
            if (map.containsKey(s.charAt(right))){
                index = map.get(s.charAt(right));
                left = Math.max(left,index+1);
            }
            map.put(s.charAt(right),right);
            result = Math.max(result,right-left+1);
        }
        return result;
    }
}

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

class LRUCache {

    private final int capacity;
    private final HashMap<Integer, Integer> cache = new LinkedHashMap<>();

    public LRUCache(int capacity) {
        this.capacity = capacity;

    }
    
    public int get(int key) {
        if (!cache.containsKey(key)){
            return -1;
        }
        int value = cache.get(key);
        cache.remove(key);
        cache.put(key,value);
        return value;

    }
    
    public void put(int key, int value) {
        if (cache.remove(key)!=null){
            cache.put(key,value);
            return;
        }
        if (cache.size()==capacity){
            int olderKey = cache.keySet().iterator().next();
            cache.remove(olderKey);
        }
        cache.put(key,value);
    }
}

相关推荐

  1. nvm1.1.11

    2024-03-15 05:00:03       52 阅读
  2. 1.下午试题1

    2024-03-15 05:00:03       29 阅读
  3. HTML-1

    2024-03-15 05:00:03       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 05:00:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 05:00:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 05:00:03       87 阅读
  4. Python语言-面向对象

    2024-03-15 05:00:03       96 阅读

热门阅读

  1. 动态规划矩阵

    2024-03-15 05:00:03       40 阅读
  2. 云计算有什么作用

    2024-03-15 05:00:03       40 阅读
  3. ARMv8系统寄存器-0

    2024-03-15 05:00:03       45 阅读
  4. Flink广播流 BroadcastStream

    2024-03-15 05:00:03       41 阅读
  5. Kafka问题纪要

    2024-03-15 05:00:03       32 阅读