
interview
文章平均质量分 71
xiaolienahu
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode #29 in cpp
The question is to divide two integer without multiplication,mod and division. Solution: The only things we can use are + and -. We know that multiplication is actually the shortcut of addition. 2原创 2016-05-24 02:37:01 · 205 阅读 · 0 评论 -
leetcode #30 in cpp
Solution:You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words ex原创 2016-05-25 00:23:57 · 183 阅读 · 0 评论 -
leetcode #31 in cpp
This question is to write the next permutation. Solution: Example: 1,2 1, 3 ---> 1, 2, 3,1; 1 violates the increasing order first.4, 5, 0, 3 -----> 4, 5, 3, 0; 0 violates the increasing order fi原创 2016-05-25 00:24:52 · 243 阅读 · 0 评论 -
leetcode #32 in cpp
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",原创 2016-05-25 02:46:49 · 209 阅读 · 0 评论 -
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 · 206 阅读 · 0 评论 -
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 · 304 阅读 · 0 评论 -
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 · 171 阅读 · 0 评论 -
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 · 200 阅读 · 0 评论 -
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 · 180 阅读 · 0 评论 -
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 · 179 阅读 · 0 评论 -
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 · 230 阅读 · 0 评论 -
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 · 173 阅读 · 0 评论 -
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 · 229 阅读 · 0 评论 -
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 · 212 阅读 · 0 评论 -
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 · 246 阅读 · 0 评论 -
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 · 242 阅读 · 0 评论 -
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 · 186 阅读 · 0 评论 -
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 · 197 阅读 · 0 评论 -
*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 · 183 阅读 · 0 评论 -
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 · 179 阅读 · 0 评论 -
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 · 256 阅读 · 0 评论 -
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 · 200 阅读 · 0 评论 -
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 · 313 阅读 · 0 评论 -
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 · 308 阅读 · 0 评论 -
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 · 192 阅读 · 0 评论 -
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 · 365 阅读 · 0 评论 -
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 · 241 阅读 · 0 评论 -
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 · 150 阅读 · 0 评论 -
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 · 189 阅读 · 0 评论 -
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 · 191 阅读 · 0 评论 -
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 · 218 阅读 · 0 评论 -
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 · 220 阅读 · 0 评论 -
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 · 235 阅读 · 0 评论 -
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 · 271 阅读 · 0 评论 -
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 · 218 阅读 · 0 评论 -
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 · 286 阅读 · 0 评论 -
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 · 171 阅读 · 0 评论 -
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 · 213 阅读 · 0 评论 -
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 · 610 阅读 · 0 评论 -
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 · 207 阅读 · 0 评论