leetcode6
好吧,今天晚上赶项目确实是做不了三道题目了,最近项目在网络编程方面有些进步,学到了东西,有时间再积累下来,很深的体会就是,和别人一起写代码,虽然蛋疼但是比自己一个人写要好点,不过发现自己对链表和排序什么的都不太熟练啊,得好好认真练习,加油!
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的更多相关文章
- leetcode6:Zigzag Conversion@Python
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode6 ZigZag Conversion
题意: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- leetcode6 Reverse Words in a String 单词取反
Reverse Words in a String 单词取反 whowhoha@outlook.com Question: Given an input string s, reverse the ...
- [Swift]LeetCode6. Z字形变换 | ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode6. Z字形变换
描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- LeetCode6.Z字形变换 JavaScript
将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...
- string leetcode-6.ZigZag
6. ZigZag Conversion 题面 The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- LeetCode6 dp
120. Triangle 我的解法用了一个二维数组,这样比较浪费空间.O(n*n) 但是标准答案自底向上,一是不需要进行特别判断,二是可以覆盖数组,则只需要O(n)的空间大小. class Solu ...
- Leetcode6.ZigZag ConversionZ字形变换
将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: 之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR" 实现一个将字符串进行指定行 ...
随机推荐
- install intel c/c++ compiler
通过在Intel官网上申请试用版本Intel® Parallel Studio XE Cluster Edition for Linux,会让你提交邮箱等信息,完成后会很快回复邮件,邮件会给出下载地址 ...
- php 资源
ThinkPHP http://www.thinkphp.cn/ 小案例 http://www.helloweba.com/php.html Github上的PHP资源汇总大全 http://www. ...
- winpcap抓包原理
winpcap抓包原理 WinPcap 是由伯克利分组捕获库派生而来的分组捕获库,它是在Windows 操作平台上来实现对底层包的截取过滤.WinPcap 是 BPF 模型和 Libpcap 函数库在 ...
- plist基本操作
重要概念:某些路径下“只能读,不能写”的原因 iPhone.ipad真机上 Resouces文件夹:是只读的,无法写入. document 和temp文件夹:可读,可写. 一.工程结构
- Python之路 day2 字典练习题之 三级菜单
#Author:ersa ''' 程序: 三级菜单 要求: 打印省.市.县三级菜单 可返回上一级 可随时退出程序 ''' menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{ ...
- JQuery实现table分页
1.直接贴代码: ; //每页显示的记录条数 ; //显示第curPage页 var len; //总行数 var page; //总页数 $(function(){ len =$(; //去掉表头 ...
- express+gulp构建项目(二)启动项目和主文件
这一次整理的内容是项目主文件和如何启动项目. 启动项目 通过nodejs官网的例子https://nodejs.org/docs/latest-v4.x/doc/api/synopsis.html我们 ...
- Java 中的 request 和response 区别
1.response 属于重定向请求: 其地址栏的URL会改变: 会向服务器发送两次请求: 2. request 属于请求转发: 其地址栏的URL不会改变: 向服务器发送一次请求: 举一个区分它们的简 ...
- Nodejs express中创建ejs项目,解决express下默认创建jade,无法创建ejs问题
最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了, 书上命令为: express -t ejs microblog 可是执行后,仍 ...
- 检测cpu是否支持虚拟化和二级地址转换【转】
SLAT:二级地址转换 用微软的小工具“Coreinfo.exe” 下载地址是: http://technet.microsoft.com/en-us/sysinternals/cc835722 ...