摘要:
递归法 复杂度 时间O(n) 空间O(h) 思路 因为要找最长的连续路径,我们在遍历树的时候需要两个信息,一是目前连起来的路径(curLen)有多长(以当前节点为终点),二是目前路径的上一个节点的值(preVal)。 每一次递归,先检查当前节点是否比上一节点大1, 是,curLen++, 否curL
阅读全文
posted @ 2015-12-29 01:11
neverlandly
阅读(361)
推荐(0)
摘要:
A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the h...
阅读全文
posted @ 2015-12-28 12:06
neverlandly
阅读(286)
推荐(0)
摘要:
最大最小堆 复杂度 时间 O(logN) insert, O(1) query, 空间 O(N) 思路 维护一个最大堆,一个最小堆。最大堆存的是到目前为止较小的那一半数,最小堆存的是到目前为止较大的那一半数,这样中位数只有可能是堆顶或者堆顶两个数的均值。而维护两个堆的技巧在于判断堆顶数和新来的数的大
阅读全文
posted @ 2015-12-28 07:45
neverlandly
阅读(280)
推荐(0)
摘要:
You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take tu
阅读全文
posted @ 2015-12-28 06:05
neverlandly
阅读(535)
推荐(0)
摘要:
You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take tu...
阅读全文
posted @ 2015-12-28 04:17
neverlandly
阅读(303)
推荐(0)
摘要:
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 ston...
阅读全文
posted @ 2015-12-28 02:17
neverlandly
阅读(226)
推荐(0)
摘要:
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter...
阅读全文
posted @ 2015-12-28 01:45
neverlandly
阅读(342)
推荐(0)
摘要:
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a lette
阅读全文
posted @ 2015-12-28 00:54
neverlandly
阅读(327)
推荐(0)
摘要:
编解码法 复杂度 时间 O(NN) 空间 O(1) 思路 最简单的方法是再建一个矩阵保存,不过当inplace解时,如果我们直接根据每个点周围的存活数量来修改当前值,由于矩阵是顺序遍历的,这样会影响到下一个点的计算。如何在修改值的同时又保证下一个点的计算不会被影响呢?实际上我们只要将值稍作编码就行了
阅读全文
posted @ 2015-12-27 13:18
neverlandly
阅读(502)
推荐(0)
摘要:
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the...
阅读全文
posted @ 2015-12-27 12:05
neverlandly
阅读(573)
推荐(0)
摘要:
An abbreviation of a word follows the form . Below are some examples of word abbreviations:a) it --> it (no abbreviation) ...
阅读全文
posted @ 2015-12-27 07:14
neverlandly
阅读(382)
推荐(0)
摘要:
参考了这个总结,非常详细:http://segmentfault.com/a/1190000003817671 最好方法: 映射找环法 复杂度 时间 O(N) 空间 O(1) 思路 假设数组中没有重复,那我们可以做到这么一点,就是将数组的下标和1到n每一个数一对一的映射起来。比如数组是213,则映射
阅读全文
posted @ 2015-12-26 13:56
neverlandly
阅读(321)
推荐(0)
摘要:
Push all gates into queue first. Then for each gate update its neighbor cells who is empty and push them to the queue. Repeating above steps until the
阅读全文
posted @ 2015-12-26 13:14
neverlandly
阅读(396)
推荐(0)
摘要:
第二遍做法: 双指针压缩法:O(N), 空间:O(1) 实际上就是将所有的非0数向前尽可能的压缩,最后把没压缩的那部分全置0就行了。比如103040,先压缩成134,剩余的3为全置为0。过程中需要一个指针记录压缩到的位置。 Q:如果要把所有的0放在前面而不是后面呢?A:同样的解题思路,但是是从后向前
阅读全文
posted @ 2015-12-26 09:51
neverlandly
阅读(358)
推荐(0)
摘要:
My thinking is: use a double cache: long cache, when no number is cached, cache = Long.MIN_VALUE. When i call next(), it will firstly check if there's
阅读全文
posted @ 2015-12-26 09:42
neverlandly
阅读(470)
推荐(0)
摘要:
网上看到更好的方法:https://leetcode.com/discuss/77805/java-5ms-short-code-with-explanations The idea is to compare root's value with p's value if root is not n
阅读全文
posted @ 2015-12-26 08:49
neverlandly
阅读(391)
推荐(0)
摘要:
Best Solution: O(N) This solution also works for K vectors Use a queue to store the iterators in different vectors. Every time we call next(), we pop
阅读全文
posted @ 2015-12-26 08:11
neverlandly
阅读(349)
推荐(0)
摘要:
Given an unsorted array nums, reorder it in-place such that nums[0] = nums[2] = nums[i - 1]如果i是偶数,nums[i] nums[i - 1], 则交换以后肯定有nums[i] =nums[i-1])) {...
阅读全文
posted @ 2015-12-26 07:38
neverlandly
阅读(300)
推荐(0)
摘要:
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, ...
阅读全文
posted @ 2015-12-26 04:57
neverlandly
阅读(260)
推荐(0)
摘要:
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality c...
阅读全文
posted @ 2015-12-26 02:43
neverlandly
阅读(241)
推荐(0)
摘要:
最佳做法:O(N)time, O(1)space The first pass is to pick out the candidate. If candidate knows i, then switch candidate. The second pass is to check whether
阅读全文
posted @ 2015-12-26 01:46
neverlandly
阅读(378)
推荐(0)
摘要:
Career Cup 150 Pg 442 Think of Convert(19,323,984) = Process(19) + "million" + Process(323) + "thousand" + Process(984) + "" The Process is a process
阅读全文
posted @ 2015-12-26 00:08
neverlandly
阅读(376)
推荐(0)
摘要:
There is a fence with n posts, each post can be painted with one of the k colors.You have to paint all the posts such that no more than two adjacent f...
阅读全文
posted @ 2015-12-25 05:59
neverlandly
阅读(787)
推荐(0)
摘要:
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? Hint: Expected runtime complexity
阅读全文
posted @ 2015-12-25 03:10
neverlandly
阅读(431)
推荐(0)
摘要:
Hint: Sort first then scan: O(NlogN) time, O(1)space use another array: O(N) time, O(N) space(有点像counting sort) 我们额外使用一个大小为N+1的数组stats。stats[i]表示有多少文章
阅读全文
posted @ 2015-12-25 01:16
neverlandly
阅读(342)
推荐(0)
摘要:
Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.Note:Given target value is a floating...
阅读全文
posted @ 2015-12-24 13:14
neverlandly
阅读(324)
推荐(0)
摘要:
If I choose / as spliter, how to ensure the other / wouldn't be seen as spliter? The idea is to store length of the str This one will be encoded as "6
阅读全文
posted @ 2015-12-24 12:27
neverlandly
阅读(452)
推荐(0)
摘要:
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.Note:Given target value is a floating...
阅读全文
posted @ 2015-12-24 09:47
neverlandly
阅读(294)
推荐(0)
摘要:
Best Solution: Bit manipulation The basic idea is to use XOR operation. We all know that a^b^b =a, which means two xor operations with the same number
阅读全文
posted @ 2015-12-24 08:50
neverlandly
阅读(276)
推荐(0)
摘要:
This problem can be solved by using union find, reference this blog:https://segmentfault.com/a/1190000003791051 复杂度 时间 O(N^M) 空间 O(N) 思路 判断输入的边是否能构成一个
阅读全文
posted @ 2015-12-24 02:58
neverlandly
阅读(1008)
推荐(0)
摘要:
There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. Yo
阅读全文
posted @ 2015-12-23 13:31
neverlandly
阅读(989)
推荐(0)
摘要:
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form...
阅读全文
posted @ 2015-12-23 12:53
neverlandly
阅读(343)
推荐(0)
摘要:
Given a string, determine if a permutation of the string could form a palindrome.For example,"code" -> False, "aab" -> True, "carerac" -> True.Hint:Co...
阅读全文
posted @ 2015-12-23 11:32
neverlandly
阅读(232)
推荐(0)
摘要:
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For exa
阅读全文
posted @ 2015-12-23 06:59
neverlandly
阅读(230)
推荐(0)
摘要:
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6...
阅读全文
posted @ 2015-12-23 06:48
neverlandly
阅读(276)
推荐(0)
摘要:
Lintcode 也有这道题 Single Number III example: 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 如例所示,最后两个数8,
阅读全文
posted @ 2015-12-23 05:43
neverlandly
阅读(230)
推荐(0)
摘要:
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 =2; i--) { 6 //if (i!=nums.length-1 && (n...
阅读全文
posted @ 2015-12-23 04:47
neverlandly
阅读(320)
推荐(0)
摘要:
Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a function that takes an integer n and return all possib
阅读全文
posted @ 2015-12-23 02:33
neverlandly
阅读(472)
推荐(0)
摘要:
用到了Divide and Conquer, 跟 Leetcode: Unique Binary Search Trees II 很像 在input string里遍历各个operator, 依据每个operator分成左右子串,左右子串做递归返回所有可能的results,然后全排列。 注意很巧妙的
阅读全文
posted @ 2015-12-23 00:43
neverlandly
阅读(329)
推荐(0)
摘要:
我的方法:注意最方便的获取一个数每一位的方法,是把它转化为String先,通过String.valueOf(int c) O(1): http://my.oschina.net/Tsybius2014/blog/497645 另一个方法比较简单,可以举例说明一下。假设输入的数字是一个5位数字num,
阅读全文
posted @ 2015-12-22 13:28
neverlandly
阅读(240)
推荐(0)