10 2014 档案
面经:Google两轮背靠背
摘要:如题,谷歌两轮背靠背电面。两轮都是废话不多说直奔coding,虽然第一轮的中国大哥还是花了一点点时间了解了一下我的背景、毕业时间、research方向。说好的research面呢?中国大哥出的题:Given a set of integers, print out all the subsetsFo...
阅读全文
Leetcode: Maximum Product Subarray
摘要: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...
阅读全文
Leetcode: Evaluate Reverse Polish Notation
摘要:难度:70. RPN中文名字叫做逆波兰表示法,它的好处维基百科说了,就是不需要括号来表示运算的先后,直接根据式子本身就可以求解。解题思路就是维护一个栈,遇到数字就入栈,遇到操作符就两次出栈得到栈顶的两个操作数,运用操作符进行运算以后,再把结果入栈。直到式子结束,此时栈中唯一一个元素便是结果。 以上代
阅读全文
Leetcode: Clone Graph
摘要:Leetcode里关于图的题其实并不多,这道题就是其中之一。DFS深度优先搜索和BFS广度优先搜索都可以做,遍历完原图的所有节点。这道题的难点在于neighbour关系的拷贝:原图中某一个点跟一些点具有neighbour关系,那么该点的拷贝也要与上述那些点的拷贝具有neighbour关系。那么,就需
阅读全文
Leetcode: Text Justification
摘要: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...
阅读全文
Leetcode: Valid Number
摘要: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...
阅读全文
Leetcode: Spiral Matrix II
摘要: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...
阅读全文
Leetcode: Regular Expression Matching
摘要:难度:100,情况分得太多太细,跟Wildcard Matching很像又比那个难多了,refer to: https://discuss.leetcode.com/topic/40371/easy-dp-java-solution-with-detailed-explanation Here ar
阅读全文
Leetcode: Decode Ways
摘要:难度:90. 非常不错的一道一维DP题目,参考了网上的做法。 看到这种求数量的,我们很容易想到动态规划来存储前面信息,然后迭代得到最后结果。我们维护的量res[i]是表示前i个数字有多少种解析的方式,接下来来想想递归式,有两种方式:第一种新加进来的数字不然就是自己比较表示一个字符,那么解析的方式有r
阅读全文
Leetcode: Permutation Sequence
摘要: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 ...
阅读全文
Leetcode: 4Sum
摘要:可以按照3Sum的思路来做,并以此类推,KSum的复杂度就是O(N^(k-1)). 在3Sum外面再套一层循环,相当于N次求3Sum 若还有时间,可以参考https://discuss.leetcode.com/topic/29585/7ms-java-code-win-over-100 有更多优化
阅读全文
Leetcode: Reverse Words in a String
摘要: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...
阅读全文
Leetcode: Longest Consecutive Sequence
摘要: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
阅读全文
Leetcode: Spiral Matrix
摘要:难度:87,这道题跟 Rotate Image 很相似,都是需要把矩阵分割成层来处理,每一层都是按:1. 正上方;2. 正右方;3. 正下方;4. 正左方这种顺序添加元素到结果集合。实现中要注意两个细节,一个是因为题目中没有说明矩阵是不是方阵,因此要先判断一下行数和列数来确定螺旋的层数,mina(行
阅读全文
Leetcode: Word Ladder
摘要:难度:96.这道题看似一个关于字符串操作的题目,其实要解决这个问题得用图的方法。我们先给题目进行图的映射,顶点则是每个字符串,然后两个字符串如果相差一个字符则我们进行连边。接下来看看这个方法的优势,注意到我们的字符集只有小写字母,而且字符串长度固定,假设是L。那么可以注意到每一个字符可以对应的边则有
阅读全文
Leetcode: Simplify Path
摘要:Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" 难度:85,虽然不难,但是里
阅读全文
Leetcode: Max Points on a line
摘要: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条线...
阅读全文
Leetcode: Surrounded Regions
摘要:难度:92。这个题目用到的方法是图形学中的一个常用方法:Flood fill算法,其实就是从一个点出发对周围区域进行目标颜色的填充。背后的思想就是把一个矩阵看成一个图的结构,每个点看成结点,而边则是他上下左右的相邻点,然后进行一次广度或者深度优先搜索。 这道题首先四个边缘上的‘O’点都不是被surr
阅读全文
Leetcode: 3Sum Closest
摘要:这道题跟3Sum很像,区别就是要维护一个最小的diff,求出和目标最近的三个和。brute force时间复杂度为O(n^3),优化的解法是使用排序之后夹逼的方法,总的时间复杂度为O(n^2+nlogn)=(n^2),空间复杂度是O(n)。 第二遍做法: 第一遍做法:
阅读全文
Leetcode: 3Sum
摘要:Best 做法:不用Extra Space, Time Complexity still O(N^2) Good syntax: // creating Arrays of String type String a[] = new String[] { "A", "B", "C", "D" }; /
阅读全文
Leetcode: Wildcard Matching
摘要:想法是建立一个2维的boolean数组,booleen[][] check = new boolean[s.length()+1][p.length()+1],注意最好比string的length大一行和一列,来包括第0行和第0列的情况。这样初始化比较方便。check[m][n]表示s的前m个元素是
阅读全文
Leetcode: Edit Distance
摘要: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...
阅读全文
Leetcode: Best Time to Buy and Sell Stock III
摘要: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...
阅读全文
Leetcode: Combination Sum II
摘要: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...
阅读全文
Leetcode: Next Permutation
摘要:这道题的算法第一次可能有点难想,但是做过一次后就知道了 Shi Li Analysis(会好理解很多): Starting from the last position, the first time we find that num[i]<num[i+1], we stop. The positi
阅读全文
Leetcode: Merge Intervals
摘要:这道题跟Insert Intervals这道题很像,最开头要先对Intervals里面的Interval排序一下,用到了java的Collections.sort(List<Interval> list, Comparator<? super Interval> c)。 排序以Interval的st
阅读全文
Leetcode: Minimum Window Substring
摘要:难度:90 String问题里面有很多不好做的,动不动就DP什么的,参考了一些资料http://blog.csdn.net/fightforyourdream/article/details/17373203 For example,S = “ADOBECODEBANC”T = “ABC”Minim
阅读全文
Leetcode: Scramble String
摘要: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...
阅读全文