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

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. MATLAB地图工具箱学习总结(二)大圆和恒向线

    MATLAB地图工具箱学习总结(二)大圆和恒向线 今天要和大家谈一谈大圆.恒向线航道的画法.还是先从案例开始说起,再分别介绍相关的函数. 1                    作业案例:地图投影作 ...

  2. 修正iOS从照相机和相册中获取的图片方向(转)

    - (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientati ...

  3. Android 开机自启服务

    package com.example.lenovo.guangbo; import android.app.Service; import android.content.Intent; impor ...

  4. Hibernate 继承映射

    @Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn()public class Animal ...

  5. linux @后面的主机名如何修改

    @后面的为linux系统的主机名 临时修改方法:执行 hostname 主机名再执行 bash 永久修改方法:修改配置文件/etc/sysconfig/network修改参数HOSTNAME=主机名永 ...

  6. servlet(二)

    一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...

  7. jpg/png格式图片转eps格式的方法总结

    jpg/png格式图片转eps格式的方法总结 转自http://blog.sina.com.cn/s/blog_5410e7b50101lme2.html 用latex写论文的筒子应该遇到这样的问题: ...

  8. Chrome谷歌浏览器下不支持css字体小于12px的解决办法

    先来看下 ie.火狐.谷歌浏览器下各个字体显示情况 ie下: 火狐下: 谷歌下: 从上面的图可以很明显看出谷歌下 css设置字体大小为12px及以下时,显示都是一样大小,都是默认12px; 那么网上一 ...

  9. red hat安装mysql二进制包

    数据包命名格式解释 mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz 黑色粗体表示为包名称 蓝色表示linux系统二进制包 红色表示构架     1.上传mysql- ...

  10. nginx配合zabbix编译安装时web下一步跳转问题

    很多时候编译安装的时候把zabbix的php包拷贝到web所在目录之后(本文为nginx所在html目录),网页打开http:/localhost/zabbix却进不去下图: 或者是点了下一步没反应, ...