Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
The cache is initialized with a positive capacity.
Follow up: Could you do both operations in O(1) time complexity?
Example:
1 2 3 4 5 6 7 8 9 10 11
LRUCache cache = new LRUCache( 2 /* capacity */ );
LRUCache.prototype.deleteLRU = function () { deletethis.storage[this.keyList.shift()] }
LRUCache.prototype.updateKeyList = function (key) { if (this.storage.hasOwnProperty(key)) { var index = this.keyList.findIndex(k => key === k) this.keyList.splice(index, 1) } this.keyList.push(key) }
/** * Your LRUCache object will be instantiated and called as such: * var obj = new LRUCache(capacity) * var param_1 = obj.get(key) * obj.put(key,value) */
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
Note:
Your returned answers (both index1 and index2) are not zero-based. You may assume that each input would have exactly one solution and you may not use the same element twice. Example:
Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
Note:
Your returned answers (both index1 and index2) are not zero-based. You may assume that each input would have exactly one solution and you may not use the same element twice. Example:
Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode}head * @param {number}val * @return {ListNode} */ var removeElements = function(head, val) { if (!head) { return head } let array = listNodeToArray(head) array = array.filter(i => (i !== val)) return arrayToListNode(array) };
functionlistNodeToArray (head) { let array = [] while (head) { array.push(head.val) head = head.next } return array }
functionarrayToListNode(array) { if(!array || !array.length) { returnnull } let node let head = new ListNode(array[0]) let pnode = head //pnode变量用来保存前一个节点 for(let i = 1; i < array.length; i++) { node = new ListNode(array[i]) pnode.next = node //将前一个节点的next指向当前节点 pnode = node //将node赋值给pnode } return head }
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode}head * @return {ListNode} */ var reverseList = function(head) { if (!head) { return head } let parentNode = null while (head) { let current = new ListNode(head.val) current.next = parentNode if (head.next) { let next = new ListNode(head.next.val) next.next = current } else { let next = null return current } parentNode = current head = head.next } return head };
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarray [4,3] has the minimal length under the problem constraint.
/** * @param {number}s * @param {number[]}nums * @return {number} */ var minSubArrayLen = function(s, nums) { var left = 0; var right = -1; // right 的起始位置很重要,这里选择-1 [left, right]这个区间刚开始是没有值的 var tmpSum = 0; var minLength;
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Example:
Input: 38 Output: 2 Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up: Could you do it without any loop/recursion in O(1) runtime?
解题方案
思路 1 **- 时间复杂度: O(1)**- 空间复杂度: O(1)**
原理:
假设输入的数字是一个5位数字num,则num的各位分别为a、b、c、d、e
有如下关系:num = a * 10000 + b * 1000 + c * 100 + d * 10 + e
即:num = (a + b + c + d + e) + (a * 9999 + b * 999 + c * 99 + d * 9)
因为 a * 9999 + b * 999 + c * 99 + d * 9 一定可以被9整除,因此num模除9的结果与 a + b + c + d + e 模除9的结果是一样的。
对数字 a + b + c + d + e 反复执行同类操作,最后的结果就是一个 1-9 的数字加上一串数字,最左边的数字是 1-9 之间的,右侧的数字永远都是可以被9整除的。