-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
465 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
//运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。 | ||
// | ||
// | ||
// | ||
// 实现 LRUCache 类: | ||
// | ||
// | ||
// LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 | ||
// int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。 | ||
// void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上 | ||
//限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。 | ||
// | ||
// | ||
// | ||
// | ||
// | ||
// | ||
// 进阶:你是否可以在 O(1) 时间复杂度内完成这两种操作? | ||
// | ||
// | ||
// | ||
// 示例: | ||
// | ||
// | ||
//输入 | ||
//["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] | ||
//[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] | ||
//输出 | ||
//[null, null, null, 1, null, -1, null, -1, 3, 4] | ||
// | ||
//解释 | ||
//LRUCache lRUCache = new LRUCache(2); | ||
//lRUCache.put(1, 1); // 缓存是 {1=1} | ||
//lRUCache.put(2, 2); // 缓存是 {1=1, 2=2} | ||
//lRUCache.get(1); // 返回 1 | ||
//lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3} | ||
//lRUCache.get(2); // 返回 -1 (未找到) | ||
//lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3} | ||
//lRUCache.get(1); // 返回 -1 (未找到) | ||
//lRUCache.get(3); // 返回 3 | ||
//lRUCache.get(4); // 返回 4 | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 1 <= capacity <= 3000 | ||
// 0 <= key <= 3000 | ||
// 0 <= value <= 104 | ||
// 最多调用 3 * 104 次 get 和 put | ||
// | ||
// Related Topics 设计 哈希表 链表 双向链表 | ||
// 👍 1460 👎 0 | ||
|
||
package com.cute.leetcode.editor.cn; | ||
public class LruCache { | ||
public static void main(String[] args) { | ||
Solution solution = new LruCache().new Solution(); | ||
} | ||
//leetcode submit region begin(Prohibit modification and deletion) | ||
/* | ||
* 方法一:采用哈希表+双向链表实现,哈希表是为了在O(1)时间get,双向链表是为了方便在O(1)新增、淘汰、get后移到首部 | ||
* */ | ||
class LRUCache { | ||
|
||
public class DLinkedNode { | ||
int key; | ||
int value; | ||
DLinkedNode prev; | ||
DLinkedNode next; | ||
public DLinkedNode() {} | ||
public DLinkedNode(int _key, int _value) {key = _key; value = _value;} | ||
} | ||
/*双向链表节点*/ | ||
|
||
/*哈希表定义*/ | ||
private Map<Integer, DLinkedNode> cache = new HashMap<Integer, DLinkedNode>(); | ||
/*key--node*/ | ||
private int size; | ||
private int capacity; | ||
private DLinkedNode head, tail; | ||
|
||
|
||
public LRUCache(int capacity) { | ||
this.size=0; | ||
this.capacity=capacity; | ||
head=new DLinkedNode(); | ||
tail=new DLinkedNode(); | ||
head.next=tail; | ||
tail.prev=head; | ||
} | ||
|
||
public int get(int key) { | ||
DLinkedNode node=cache.get(key); | ||
if(node==null) return -1; | ||
moveToHead(node); | ||
return node.value; | ||
} | ||
|
||
public void put(int key, int value) { | ||
DLinkedNode node = cache.get(key); | ||
if(node==null){ | ||
//如果key不存在,创建一个新节点 | ||
DLinkedNode newNode=new DLinkedNode(key,value); | ||
//添加到哈希表 | ||
cache.put(key,newNode); | ||
//添加到双向链表头部 | ||
addToHead(newNode); | ||
++size; | ||
if(size>capacity){ | ||
//若超出容量,删除双向链表尾部节点,并拿到删除的key去哈希表删除 | ||
DLinkedNode tail=removeTail(); | ||
//删除哈希表对应的项 | ||
cache.remove(tail.key); | ||
--size; | ||
} | ||
}else{ | ||
//如果key存在,先通过哈希表定位,再修改value,并移动到头部 | ||
node.value=value; | ||
moveToHead(node); | ||
} | ||
} | ||
|
||
/* | ||
* 1.新增元素的操作--加入链表头部,若超过容量淘汰链表尾部 | ||
* 所以需要方法 | ||
* 1.移动某元素到头部=(删除该元素+新增元素到头部) | ||
* 2.新增元素到头部 | ||
* 3.尾部删除=(删除该元素+返回删除的值) | ||
* 4.删除某元素(任意位置--这时候由于是双向链表所以可以在O(1)时间完成) | ||
* */ | ||
private void moveToHead(DLinkedNode node){ | ||
removeNode(node); | ||
addToHead(node); | ||
} | ||
|
||
private void addToHead(DLinkedNode node){ | ||
node.prev=head; | ||
node.next=head.next; | ||
head.next.prev=node; | ||
head.next=node; | ||
} | ||
|
||
private DLinkedNode removeTail(){ | ||
DLinkedNode res=tail.prev; | ||
removeNode(res); | ||
return res; | ||
} | ||
|
||
private void removeNode(DLinkedNode node){ | ||
node.prev.next=node.next; | ||
node.next.prev=node.prev; | ||
} | ||
} | ||
|
||
/** | ||
* Your LRUCache object will be instantiated and called as such: | ||
* LRUCache obj = new LRUCache(capacity); | ||
* int param_1 = obj.get(key); | ||
* obj.put(key,value); | ||
*/ | ||
//leetcode submit region end(Prohibit modification and deletion) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。 | ||
// | ||
// 进阶:你能尝试使用一趟扫描实现吗? | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
//输入:head = [1,2,3,4,5], n = 2 | ||
//输出:[1,2,3,5] | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
//输入:head = [1], n = 1 | ||
//输出:[] | ||
// | ||
// | ||
// 示例 3: | ||
// | ||
// | ||
//输入:head = [1,2], n = 1 | ||
//输出:[1] | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 链表中结点的数目为 sz | ||
// 1 <= sz <= 30 | ||
// 0 <= Node.val <= 100 | ||
// 1 <= n <= sz | ||
// | ||
// Related Topics 链表 双指针 | ||
// 👍 1421 👎 0 | ||
|
||
package com.cute.leetcode.editor.cn; | ||
public class RemoveNthNodeFromEndOfList { | ||
public static void main(String[] args) { | ||
Solution solution = new RemoveNthNodeFromEndOfList().new Solution(); | ||
} | ||
//leetcode submit region begin(Prohibit modification and deletion) | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public ListNode removeNthFromEnd(ListNode head, int n) { | ||
|
||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。 | ||
// | ||
// 算法的时间复杂度应该为 O(log (m+n)) 。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
//输入:nums1 = [1,3], nums2 = [2] | ||
//输出:2.00000 | ||
//解释:合并数组 = [1,2,3] ,中位数 2 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
//输入:nums1 = [1,2], nums2 = [3,4] | ||
//输出:2.50000 | ||
//解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5 | ||
// | ||
// | ||
// 示例 3: | ||
// | ||
// | ||
//输入:nums1 = [0,0], nums2 = [0,0] | ||
//输出:0.00000 | ||
// | ||
// | ||
// 示例 4: | ||
// | ||
// | ||
//输入:nums1 = [], nums2 = [1] | ||
//输出:1.00000 | ||
// | ||
// | ||
// 示例 5: | ||
// | ||
// | ||
//输入:nums1 = [2], nums2 = [] | ||
//输出:2.00000 | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// nums1.length == m | ||
// nums2.length == n | ||
// 0 <= m <= 1000 | ||
// 0 <= n <= 1000 | ||
// 1 <= m + n <= 2000 | ||
// -106 <= nums1[i], nums2[i] <= 106 | ||
// | ||
// Related Topics 数组 二分查找 分治 | ||
// 👍 4647 👎 0 | ||
|
||
package com.cute.leetcode.editor.cn; | ||
public class MedianOfTwoSortedArrays { | ||
public static void main(String[] args) { | ||
Solution solution = new MedianOfTwoSortedArrays().new Solution(); | ||
} | ||
//leetcode submit region begin(Prohibit modification and deletion) | ||
class Solution { | ||
public double findMedianSortedArrays(int[] nums1, int[] nums2) { | ||
|
||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//请实现一个函数,把字符串 s 中的每个空格替换成"%20"。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// 输入:s = "We are happy." | ||
//输出:"We%20are%20happy." | ||
// | ||
// | ||
// | ||
// 限制: | ||
// | ||
// 0 <= s 的长度 <= 10000 | ||
// 👍 108 👎 0 | ||
|
||
package com.cute.leetcode.editor.cn; | ||
public class TiHuanKongGeLcof { | ||
public static void main(String[] args) { | ||
Solution solution = new TiHuanKongGeLcof().new Solution(); | ||
} | ||
//leetcode submit region begin(Prohibit modification and deletion) | ||
class Solution { | ||
public String replaceSpace(String s) { | ||
StringBuilder string = new StringBuilder(); | ||
for(int i=0;i<s.length();i++){ | ||
char c=s.charAt(i); | ||
if(c==' '){ | ||
string.append("%20"); | ||
}else{ | ||
string.append(c); | ||
} | ||
} | ||
return string.toString(); | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) | ||
|
||
} |
Oops, something went wrong.