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);
}
}