随笔分类 -  LeetCode

摘要:Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a character思考:DP。dp[i-1][j]+1:删除一个字符dp[i][j-1]+1:插入一个字符dp 阅读全文
posted @ 2014-01-14 15:01 七年之后 阅读(185) 评论(0) 推荐(0)
摘要:Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"click to show corner cases.Corner Cases:Did you consider the case wherepath="/../"?In this case, you should return&quo 阅读全文
posted @ 2014-01-13 15:32 七年之后 阅读(179) 评论(0) 推荐(0)
摘要:Given an array of words and a lengthL, format the text such that each line has exactlyLcharacters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces' 'when necessary so that each line 阅读全文
posted @ 2014-01-12 21:38 七年之后 阅读(383) 评论(0) 推荐(0)
摘要:Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.思考:先首尾连成环,head前进(len-k%len)步,拆环。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * L 阅读全文
posted @ 2014-01-12 16:55 七年之后 阅读(165) 评论(0) 推荐(0)
摘要:The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123""132""213""231""312""321"Givennandk, return thekthpermutation sequence.Not 阅读全文
posted @ 2014-01-12 16:45 七年之后 阅读(169) 评论(0) 推荐(0)
摘要:Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]思考:ACM蛇形填数。class Solution {public: vector > generateMatrix(int n) { vector > res; if(n==0) ... 阅读全文
posted @ 2014-01-12 14:53 七年之后 阅读(216) 评论(0) 推荐(0)
摘要:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.思考:同样,满足条件res++即可。class Solution {private: int res;public: bool check(int *a,int k,int i) { for(int j=0;j<k;j++) { if(a[j]==i||abs(a[j]-i)==abs... 阅读全文
posted @ 2014-01-12 13:38 七年之后 阅读(201) 评论(0) 推荐(0)
摘要:Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinc... 阅读全文
posted @ 2014-01-12 13:32 七年之后 阅读(391) 评论(0) 推荐(0)
摘要:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.思考:map应用。遍历两次strs,第一次建立map,第二次打印符合提议的字符串。class Solution {private: vector res; map m;public: vector anagrams(vector &strs) { int i; for(i=0;i::iterator iter=m.find(t... 阅读全文
posted @ 2014-01-11 14:31 七年之后 阅读(164) 评论(0) 推荐(0)
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique perm... 阅读全文
posted @ 2014-01-11 13:57 七年之后 阅读(171) 评论(0) 推荐(0)
摘要:Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,... 阅读全文
posted @ 2014-01-11 12:30 七年之后 阅读(147) 评论(0) 推荐(0)
摘要:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, givens="leetcode",dict=["leet", "code"].Return true because"leetcode"can be segmented as"leet code&quo 阅读全文
posted @ 2014-01-07 15:14 七年之后 阅读(213) 评论(0) 推荐(0)
摘要: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 not 阅读全文
posted @ 2014-01-06 22:03 七年之后 阅读(203) 评论(0) 推荐(0)
摘要:Sort a linked list in O(n log n) time using constant space complexity.思路:分治+递归。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {private: int getlen(ListNode *head) { Lis... 阅读全文
posted @ 2014-01-06 16:35 七年之后 阅读(244) 评论(0) 推荐(0)
摘要:Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line.思考:考虑以下几种情况:1.points中0、1、2个点;2.points含相同点如{[0,0],[0,0]},{[1,1],[1,1],[2,2],[2,2]}};3.斜率为无穷。/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Poi... 阅读全文
posted @ 2013-12-29 19:40 七年之后 阅读(218) 评论(0) 推荐(0)
摘要:Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].Example 2:Given[1,2],[3,5],[6,7],[8 阅读全文
posted @ 2013-12-20 14:37 七年之后 阅读(253) 评论(0) 推荐(0)
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A =[2,3,1,1,4]The minimum n 阅读全文
posted @ 2013-12-19 14:50 七年之后 阅读(132) 评论(0) 推荐(0)
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse.cl 阅读全文
posted @ 2013-12-19 14:11 七年之后 阅读(180) 评论(0) 推荐(0)
摘要:Implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prototype should be:bool i 阅读全文
posted @ 2013-12-19 11:47 七年之后 阅读(232) 评论(0) 推荐(0)
摘要:Validate if a given string is numeric.Some examples:"0"=>true" 0.1 "=>true"abc"=>false"1 a"=>false"2e10"=>trueNote:It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementin 阅读全文
posted @ 2013-12-18 15:48 七年之后 阅读(333) 评论(0) 推荐(0)