随笔分类 -  LeetCode

Solution and analysis of problems in LeetCode
[Leetcode 106] 130 Surrounded Regions
摘要:Problem:Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surrounded region .For example,X X X XX O O XX X O XX O X XAfter running your function, the board should be:X X X XX X 阅读全文

posted @ 2013-08-17 03:35 freeneng 阅读(228) 评论(0) 推荐(0)

[Leetcode 105] 90 Subsets II
摘要:Problem:Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]Analysi... 阅读全文

posted @ 2013-08-11 14:09 freeneng 阅读(281) 评论(0) 推荐(0)

[Leetcode 104] 131 Palindrome Partitioning
摘要:Problem:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.For example, givens="aab",Return [ ["aa","b"], ["a","a","b"] ]Analysis:At first glance, it seems v 阅读全文

posted @ 2013-08-09 12:11 freeneng 阅读(218) 评论(0) 推荐(0)

[Leetcode 103] 37 Sudoku Solver
摘要:Problem:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'.You may assume that there will be only one unique solution.A sudoku puzzle......and its solution numbers marked in red.Analysis:The basic dfs problem. Notice the structur 阅读全文

posted @ 2013-08-09 10:45 freeneng 阅读(186) 评论(0) 推荐(0)

[Leetcode 101] 47 Permutations II
摘要:Problem:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1].Analysis:An normal way to solve the problem is first generate all the possible permutations, and then com 阅读全文

posted @ 2013-08-07 07:10 freeneng 阅读(244) 评论(0) 推荐(0)

[Leetcode 100] 130 Jump Game II
摘要:Problem: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 m 阅读全文

posted @ 2013-08-07 06:06 freeneng 阅读(252) 评论(0) 推荐(0)

[Leetcode 99] 40 Combination Sum II
摘要:Problem:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, � ,a 阅读全文

posted @ 2013-08-07 05:06 freeneng 阅读(164) 评论(0) 推荐(0)

[Leetcode 98] 54 Spiral Matrix
摘要:Problem:Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return[1,2,3,6,9,8,7,4,5].Analysis:The problem is a simulation problem. With the help of a direction vari 阅读全文

posted @ 2013-08-06 15:14 freeneng 阅读(204) 评论(0) 推荐(0)

[Leetcode 97] 75 Sort Colors
摘要:Problem:Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.Note:You are not suppose to u 阅读全文

posted @ 2013-08-06 14:41 freeneng 阅读(187) 评论(0) 推荐(0)

[Leetcode 96] 41 First Missing Positive
摘要:Problem: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.Analysis:At first thought, hash table can be used in this problem. First hash all the numbers in the giv 阅读全文

posted @ 2013-08-06 13:04 freeneng 阅读(173) 评论(0) 推荐(0)

[Leetcode 95] 103 Binary Tree Zigzag Level Order Traversal
摘要:Problem:Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:... 阅读全文

posted @ 2013-07-28 12:13 freeneng 阅读(187) 评论(0) 推荐(0)

[Leetcode 94] 72 Binary Tree Inorder Traversal
摘要:Problem:Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?Analysis:Basic tree traversal problems. Recursive solution is just to proper arran... 阅读全文

posted @ 2013-07-28 11:48 freeneng 阅读(145) 评论(0) 推荐(0)

[Leetcode 93] 72 Edit Distance
摘要:Problem: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 characterAnalysis:This is the classic dynamic p 阅读全文

posted @ 2013-07-28 11:00 freeneng 阅读(180) 评论(0) 推荐(0)

[Leetcode 92] 52 N-Queens II
摘要:Problem:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.Analysis:This is the extension of the former N-Queens problem. If we use the code before directly, there will be TLE problem. After checking the code, we find the isVali 阅读全文

posted @ 2013-07-28 08:41 freeneng 阅读(262) 评论(0) 推荐(0)

[Leetcode 91] 51 N-Queens
摘要:Problem:Then-queens puzzle is the problem of placingnqueens on ann�nchessboard such that no two queens attack each other.Given an integern, return all distinct solutions to then-queens puzzle.Each solution contains a distinct board configuration of then-queens' placement, where'Q'and' 阅读全文

posted @ 2013-07-28 07:16 freeneng 阅读(240) 评论(0) 推荐(0)

[Leetcode 90] 43 Multiply Strings
摘要:Problem:Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.Analysis:It's the big number simulation problem. Recall the process how we compute the multiplication manually. Multiply the tow numb 阅读全文

posted @ 2013-07-27 08:02 freeneng 阅读(227) 评论(0) 推荐(0)

[Leetcode 89] 34 Search for a Range
摘要:Problem: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].A 阅读全文

posted @ 2013-07-27 06:18 freeneng 阅读(185) 评论(0) 推荐(0)

[Leetcode 88] 33 Search in Rotated Sorted Array
摘要:Problem:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array.Analysis:The problem is 阅读全文

posted @ 2013-07-26 12:56 freeneng 阅读(164) 评论(0) 推荐(0)

[Leetcode 87] 29 Divide Two Integers
摘要:Problem:Divide two integers without using multiplication, division and mod operator.Analysis:I think the problem wants us to use binary search to find the answer instead of simply using some arithmetic operations. So the binary search version comes first. When dealing with it, pay attention to the e 阅读全文

posted @ 2013-07-25 08:27 freeneng 阅读(215) 评论(0) 推荐(0)

[Leetcode 86] 57 Insert Interval
摘要:Problem: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], 阅读全文

posted @ 2013-07-25 06:32 freeneng 阅读(185) 评论(0) 推荐(0)

导航