跳至主要内容

博文

新鲜出炉,Amazon SDE 面经(电面+Onsite)

刚刚参加完Amazon的面试,来写一下自己的面试过程。我申请的是SDE-1的职位。面试流程是2轮电面,4轮Onsite. 一开始是电面。 第一轮 电面 1 翻转字符串 reverse words in a string。 LintCode原题: http://www.lintcode.com/en/problem/reverse-words-in-a-string/ 参考答案: http://www.jiuzhang.com/solutions/reverse-words-in-a-string/ follow-up: 翻转的时候如何处理空格 划分数组。 LintCode原题: http://www.lintcode.com/zh-cn/problem/partition-array/ 参考答案: http://www.jiuzhang.com/solutions/partition-array/ 第二轮 电面2 链表求和。要求O(n)的时间复杂度。 LintCode原题: http://www.lintcode.com/zh-cn/problem/add-two-numbers/ 参考答案: http://www.jiuzhang.com/solutions/reverse-words-in-a-string/ Find all ancestors of LCA. 给出两个节点,打印出两个节点的所有公共祖先节点,算法也就是找出最近公共祖先(LCA),然后打印root到LCA的所有节点。 LintCode原题: http://www.lintcode.com/zh-cn/problem/lowest-common-ancestor/ 参考答案: http://www.jiuzhang.com/solutions/lowest-common-ancestor/ follow-up: 问了一些关于堆和宽度优先搜索(heap & BST)的知识点。像是什么时候用宽度优先搜索,这个在《九章算法班》有讲。 电面结束后几天,HR告诉我面试通过了,并安排我去参加Onsite. 第三轮 Onsite 1 面试官先是问了我一些简历上的问题,比如我简历上的项目经验,问的还算比较细。 接着问了1道算...
最新博文

Lintcode17 Subsets solution 题解

【题目描述】 Given a set of distinct integers, return all possible subsets. Notice:Elements in a subset must be in non-descending order;The solution set must not contain duplicate subsets. 给定一个含不同整数的集合,返回其所有的子集 注意:子集中的元素排列必须是非降序的,解集必须不包含重复的子集 【题目链接】 http://www.lintcode.com/en/problem/subsets/ 【题目解析】 子集类问题类似Combination,以输入数组[1, 2, 3]分析,根据题意,最终返回结果中子集类的元素应该按照升序排列,故首先需要对原数组进行排序。题目的第二点要求是子集不能重复,至此原题即转化为数学中的组合问题。我们首先尝试使用 DFS 进行求解,大致步骤如下: [1] -> [1, 2] -> [1, 2, 3] [2] -> [2, 3] [3] 将上述过程转化为代码即为对数组遍历,每一轮都保存之前的结果并将其依次加入到最终返回结果中。 【答案链接】 http://www.jiuzhang.com/solution/subsets/

Lintcode16 Permutations II solution 题解

【题目描述】 Given a list of numbers with duplicate number in it. Find all unique permutations. 给出一个具有重复数字的列表,找出列表所有不同的排列。 【题目链接】 http://www.lintcode.com/en/problem/permutations-ii/ 【题目解析】 跟 Permutations的解法一样,就是要考虑“去重”。先对数组进行排序,这样在DFS的时候,可以先判断前面的一个数是否和自己相等,相等的时候则前面的数必须使用了,自己才能使用,这样就不会产生重复的排列了。 与Permitations的code相比,只加了3行,Line 8,23,24。 【答案链接】 http://www.jiuzhang.com/solutions/permutations-ii/

Lintcode15 Permutations solution 题解

【题目描述】 Given a list of numbers, return all possible permutations. Notice:You can assume that there is no duplicate numbers in the list. 给定一个数字列表,返回其所有可能的排列。 注意:你可以假设没有重复数字。 【题目链接】 http://www.lintcode.com/en/problem/permutations/ 【题目解析】 遇到这种问题,很显然,第一个想法我们首先回去想到DFS,递归求解,对于数组中的每一个元素,找到以他为首节点的Permutations,这就要求在递归中,每次都要从数组的第一个元素开始遍历,这样,,就引入了另外一个问题,我们会对于同一元素访问多次,这就不是我们想要的答案了,所以我们引入了一个bool类型的数组,用来记录哪个元素被遍历了(通过下标找出对应).在对于每一个Permutation进行求解中,如果访问了这个元素,我们将它对应下表的bool数组中的值置为true,访问结束后,我们再置为false. 时间复杂度分析: 这道题同Combination,所以对于这道题的解答,时间复杂度同样是O(n!) 【答案链接】 https://www.jiuzhang.com/solutions/permutations/

Lintcode14 First Position of Target Solution 题解

【题目描述】 For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity. If the target number does not exist in the array, return -1. 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。 【题目链接】 http://www.lintcode.com/en/problem/first-position-of-target/ 【题目解析】 这题目要求O(logn)的复杂度,又是sorted array,话不多说我们来搞二分法。注意要求first position of target所以当(array[mid] == target)的时候,操作是end = mid。 二分查找到第一个target后使用逐个向前查询第一个target 全程使用二分查找 【答案链接】 http://www.jiuzhang.com/solutions/first-position-of-target/

Lintcode13 strStr solution 题解

【题目描述】 For a given source string and a target string, you should output the first index(from 0) of target string in source string. If target does not exist in source, just return -1. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。 【题目链接】 http://www.lintcode.com/en/problem/strstr/ 【题目解析】 对于字符串查找问题,可使用双重for循环解决,效率更高的则为KMP算法。 源码分析 1.边界检查:source和target有可能是空串。 2.边界检查之下标溢出:注意变量i的循环判断条件,如果是单纯的i < source.length()则在后面的source.charAt(i + j)时有可能溢出。 3.代码风格:(1)运算符==两边应加空格;(2)变量名不要起s1``s2这类,要有意义,如target``source;(3)即使if语句中只有一句话也要加大括号,即{return -1;};(4)Java 代码的大括号一般在同一行右边,C++ 代码的大括号一般另起一行;(5)int i, j;声明前有一行空格,是好的代码风格。 4.不要在for的条件中声明i,j,容易在循环外再使用时造成编译错误 【答案链接】 http://www.jiuzhang.com/solutions/strstr/

Lintcode11 Search Range in Binary Search Tree solution 题解

【题目描述】 Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. 给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点。找到树中所有值在 k1 到 k2 范围内的节点。即打印所有x (k1 <= x <= k2) 其中 x 是二叉查找树的中的节点值。返回所有升序的节点值。 【题目链接】 http://www.lintcode.com/en/problem/search-range-in-binary-search-tree/ 【题目解析】 中等偏易难度题,本题涉及到二叉查找树的按序输出,应马上联想到二叉树的中序遍历,对于二叉查找树而言,使用中序遍历即可得到有序元素。对每次访问的元素加以判断即可得最后结果,由于 OJ 上给的模板不适合递归处理,新建一个私有方法即可。 D & C。若root值比k1小,则只搜root右子树,若root值比k2大,则只搜root左子树,否则左右子树都搜,然后先加左子树返回的答案,再加root,最后加右子树返回的答案。 剪枝的判断条件容易出错,应将当前节点的值与k1和k2进行比较而不是其左子节点或右子节点的值。 【答案链接】 http://www.jiuzhang.com/solutions/search-range-in-binary-search-tree/