- 博客(123)
- 收藏
- 关注
原创 Leetcode 78. Subsets
Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,3], a solution is:[ [3], [1], [2], [1,
2017-01-31 14:41:16 215
原创 Leetcode 77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.For example, If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]s思
2017-01-31 14:18:22 118
原创 Leetcode 75. Sort Colors
Given an array with n objects 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,
2017-01-31 13:35:16 151
原创 Leetcode 74. Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of each row is
2017-01-31 10:09:49 125
原创 Leetcode 73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea.
2017-01-31 09:51:53 248
原创 Leetcode 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” Corner Cases:Did you consider the case where path = “/../”? -
2017-01-31 08:17:24 546
原创 Leetcode 70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a positive inte
2017-01-31 04:57:04 343
原创 Leetcode 69. Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.s思路: 1.这类题用binary search. 这样复杂度就只有o(lgx)。能用binary search的原因是,sqrt(x)可以尝试从1到x,而且sqrt(x)是单调增函数,每次可以根据binary search到的candidate和结果比较来判断是所以
2017-01-31 04:25:56 174
原创 Leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”.s思路: 1. 这类题,用string去模拟int做各种运算,比如这题的加法,还有之前的415. Add Strings, 44. Multiply Strings。 2.
2017-01-30 13:20:33 181
原创 Leetcode 66. Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits are st
2017-01-30 12:10:55 148
原创 Leetcode 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any
2017-01-30 10:50:23 167
原创 Leetcode 63. Unique Paths II
Follow up for “Unique Paths”:Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.For ex
2017-01-30 10:04:13 111
原创 Leetcode 62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the botto
2017-01-30 09:21:40 202
原创 Leetcode 61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.s思路: 1. 没什么好说的。需要理解题意,由于往右shift k个位置,k很可能
2017-01-30 08:52:50 145
原创 Leetcode 60. Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3):“123” “132” “213” “231” “312”
2017-01-29 15:00:46 161
原创 Leetcode 59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2n^2 in spiral order.For example, Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6,
2017-01-29 12:11:31 121
原创 Leetcode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined as
2017-01-29 11:38:12 131
原创 Leetcode 56. Merge Intervals
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].s思路: 1. 先排序,让相邻的interval就在左右,从而复杂度o(nlgn). 2. 每次从vector<
2017-01-29 10:53:20 164
原创 Leetcode 55. Jump Game
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
2017-01-29 10:24:04 119
原创 Leetcode 54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), 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
2017-01-29 09:56:38 154
原创 Leetcode 53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the
2017-01-29 08:10:15 178
原创 Leetcode 50. Pow(x, n)
Implement pow(x, n).s思路: 1. 举例:5205^{20} ,如果一次一次的计算,这就太简单粗暴无聊了!注意到,类似的运算始终朝一个方向进行,都是×5,所以可以写成510×5105^{10}×5^{10},而510=55×555^{10}=5^{5}×5^{5}。这个方法就是binary search。bug:忽略了n为负数的可能!更大bug:对负数取反,就要时刻考虑是
2017-01-28 10:31:39 154
原创 Leetcode 49. Group Anagrams
Given an array of strings, group anagrams together.For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return:[ [“ate”, “eat”,”tea”], [“nat”,”tan”], [“bat”] ]s思路: 1. 这道题,让group
2017-01-28 10:10:26 118
原创 Leetcode 48. Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up: Could you do this in-place?s思路: 1. 初看这个题,比较nasty,很烦。矩阵旋转,简单粗暴的做法:每个元素根据坐标找到旋转后的位置,每个坐标都要计算
2017-01-28 09:58:05 178
原创 Leetcode 47. Permutations II
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], [2,1,1]
2017-01-28 09:13:00 154
原创 Leetcode 46. Permutations
Given a collection of distinct 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,1,2], [3,2,1]]s思路:
2017-01-27 14:51:34 147
原创 Leetcode 43. Multiply Strings
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.Note:The length of both num1 and num2 is < 110. Both num1 and num2 contains only digits 0-9.
2017-01-27 14:28:01 174
原创 Leetcode 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.No
2017-01-27 10:33:18 187
原创 Leetcode 39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C
2017-01-27 10:05:12 115
原创 Leetcode 38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, …1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as “one 2, the
2017-01-27 09:26:22 123
原创 Leetcode 36.Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’. A partially filled sudoku whic
2017-01-27 09:02:02 145
原创 Leetcode 35. Search Insert Position
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
2017-01-27 08:25:10 151
原创 Leetcode 34. Search for a Range
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the order of O(log n).If the target is
2017-01-27 07:55:38 168
原创 Leetcode 33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the ar
2017-01-27 04:58:06 218
原创 Leetcode 31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible orde
2017-01-26 14:30:16 167
原创 Leetcode 29. Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.s思路: 1. 除法,可以用减法做。例如:20/4:20每次减去4,直到小于4,每减一次计数器加一,结果就是商。这个方法太直接粗暴了! 2. 如何加速?当然是移位。例如:4每次往
2017-01-26 13:38:51 142
原创 Leetcode 28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.思路: 1. 这个题很大陷阱。以为直接用two pointer,分别指向haystack和needle,比较是否相等,不等则指向needle的指针重新指
2017-01-26 12:45:01 110
原创 Leetcode 27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The order
2017-01-26 10:25:40 109
原创 Leetcode 26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with cons
2017-01-26 10:07:30 156
原创 Leetcode 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may no
2017-01-26 09:39:06 159
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人