摘要:
这道题是我微软onsite时遇到的一道题,没做过遇到这道题确实有点难一下子理得很清楚(我当时这道题应该做的不好,从most significant digit做,而且忘了n要-1)。这道题说白了其实就是十进制转换26进制,而且是从1开始的1十进制的转换 短除法: Short Division 本质是
阅读全文
posted @ 2014-12-23 12:43
neverlandly
阅读(2625)
推荐(0)
摘要:
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index...
阅读全文
posted @ 2014-12-23 08:28
neverlandly
阅读(751)
推荐(0)
摘要:
第一想法是用HashSet<ListNode>, A list先遍历,存HashSet,然后B list遍历,发现ListNode存在就返回。但是这个方法不满足O(1)memory的要求。 再想了一会儿,略微受了点提醒,发现可以利用这个O(n) time做文章。这个条件方便我们scan list几次
阅读全文
posted @ 2014-12-23 04:50
neverlandly
阅读(422)
推荐(0)
摘要:
Leetcode的官方答案给的解答很好,我的方法是HashMap. 除了brute force和sorting常见方法以外,还有几个方法,思路都还不错,1是我的方法,我觉得2、4、5都是不错的思路。 位操作法 复杂度 时间 O(N) 空间 O(1) 思路 假设一个数是最多只有32位的二进制数,那么我
阅读全文
posted @ 2014-12-23 04:07
neverlandly
阅读(6192)
推荐(0)
摘要:
这里列举的是一些我平时碰到的一些Java Grammar,日积月累。Class Variable vs Instance Variable:Instance variablesInstance variable is the variable declared inside a class, but...
阅读全文
posted @ 2014-11-22 11:58
neverlandly
阅读(513)
推荐(0)
摘要:
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?前面那道题:Find ...
阅读全文
posted @ 2014-11-22 11:38
neverlandly
阅读(348)
推荐(0)
摘要:
这道题跟Search in Rotated Sorted Array这道题很像,区别在于这道题不是找一个目标值,而是找最小值。所以与那道题的思路类似又有区别。类似在于同样是用binary search找目标区域,通过左边界和中间的大小关系来得到左边或者右边有序。区别在于这一次迭代不会存在找到targ
阅读全文
posted @ 2014-11-22 10:40
neverlandly
阅读(461)
推荐(0)
摘要:
这是一道关于栈的题目,整体来说比较简单。我最开始想的时候想的太简单,以为使用一个栈st, 再维护一个最小值变量就好了。突然发现pop()操作之后需要更新这个最小值,那就需要知道第二小的值,这个第二小的值怎么找呢?于是乎我想到了使用另外一个栈minst专门来存储最小值。push()操作的时候每当x小于
阅读全文
posted @ 2014-11-22 05:11
neverlandly
阅读(586)
推荐(0)
摘要:
如题,谷歌两轮背靠背电面。两轮都是废话不多说直奔coding,虽然第一轮的中国大哥还是花了一点点时间了解了一下我的背景、毕业时间、research方向。说好的research面呢?中国大哥出的题:Given a set of integers, print out all the subsetsFo...
阅读全文
posted @ 2014-10-28 12:15
neverlandly
阅读(3195)
推荐(1)
摘要:
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],th...
阅读全文
posted @ 2014-10-14 08:23
neverlandly
阅读(552)
推荐(0)
摘要:
难度:70. RPN中文名字叫做逆波兰表示法,它的好处维基百科说了,就是不需要括号来表示运算的先后,直接根据式子本身就可以求解。解题思路就是维护一个栈,遇到数字就入栈,遇到操作符就两次出栈得到栈顶的两个操作数,运用操作符进行运算以后,再把结果入栈。直到式子结束,此时栈中唯一一个元素便是结果。 以上代
阅读全文
posted @ 2014-10-14 07:52
neverlandly
阅读(492)
推荐(0)
摘要:
Leetcode里关于图的题其实并不多,这道题就是其中之一。DFS深度优先搜索和BFS广度优先搜索都可以做,遍历完原图的所有节点。这道题的难点在于neighbour关系的拷贝:原图中某一个点跟一些点具有neighbour关系,那么该点的拷贝也要与上述那些点的拷贝具有neighbour关系。那么,就需
阅读全文
posted @ 2014-10-14 07:08
neverlandly
阅读(409)
推荐(0)
摘要:
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 shoul...
阅读全文
posted @ 2014-10-14 05:31
neverlandly
阅读(431)
推荐(0)
摘要:
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the p...
阅读全文
posted @ 2014-10-13 12:36
neverlandly
阅读(422)
推荐(0)
摘要:
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.For example,Given n = 3,You should return the followin...
阅读全文
posted @ 2014-10-13 08:34
neverlandly
阅读(331)
推荐(0)
摘要:
难度:100,情况分得太多太细,跟Wildcard Matching很像又比那个难多了,refer to: https://discuss.leetcode.com/topic/40371/easy-dp-java-solution-with-detailed-explanation Here ar
阅读全文
posted @ 2014-10-13 06:58
neverlandly
阅读(851)
推荐(0)
摘要:
难度:90. 非常不错的一道一维DP题目,参考了网上的做法。 看到这种求数量的,我们很容易想到动态规划来存储前面信息,然后迭代得到最后结果。我们维护的量res[i]是表示前i个数字有多少种解析的方式,接下来来想想递归式,有两种方式:第一种新加进来的数字不然就是自己比较表示一个字符,那么解析的方式有r
阅读全文
posted @ 2014-10-12 13:15
neverlandly
阅读(414)
推荐(0)
摘要:
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 ...
阅读全文
posted @ 2014-10-12 07:12
neverlandly
阅读(450)
推荐(0)
摘要:
可以按照3Sum的思路来做,并以此类推,KSum的复杂度就是O(N^(k-1)). 在3Sum外面再套一层循环,相当于N次求3Sum 若还有时间,可以参考https://discuss.leetcode.com/topic/29585/7ms-java-code-win-over-100 有更多优化
阅读全文
posted @ 2014-10-12 04:03
neverlandly
阅读(381)
推荐(0)
摘要:
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Cl...
阅读全文
posted @ 2014-10-11 23:56
neverlandly
阅读(352)
推荐(0)
摘要:
Best Solution: only scan one direction, If the number x is the start of a streak (i.e., x-1 is not in the set), then test y = x+1, x+2, x+3, ... and s
阅读全文
posted @ 2014-10-11 12:38
neverlandly
阅读(496)
推荐(0)
摘要:
难度:87,这道题跟 Rotate Image 很相似,都是需要把矩阵分割成层来处理,每一层都是按:1. 正上方;2. 正右方;3. 正下方;4. 正左方这种顺序添加元素到结果集合。实现中要注意两个细节,一个是因为题目中没有说明矩阵是不是方阵,因此要先判断一下行数和列数来确定螺旋的层数,mina(行
阅读全文
posted @ 2014-10-11 07:51
neverlandly
阅读(447)
推荐(0)
摘要:
难度:96.这道题看似一个关于字符串操作的题目,其实要解决这个问题得用图的方法。我们先给题目进行图的映射,顶点则是每个字符串,然后两个字符串如果相差一个字符则我们进行连边。接下来看看这个方法的优势,注意到我们的字符集只有小写字母,而且字符串长度固定,假设是L。那么可以注意到每一个字符可以对应的边则有
阅读全文
posted @ 2014-10-11 05:39
neverlandly
阅读(425)
推荐(0)
摘要:
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" 难度:85,虽然不难,但是里
阅读全文
posted @ 2014-10-10 13:20
neverlandly
阅读(335)
推荐(0)
摘要:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.Brute Force的做法,N个点两两可以构成N(N-1)/2条线,我们可以找这N(N-1)/2条线...
阅读全文
posted @ 2014-10-10 08:32
neverlandly
阅读(408)
推荐(0)
摘要:
难度:92。这个题目用到的方法是图形学中的一个常用方法:Flood fill算法,其实就是从一个点出发对周围区域进行目标颜色的填充。背后的思想就是把一个矩阵看成一个图的结构,每个点看成结点,而边则是他上下左右的相邻点,然后进行一次广度或者深度优先搜索。 这道题首先四个边缘上的‘O’点都不是被surr
阅读全文
posted @ 2014-10-10 04:01
neverlandly
阅读(555)
推荐(0)
摘要:
这道题跟3Sum很像,区别就是要维护一个最小的diff,求出和目标最近的三个和。brute force时间复杂度为O(n^3),优化的解法是使用排序之后夹逼的方法,总的时间复杂度为O(n^2+nlogn)=(n^2),空间复杂度是O(n)。 第二遍做法: 第一遍做法:
阅读全文
posted @ 2014-10-09 04:47
neverlandly
阅读(308)
推荐(0)
摘要:
Best 做法:不用Extra Space, Time Complexity still O(N^2) Good syntax: // creating Arrays of String type String a[] = new String[] { "A", "B", "C", "D" }; /
阅读全文
posted @ 2014-10-08 13:16
neverlandly
阅读(454)
推荐(0)
摘要:
想法是建立一个2维的boolean数组,booleen[][] check = new boolean[s.length()+1][p.length()+1],注意最好比string的length大一行和一列,来包括第0行和第0列的情况。这样初始化比较方便。check[m][n]表示s的前m个元素是
阅读全文
posted @ 2014-10-08 10:36
neverlandly
阅读(643)
推荐(0)
摘要:
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have t...
阅读全文
posted @ 2014-10-08 06:10
neverlandly
阅读(742)
推荐(0)
摘要:
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complet...
阅读全文
posted @ 2014-10-06 13:35
neverlandly
阅读(1408)
推荐(0)
摘要:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each numb...
阅读全文
posted @ 2014-10-06 12:10
neverlandly
阅读(398)
推荐(0)
摘要:
这道题的算法第一次可能有点难想,但是做过一次后就知道了 Shi Li Analysis(会好理解很多): Starting from the last position, the first time we find that num[i]<num[i+1], we stop. The positi
阅读全文
posted @ 2014-10-06 08:08
neverlandly
阅读(628)
推荐(0)
摘要:
这道题跟Insert Intervals这道题很像,最开头要先对Intervals里面的Interval排序一下,用到了java的Collections.sort(List<Interval> list, Comparator<? super Interval> c)。 排序以Interval的st
阅读全文
posted @ 2014-10-06 06:36
neverlandly
阅读(593)
推荐(1)
摘要:
难度:90 String问题里面有很多不好做的,动不动就DP什么的,参考了一些资料http://blog.csdn.net/fightforyourdream/article/details/17373203 For example,S = “ADOBECODEBANC”T = “ABC”Minim
阅读全文
posted @ 2014-10-06 01:45
neverlandly
阅读(593)
推荐(0)
摘要:
If output is List: If output is array: 24-27 line can also be
阅读全文
posted @ 2014-10-05 12:33
neverlandly
阅读(425)
推荐(0)
摘要:
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representatio...
阅读全文
posted @ 2014-10-05 01:52
neverlandly
阅读(388)
推荐(1)
摘要:
上来随意交谈了一小会儿,开了点小玩笑,chat了一些关于他们recruter行程的话题,缓和了一下气氛。进入正题,问了做的research的方向,我说是DLT,然后大概给他讲解了一下具体是什么, 跟平行计算很像,举了一个例子:矩阵乘法如何划分使并行效率最高。他表示理解。然后他又问我有没有过end t...
阅读全文
posted @ 2014-09-27 07:07
neverlandly
阅读(527)
推荐(1)
摘要:
上来先问了一个系统设计的问题,一个front end, 一个back end。 front end有很多UI,一个UI对10个多customers,back end有许多processor,或者processor有多个进程、线程。问应该怎么设计这个并行分布运算的系统,才能让独立的任务得到优化。完全那...
阅读全文
posted @ 2014-09-24 13:21
neverlandly
阅读(872)
推荐(0)
摘要:
思路:这道题要观察,举个例子,1 2 * * 3 * 4 5 * * 6 7 * 8 * *, 用Stack,先序遍历,遇到数字就入栈,如果遇到 * *,说明栈顶节点是叶子节点,一条根到叶子的路径这时候就存在于栈之中,只要计算栈的size(),就知道当前这条路径的深度,树的height就是这些深度的...
阅读全文
posted @ 2014-09-23 22:53
neverlandly
阅读(920)
推荐(0)