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" 实现一个将字符串进行指定行 ...
随机推荐
- 浅析word-break work-wrap区别
word-break:[断词] 定义:规定自动换行的处理方法. 注:通过word-break使用,可以实现让浏览器在任意位置换行. 语法:word-break: normal|break-all| ...
- HTML基础(3)
1.块元素和内嵌元素(block\inline) 块的特征: 独占一行 不设定宽度,宽度将撑满整行 能设置所有样式 内嵌的特征: 默认同行可以继续跟同类型标签 内容撑开宽度 不支持宽高 不支持上下的m ...
- vc6.0如何显示行号以及出现版本不兼容问题
有时编译时,提示某某行有错,但是要定位到某一行的话,如果在编辑页面能够将行号显示出来,查找也就更方便了,下面我来介绍一下让VC6.0显示行号的方法. 工具/原料 VC6.0.显示行号的插件 方 ...
- 程序设计入门——C语言 第8周编程练习 2GPS数据处理(6分)
题目内容: NMEA-0183协议是为了在不同的GPS(全球定位系统)导航设备中建立统一的BTCM(海事无线电技术委员会)标准,由美国国家海洋电子协会(NMEA-The National Marine ...
- OAM
OAM -- Operation Administration and Maintenance[ˈmentənəns]. 根据运营商网络运营的实际需要,通常将网络的管理工作划分为3大类:操作(Oper ...
- ORACLE误删除ASM磁盘修复
在数据库运维中,总会遇到一些粗心大意的DBA,一不小心删除一些东西,这里举例讲解在误删除ASM磁盘之后,如果用KFED工具进行恢复: [grid@RAC1 ~]$ sqlplus / as sysas ...
- MySQL wamp密码修改
WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...
- python3 实现简单信用卡管理程序
1.程序执行代码: #Author by Andy #_*_ coding:utf-8 _*_ import os,sys,time Base_dir=os.path.dirname(os.path. ...
- adb命令学习
录制屏幕操作Android4.4版本以上支持录制屏幕 adb shell screenrecord /sdcard/demo.mp4 ADB logcat 输出时间信息: adb logcat -v ...
- DTD的作用
在介绍DTD的作用之前先介绍一下SGML:SGML SGML(Standard Generalized Markup Language,标准通用标记语言),是一种定义电子文档结构和描述其内容的国际标准 ...