摘要:
题目Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is no 阅读全文
摘要:
题目Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.思路1. 使用数组模拟哈希表, 数组下标0-25分别代表字符'a'-'z', a[0] 代表 'a' 在单词中出现的次数2. 排序, 只有相邻的单词才有可能是相同的3. 这么慢的方法没想到 176ms 就能通过总结1. word 起初没有对 char 数组初始化, 结果 VS 成功运行, 但 Leetcode 上却是 W 阅读全文
摘要:
题目Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.思路1. 排序是 nlogn, 没有其他想法了2. 参考别人思路. 将 A[i] 月 A[A[i]] 替换, 第二遍 PASS 检查 A[i] == i代码class Solution {public: int f... 阅读全文
摘要:
题目Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted acco 阅读全文
摘要:
题目:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].思路:1. 阅读全文
摘要:
题目大整数乘法思路:1. 转化成分组背包问题, 代码比较 tricky2. 将 string 相乘转化为 string 按位乘再加的过程3. 细节. 不同长度 string 相加可以先补 0, 对齐. 按位相乘的时候, 可以从左向右计算, 便于移位代码:class Solution {public: string multiply(string num1, string num2) { if(num1.size() len2) { res.append(num1.size()-num2.size(), '0').append(num2); num2 = res; } re... 阅读全文
摘要:
题目:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0思路:1. 二分法变形, 最讨 阅读全文
摘要:
题目:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of i 阅读全文
摘要:
题目:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?update先转置, 后按列翻转class Solution {public: void rotate(vector > &matrix) { transpose(matrix); for(int i = 0; i > &matrix) { int n = matrix.size(... 阅读全文
摘要:
题目Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units o 阅读全文