好吧,今天晚上赶项目确实是做不了三道题目了,最近项目在网络编程方面有些进步,学到了东西,有时间再积累下来,很深的体会就是,和别人一起写代码,虽然蛋疼但是比自己一个人写要好点,不过发现自己对链表和排序什么的都不太熟练啊,得好好认真练习,加油!

6

LRU Cache

public class LRUCache {
private int capacity;
private HashMap<Integer, Node> map;
private Node firstNode;
private Node lastNode; public LRUCache(int capacity) {
this.capacity = capacity;
firstNode = new Node();
lastNode = new Node();
firstNode.next = lastNode;
lastNode.pre = firstNode;
map = new HashMap<Integer, Node>();
} public int get(int key) {
if (map.containsKey(key)) {
Node node = map.get(key);
moveToHead(node);
return node.value; } else {
return -1;
} } public void set(int key, int value) {
if (map.containsKey(key)) {
Node node = map.get(key);
node.value = value;
moveToHead(node);
}else{
if(map.size()>=capacity){
if(lastNode!=null&&capacity>0){
Node dNode = lastNode.pre;
lastNode.pre = dNode.pre;
dNode.pre.next = lastNode;
map.remove(dNode.key);
Node node = new Node();
node.key = key;
node.value = value;
moveToHead(node);
map.put(key, node);
}
}else{
Node node = new Node();
node.key = key;
node.value = value;
moveToHead(node);
map.put(key, node);
} } } class Node {
Node pre;
Node next;
int key;
int value; } public void moveToHead(Node node) {
if (node.next != null&&node.pre != null) {
node.pre.next = node.next;
node.next.pre = node.pre;
}
if (firstNode != null) {
node.pre = firstNode;
node.next = firstNode.next;
firstNode.next.pre = node;
firstNode.next = node;
} } }

leetcode6的更多相关文章

  1. leetcode6:Zigzag Conversion@Python

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  2. LeetCode6 ZigZag Conversion

    题意: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  3. leetcode6 Reverse Words in a String 单词取反

    Reverse Words in a String  单词取反 whowhoha@outlook.com Question: Given an input string s, reverse the ...

  4. [Swift]LeetCode6. Z字形变换 | ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  5. LeetCode6. Z字形变换

    描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  6. LeetCode6.Z字形变换 JavaScript

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...

  7. string leetcode-6.ZigZag

    6. ZigZag Conversion 题面 The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  8. LeetCode6 dp

    120. Triangle 我的解法用了一个二维数组,这样比较浪费空间.O(n*n) 但是标准答案自底向上,一是不需要进行特别判断,二是可以覆盖数组,则只需要O(n)的空间大小. class Solu ...

  9. Leetcode6.ZigZag ConversionZ字形变换

    将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: 之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR" 实现一个将字符串进行指定行 ...

随机推荐

  1. ionic入门之色彩、图标、边距和界面组件:列表

    目录: 色彩.图标和边距 色彩 图标 内边距 界面组件:列表 列表:.list 成员容器:.item .item: 嵌入文本 .item : 嵌入图标 .item : 嵌入头像 .item : 嵌入缩 ...

  2. 关于IONIC 报错 XX is not a function

    刚开始 做一个项目,总是报错"XX is not  a function"   最后发现 原因 ,   原来是 服务的 注入位置 有问题. angular.module(" ...

  3. 增量处理属性之记录模式(Record Modes)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. 如何隐藏winform中报表设计器中的按钮

    https://www.devexpress.com/Support/Center/Question/Details/T246117 DesignMdiController.SetCommandVis ...

  5. WCF入门简单教程(图文) VS2010版

    在这个例子中我们将使用VS 2010 创建一个WCF服务,其中会了解 [DataContract] [ServiceContract] 等特性.  内置的 WCFSVCHost ,并使用“WCF测试客 ...

  6. js无刷新上传图片,服务端有生成缩略图,剪切图片,iphone图片旋转判断功能

    html: <form action="<{:AppLink('circle/uploadimg')}>" id="imageform" me ...

  7. 参考__JS

    教程 jQuery选择器总结 日期和时间 Bootstrap日期和时间表单组件 图表 morrisHighcharts 垂直滚动 fsvs 水平滚动 swiper 倒计时 countdownjs 全屏 ...

  8. python函数默认参数坑

    def add(a=3,b): print a,b add(4) 这样写的话,运行的话就会报错:SyntaxError: non-default argument follows default ar ...

  9. java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter

    今天在用git merge 新代码后报了如下错误:java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterE ...

  10. Python之路 day2 字符串/元组/列表/字典互转

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...