摘要: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 maximu...
阅读全文
摘要: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 =...
阅读全文
摘要:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"tw...
阅读全文
摘要: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 ord...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要:这道题是个细节实现题,只要把valid sudoku满足的三个条件判断一下即可。valid sudoku需满足下列三个条件:1)每一行数字1~9有且只出现一次。2)每一列数字1~9有且只出现一次。3)对于每个3*3的sub-box(用i=3、6,j=3、6两条线划分,总共9个sub-box)数字1~...
阅读全文
摘要: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 ...
阅读全文
摘要:Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must close in the correct ...
阅读全文
摘要: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.分析...
阅读全文
摘要:Determine whether an integer is a palindrome. Do this without extra space.按照palindrome的定义,负数一定不是palindrome number。而且palindrome number肯定是沿着中轴(可能为一个数或者两...
阅读全文
摘要: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 rep...
阅读全文
摘要:这道题可以用双指针的方法解,将两个指针p,q的距离保持在n-1,然后移动q到List的最后一个元素,那么此时p指向的便是the Nth node from the end。要删除这个node,要分两种情况,一种是该node为head,另一种该node是中间node。对于第一种情况,可以简单的采用以下...
阅读全文
摘要:Question: Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.要产生全部的combinations,我们需要一种有序的方式产生combination以达到既产生全部又没有重复的...
阅读全文
摘要:要搜索的对象是一个rotated sorted array,所以从直觉上时间复杂度应该不会超过O(logn)。解决这个题目的另一个思路就是先把pivot找出来,在这里我把pivot定义为数组中最小的那个数。所以下面要解决的便是能不能在O(logn)的时间找到pivot。沿着这个思路,然后综合rota...
阅读全文
摘要:这道题实质是用螺旋线的形式遍历二维数组,而且这种遍历方式有很强的规律性,从最外层开始一直到最里层,而且每层都按顺时针的方向遍历。根据这种规律性,我们可以用两层循环,在遍历的同时对二维数组对应元素重新赋值,第一层循环是层数遍历,第二层循环是每一层元素遍历(值得注意的一点是顺时针遍历)。class So...
阅读全文
摘要:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.分析:跟N-Queens一样,DFS+Backtrac...
阅读全文
摘要:Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinc...
阅读全文
摘要:最近做Leetcode上的题目,感觉有些挺有趣的,比如这个Single Number题目:Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?如果没有时间和空间复杂度的限制,这道题并没有什么难处,大可以扫一遍数组,然后用hashmap记录每个数值出
阅读全文