- 博客(62)
- 收藏
- 关注
原创 leetcode #73 in cpp
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Solution 1: O(m+n) space.We can use two arrays to record which row and which column should be set zero.
2016-05-31 22:34:28 167
原创 leetcode #72 in cpp
Solution:We use DP where dp[i][j] represents minimum steps from word1[0...i-1] to words2[0...j-1]Code:class Solution {public: int minDistance(string word1, string word2) { int len1
2016-05-31 22:11:40 151
原创 leetcode #71 in cpp
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Solution:Rules could be found in wiki. 1. when we meet
2016-05-31 09:45:25 140
原创 leetcode #70 in cpp
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?Solution:We use DP. see co
2016-05-31 09:14:19 164
原创 leetcode #69 in cpp
Implement int sqrt(int x).Compute and return the square root of x.Solution:Use binary search. class Solution {public: int mySqrt(int x) { long int left = 0; long int right
2016-05-31 08:57:01 513
原创 leetcode #68 in cpp
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that i
2016-05-31 04:42:44 164
原创 leetcode #67 in cpp
Solution:It is the same as #66. We use 2 instead of 10 as the modulo.Code:class Solution {public: string addBinary(string a, string b) { int carry = 0; if(a.length() < b.lengt
2016-05-31 02:10:58 142
原创 leetcode #66 in cpp
Solution: 1.start from the tail of the input, and keep adding 1 until we reach the head of the input or the digit + 1 is less than 10. 2.check if the head >= 10. If so insert an extra 1 to the inp
2016-05-31 01:48:14 247
原创 leetcode #64 in cpp
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
2016-05-31 01:00:16 193
原创 leetcode #63 in cpp
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
2016-05-30 22:51:08 206
原创 leetcode #62 in cpp
Solution:Suppose if w are currently in a cell, we can only move right or down. Thus the number of paths from this cell to the destination is the sum the number of paths to destination from the cell
2016-05-30 22:29:10 156
原创 leetcode #61 in cpp
Solution: If k 1->2->3->4, say 3 is the new head1->2->null 3->4----|______________|link from tail to original headIf k >= length of the node. we rotate k%length. It is simply like
2016-05-30 11:26:47 239
原创 leetcode #60 in cpp
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""3
2016-05-30 09:49:42 174
原创 leetcode #59 in cpp
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [
2016-05-30 07:42:00 166
原创 leetcode #58 in cpp
Solution:When we meet a non-empty-space character we start counting. When we meet a empty-space when we are counting, we stop counting. Code:class Solution {public: int lengthOfLastWord(s
2016-05-30 07:22:21 193
原创 leetcode #57 in cpp
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.E
2016-05-30 06:59:19 161
原创 leetcode #56 in cpp
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].Solution 1: O(n^2) Code:/** * Definition for
2016-05-30 03:35:07 121
原创 leetcode #55 in cpp
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 i
2016-05-30 02:46:27 198
原创 leetcode #54 in cpp
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 ]]
2016-05-30 02:11:47 334
原创 leetcode #53 in cpp
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] ha
2016-05-29 22:30:30 163
原创 leetcode #52 in cpp
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.This question is simply to calculate how many distinct solution
2016-05-29 11:13:34 276
原创 leetcode #51
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queen
2016-05-29 10:56:45 272
原创 leetcode #50 in cpp
Implement pow(x, n).Solution:Brute force method is to have x^n = x*x*.....*x. We use divide and conquer method pow(x,n) = pow(x,n/2)^2 if n is even or pow(x,n) = x*pow(x,n-1) if n is odd. Co
2016-05-29 04:20:28 175
原创 leetcode #49 in cpp
Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]Note:
2016-05-29 03:53:27 217
原创 leetcode #48 in cpp
This is to rotate a matrix clockwise with 90 degree.Solution 1: Naive rotateSuppose the input is a n*n matrix. Then the ith row should be put to (n - 1 - i)th column. For example n =3. Then
2016-05-29 02:26:19 149
原创 *leetcode #47 in cpp
The solution is very similar to the solution of $46. The only thing we should do is to avoid duplicates. Analysis to avoid duplicates is almost the same as the one in #40. Code: class Solution {
2016-05-28 22:51:17 156
原创 leetcode #46 in cpp
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], and [3,2,1].Soluti
2016-05-28 04:32:36 169
原创 leetcode #45 in cpp
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 i
2016-05-28 03:46:57 157
原创 leetcode #44 in cpp
Solution: We use DP to solve this problem. Initialize bool dp[pattern length + 1][ s length + 1] = {false}, where dp[i][j] = true if pattern[0....i-1] matches s[0....j-1] and false otherwise. Th
2016-05-27 22:48:15 214
原创 leetcode #43 in cpp
The question is to multiply two integer in strings.Solution: The idea is to use divide-and-conquer. For example: 234 * 235 = (234*5) + (234*3)*10 + (234*2)*100and 234*5 = 2*5*100 + 3*5*10
2016-05-27 08:48:05 184
原创 leetcode $42 in cpp
Code: (This method is not the best one. There are other methods using two pointers)Key: a bar could be a left bound if it is no less than its right. A bar could be a right bound if it is no less tha
2016-05-27 04:03:40 189
原创 leetcode #41 in cpp
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant
2016-05-27 01:33:14 143
原创 leetcode #40 in cpp
Solution:It is very similar to question #39 except that the candidates are given in a collection instead of a set, which would allow duplicates in candidates, and that only one number could be used
2016-05-26 11:54:49 196
原创 leetcode #39 in cpp
The question is to find combination of numbers sum up to a target in a set. Solution: 1. Note that a set contains no duplicates. 2. the combination could be unlimited in size. Say target = 3, ar
2016-05-26 06:37:52 205
原创 leetcode #38 in cpp
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
2016-05-26 05:31:24 146
原创 leetcode #37 in cpp
The question is to solve a Sudoku.Solution:We scan through the Sudoku. Whenever we meet a '.', we try to fill it 1 to 9. Every time we fill in a number, first check if this number could be put wi
2016-05-26 04:57:08 152
原创 leetcode #36 in cpp.
The question is to determine a Sudoku is valid. Solution: if a Sudoku is valid, each row, each column and each block in it would not have duplicates.Thus we have 3 loops to check if each condition
2016-05-26 00:27:47 175
原创 leetcode #35 in cpp
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.
2016-05-25 22:53:09 140
原创 leetcode #34 in cpp
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 of O(log n).If the target is not found
2016-05-25 10:07:54 275
原创 leetcode #33 in cpp
The solution is to use binary search. Each time we choose a partition, we need to see if it contains a pivot or not. If partition[left end] > partition[right end] then it contains a pivot, otherwise i
2016-05-25 05:13:15 176
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人