摘要: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest ... 阅读全文
posted @ 2014-04-16 10:04 Awy 阅读(169) 评论(0) 推荐(0) 编辑
摘要: 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 i... 阅读全文
posted @ 2014-04-16 09:12 Awy 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 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], []]思路:这道题在Subsets基... 阅读全文
posted @ 2014-04-11 23:26 Awy 阅读(108) 评论(0) 推荐(0) 编辑
摘要: Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not co... 阅读全文
posted @ 2014-04-11 22:46 Awy 阅读(241) 评论(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 @ 2014-04-11 10:35 Awy 阅读(153) 评论(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.思路 阅读全文
posted @ 2014-04-11 08:56 Awy 阅读(193) 评论(0) 推荐(0) 编辑
摘要: 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 which is valid.Note:A valid Sudoku board (partially filled) is not necessarily solvable. Only the f 阅读全文
posted @ 2014-04-09 20:50 Awy 阅读(135) 评论(0) 推荐(0) 编辑
摘要: Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not.思路: 阅读全文
posted @ 2014-04-09 17:11 Awy 阅读(147) 评论(0) 推荐(0) 编辑
摘要: Given a stringsconsists 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 a character sequence consists of non-space characters only.For example,Givens="Hello Worl 阅读全文
posted @ 2014-04-08 15:35 Awy 阅读(177) 评论(0) 推荐(0) 编辑
摘要: Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of 阅读全文
posted @ 2014-04-08 14:59 Awy 阅读(97) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.T 阅读全文
posted @ 2014-04-07 23:22 Awy 阅读(237) 评论(0) 推荐(0) 编辑
摘要: Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extra space.For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 ... 阅读全文
posted @ 2014-04-07 22:57 Awy 阅读(161) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路:求二叉树最小深度,典型的递归。直接上代码吧,都看得明白。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right... 阅读全文
posted @ 2014-04-06 17:43 Awy 阅读(124) 评论(0) 推荐(0) 编辑
摘要: Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. Howe 阅读全文
posted @ 2014-04-06 17:07 Awy 阅读(116) 评论(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 ]]思路:这道题顺时针从1到n2放入数组,没啥高深数据结构或算法,直接顺时针打印就可以了。class Solution {public: vector > generateMatrix(int n) { ... 阅读全文
posted @ 2014-04-05 23:09 Awy 阅读(100) 评论(0) 推荐(0) 编辑
摘要: 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].解析:这道题没有什么太过复杂的数据结构的思想,就是顺时针打印矩阵。我们来分析循环结束条件。假设这个矩阵的的行数rows,列数是columns。打印第一圈的左上角的坐标(1,1 阅读全文
posted @ 2014-04-05 21:52 Awy 阅读(171) 评论(0) 推荐(0) 编辑
摘要: Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.思路:这是N-Queens的变形,只需要输出所有八皇后的个数。思路和N-Queens差不多,只是不用存八皇后的排列了。所以在判断条件下,如果为真,则直接加1.如果不是,则不加。class Solution {public: bool check(vector &data,int n) { for(int i=0;i &data... 阅读全文
posted @ 2014-04-04 12:15 Awy 阅读(166) 评论(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 distinct solutions to then-queens puzzle.Each solution contains a distinct board configuration of then-queens' placement, where'Q'and'.& 阅读全文
posted @ 2014-04-04 11:50 Awy 阅读(195) 评论(0) 推荐(0) 编辑
摘要: Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]思路:本题就是一个组合问题,举个例子就会明白了。例如n=4,k=2,则先将1放入一数组中,然后step记为1,然后循环遍历2加入数组,step记为2,依次加入,等step==k时停止,最后将这个数组加入到vector >中。如果没到达4,则pop最后... 阅读全文
posted @ 2014-04-02 16:15 Awy 阅读(119) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Givennwill always be valid.Try to do this in one pass.思路 阅读全文
posted @ 2014-04-02 15:27 Awy 阅读(271) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.思路:解题思路和Search in Rotated Sorted Array差不多,但是出现了另一种情况有重复元素出现,头尾指针各向中间移一步。然后在进行判断。class Solution 阅读全文
posted @ 2014-04-01 09:42 Awy 阅读(128) 评论(0) 推荐(0) 编辑
摘要: 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.解题思路:这题旋转排序数组,查找目标值并返回其位置。Rotate 阅读全文
posted @ 2014-04-01 08:40 Awy 阅读(127) 评论(0) 推荐(0) 编辑
摘要: Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(k) extra space?思路:这道题杨辉三角的一种特殊情况,可以使用从后往前遍历,一次覆盖上一行的内容,主要公式result[i]=result[i]+result[i-1]。等号左边的result[i]表示新的元素覆盖result[i]旧的值,等号右边的result[i]原来的值,r 阅读全文
posted @ 2014-03-29 00:24 Awy 阅读(120) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].思路:这道题与Remove Duplicates from Sorted Array思路差不多吧,就是在记录相等元素的时候有些变化,当相等元素超过两个的时候直接跳过,后面的数不作为记录。class So 阅读全文
posted @ 2014-03-28 23:33 Awy 阅读(105) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]... 阅读全文
posted @ 2014-03-27 23:15 Awy 阅读(152) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
posted @ 2014-03-27 22:49 Awy 阅读(140) 评论(0) 推荐(0) 编辑
摘要: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m+n) space, but still not the best solution.Could you de 阅读全文
posted @ 2014-03-26 15:04 Awy 阅读(133) 评论(0) 推荐(0) 编辑
摘要: Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, 3, ... 阅读全文
posted @ 2014-03-26 12:33 Awy 阅读(124) 评论(0) 推荐(0) 编辑
摘要: Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.思路:这道题就是一个大数加法。从右往左,最低位加上1,然后判断result[n-1]是不是大于9,即取其除数向前进位。class Solution {public: vector plusOne(vector &digits) { ve... 阅读全文
posted @ 2014-03-25 16:48 Awy 阅读(152) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].No... 阅读全文
posted @ 2014-03-25 11:59 Awy 阅读(193) 评论(0) 推荐(0) 编辑