12 2017 档案

摘要:Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators a 阅读全文
posted @ 2017-12-28 05:11 apanda009 阅读(201) 评论(0) 推荐(0)
摘要:Use buckets to record the last position of digit 0 ~ 9 in this num. Loop through the num array from left to right. For each position, we check whether 阅读全文
posted @ 2017-12-20 13:04 apanda009 阅读(137) 评论(0) 推荐(0)
摘要:The sample input is very confusing when time t has mixed meaning of beginning of time t for start and end of time t for end We can increase all end ti 阅读全文
posted @ 2017-12-20 12:53 apanda009 阅读(170) 评论(0) 推荐(0)
摘要:注意这道题:Must be exactly one distance apart. Not the same. 另外FB面经有一道比较狠的这个题的变形: 这题就是one edit distance的变形题,难点在于给的Iterator,事先不知道两个file的长度,也不允许用extra space( 阅读全文
posted @ 2017-12-20 12:08 apanda009 阅读(156) 评论(0) 推荐(0)
摘要:小心这里是index triplets, 不是nums[i]数组元素的triplets, 所以3Sum那道题里面的跳过条件不用了 因为不关心每个index具体是什么,只关心个数,所以可以排序 阅读全文
posted @ 2017-12-20 10:36 apanda009 阅读(145) 评论(0) 推荐(0)
摘要:Time Complexity will be O(n) because the "start" and "end" points will only move from left to right once. 阅读全文
posted @ 2017-12-20 06:24 apanda009 阅读(137) 评论(0) 推荐(0)
摘要:我又想到了另一个idea,使用那个assumption可以达到O(1) 复杂度:首先判断isUp or not,仅以isUp为例, 假设A[x] 是最高点,则有:A[x] - A[i] = x - i. A[x] - A[j] = j - x由此可以解出 x = 1/ 2 * (A[j] - A[i 阅读全文
posted @ 2017-12-20 02:49 apanda009 阅读(163) 评论(0) 推荐(0)
摘要:A->B->C->D->E | V M | V N 有right和down的linkedlist,要求更新linkedlist变成A->M->N->B->C->D->E,注意M可以还有right,B也可以有down,其实就是递归输出。然后我做了一会才做出,之前有个大神的面经里发过,但我没仔细看很后悔。我昨晚发现这tmd不是inorder traversal么 // Class to ... 阅读全文
posted @ 2017-12-13 01:05 apanda009 阅读(261) 评论(0) 推荐(0)
摘要:写了一行,面试官打断我,让我再说一下思路,我又说了一下准备定义一个array,n+1的size,第一个存0,保存到当前值的所有之前numbers的和,然后比如query(0,3),就能用preSum[4]-preSum[0];他表示不懂...我又举了3个例子...面试官表示ok,但你不要写代码,先写 阅读全文
posted @ 2017-12-13 00:12 apanda009 阅读(168) 评论(0) 推荐(0)
摘要:The idea is to use two arrays len[n] and cnt[n] to record the maximum length of Increasing Subsequence and the coresponding number of these sequence w 阅读全文
posted @ 2017-12-13 00:00 apanda009 阅读(176) 评论(0) 推荐(0)
摘要:what if the list doesn't contains the result? 阅读全文
posted @ 2017-12-08 06:58 apanda009 阅读(189) 评论(0) 推荐(0)
摘要:Write a program that takes an integer and prints out all ways to multiply smaller integers that equal the original number, without repeating sets of factors. In other words, if your output contains 4... 阅读全文
posted @ 2017-12-07 23:19 apanda009 阅读(165) 评论(0) 推荐(0)
摘要:Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" O... 阅读全文
posted @ 2017-12-06 12:21 apanda009 阅读(211) 评论(0) 推荐(0)
摘要:Approach #2: Double Linked List + TreeMap [Accepted] Intuition Using structures like Array or Stack will never let us popMax quickly. We turn our atte 阅读全文
posted @ 2017-12-03 11:33 apanda009 阅读(353) 评论(0) 推荐(0)
摘要:Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5 Example 2: Input: ... 阅读全文
posted @ 2017-12-03 11:22 apanda009 阅读(146) 评论(0) 推荐(0)
摘要:array不是sorted的,里面可能有负数。解法也是从两边往中间搞,用两个数组left和right存原数组从左向右和从右向左的元素和。然后按index来比较left和right两个数组里的元素,有相等的就是balance point. 阅读全文
posted @ 2017-12-03 10:38 apanda009 阅读(178) 评论(0) 推荐(0)
摘要:Write a function to find the longest common prefix string amongst an array of strings.第二遍做法:时间复杂度应该是O(m*n),m表示字符串的最大长度,n表示字符串的个数,空间复杂度应该是O(m),即字符串的长度 public class Solution { public String longest... 阅读全文
posted @ 2017-12-03 10:27 apanda009 阅读(160) 评论(0) 推荐(0)
摘要:二分法逼近,然后找个误差值到时候跳出循环 阅读全文
posted @ 2017-12-03 10:12 apanda009 阅读(169) 评论(0) 推荐(0)
摘要:Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm 阅读全文
posted @ 2017-12-03 09:52 apanda009 阅读(230) 评论(0) 推荐(0)
摘要:Given a tree string expression in balanced parenthesis format:[A[B[C][D]][E][F]].Construct a tree and return the root of the tree. A / | \ B E F / \ C 阅读全文
posted @ 2017-12-03 09:28 apanda009 阅读(170) 评论(0) 推荐(0)
摘要:private static String convertBinary(int sum) { StringBuffer binary = new StringBuffer(); while (true) { binary.insert(0, sum % 2); sum = sum / 2... 阅读全文
posted @ 2017-12-03 08:53 apanda009 阅读(177) 评论(0) 推荐(0)
摘要:we start from the leftmost point (or point with minimum x coordinate value) and we keep wrapping points in counterclockwise direction. The big questio 阅读全文
posted @ 2017-12-03 08:37 apanda009 阅读(311) 评论(0) 推荐(0)
摘要:我就想了个递归, 还是没有区分掉一些重复的情况,worst case O(2^n)基本同暴力解 Map> allSubSet = new HashMap(); Set getAllPalidrome(String s, int x, int y){ int ind = x * s.length() + y; if(allSubSet.constainsKey(ind)) return a... 阅读全文
posted @ 2017-12-03 07:41 apanda009 阅读(143) 评论(0) 推荐(0)
摘要:每次移动3个pointer 里面最小的那个就好了。记录整个过程找最近的3个数。3个数之中median没意义,距离主要由最小和最大决定。基本就是这个思路。很快编完就过了编程阶段。 阅读全文
posted @ 2017-12-03 07:27 apanda009 阅读(247) 评论(0) 推荐(0)
摘要:Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest l 阅读全文
posted @ 2017-12-03 06:35 apanda009 阅读(125) 评论(0) 推荐(0)
摘要:code 不难,multi dimension array 的求 summation。 之前准备地里的LinkedIn高频题一个没碰到。。。 /** Suppose you are given a class that implements a k-dimensional array * interface and you want to perform an operation that ... 阅读全文
posted @ 2017-12-03 06:00 apanda009 阅读(392) 评论(0) 推荐(0)
摘要:public static int LPS(int[] a) { int[][] dp = new int[a.length][a.length]; for (int i = 0; i = 0; i--) { for (int j = i + 1; j < dp.length; j++) { if (a[i] == a[j]) { dp[i][j] = dp[... 阅读全文
posted @ 2017-12-03 04:07 apanda009 阅读(109) 评论(0) 推荐(0)
摘要:import java.util.*; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class BlockQueue { public int cap... 阅读全文
posted @ 2017-12-03 02:38 apanda009 阅读(184) 评论(0) 推荐(0)
摘要:import java.util.*; import java.util.concurrent.locks.*; public class H2O { private int HCount = 0; private int OCount = 0; private Lock lock = new ReentrantLock(); private Condition condH = lo... 阅读全文
posted @ 2017-12-03 02:37 apanda009 阅读(196) 评论(0) 推荐(0)
摘要:第一道是设计一个有选择性的iterator,类型T。面试官给的API里有一个自定selector,selector里有差不多叫boolean isOK(T t)方法。设计一个iterator每次调用hasNext(),返回接下来是否能取到合格的T对象;每次调用next(),返回下一个合格的T对象。我 阅读全文
posted @ 2017-12-03 02:20 apanda009 阅读(175) 评论(0) 推荐(0)
摘要:public class MidStack { static class Node{ T val; Node next = null; Node pre = null; public Node(T val){ this.val = val; } } private int size = 0; private Node head = null; privat... 阅读全文
posted @ 2017-12-03 01:53 apanda009 阅读(176) 评论(0) 推荐(0)
摘要:这道题要求in-place做法,不能使用extra space, 那么,做法跟Rotate Array那道题非常相似 (1)reverse the whole array (2)reverse each subarray seperated by ' ' 注意不要忘了reverse最后一个word 阅读全文
posted @ 2017-12-03 01:34 apanda009 阅读(140) 评论(0) 推荐(0)
摘要:/* Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and b... 阅读全文
posted @ 2017-12-03 01:25 apanda009 阅读(253) 评论(0) 推荐(0)
摘要:这题就是不停的DFS, 直到 n == 1. 有个判断条件 if (item.size() > 1) 是为了防止答案是自己本身n, 按照题意, 这是不允许的. 参考了:http://www.meetqun.com/thread-10673-1-1.html 时间复杂度, 个人觉得是O(n*log(n 阅读全文
posted @ 2017-12-02 22:43 apanda009 阅读(391) 评论(0) 推荐(0)
摘要:O(N)扫一遍array就可以了: 找三个最大的, 两个最小的,然后根据第三个最大的正负来判断。 Approach 4: O(n) Time, O(1) Space Note – Step 1 and Step 2 can be done in single traversal of the arr 阅读全文
posted @ 2017-12-02 12:37 apanda009 阅读(175) 评论(0) 推荐(0)
摘要:是不是dp[i][j]是到a[i]和b[j]的最小diff之和 那么dp[i][j] = min( dp[i-1][j-1]+math.abs(a[i]-b[j]), dp[i][j-1]) 第一个是肯定包含b[j]的,第二个是肯定不包含b[j]的, 阅读全文
posted @ 2017-12-02 12:02 apanda009 阅读(101) 评论(0) 推荐(0)
摘要:recursive (O(logn) space) and iterative solutions (O(1) space) This is not a very intuitive problem for me, I have to spent quite a while drawing figu 阅读全文
posted @ 2017-12-02 10:14 apanda009 阅读(183) 评论(0) 推荐(0)
摘要:dp就是dp[1] = 1, dp[n] = dp[n - 1] + n, 公式应该是 n(n + 1) / 2 + 1 阅读全文
posted @ 2017-12-02 08:41 apanda009 阅读(258) 评论(0) 推荐(0)
摘要:Here is a way you can do it by hand. Basically the idea is we take in some numbers we don't want to generate, then we generate a random number and if 阅读全文
posted @ 2017-12-02 08:07 apanda009 阅读(305) 评论(0) 推荐(0)
摘要:break the loop at the last node which pointed to the entry. Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove 阅读全文
posted @ 2017-12-02 07:57 apanda009 阅读(293) 评论(0) 推荐(0)
摘要:第二题: 相当于实现Microsoft里面的查找替换的功能,给一个string article, 一个string find,一个string replace,把文章里所有的find都替换成replace,例如abcdbc把bc换成e >aede。思路比较简单,直接indexOf做了,只不过本来应该 阅读全文
posted @ 2017-12-02 06:55 apanda009 阅读(224) 评论(0) 推荐(0)
摘要:注意参数给的是List ,不知道实现的话get方法可能是o(n)的。一开始写了个用get的,doubt 我的时间复杂度,我说这个可能是o(n),那可能需要用iterator。考官说对那就写吧,然后秒写,这里楼主就突然傻逼了。这题相当于peeking iterator,双指针(双iterator)遍历 阅读全文
posted @ 2017-12-02 05:43 apanda009 阅读(191) 评论(0) 推荐(0)
摘要:方法2:进一步的方法是用HashSet, 每次取长度为10的字符串,O(N)时间遍历数组,重复就加入result,但这样需要O(N)的space, 准确说来O(N*10bytes), java而言一个char是2 bytes,所以O(N*20bytes)。String一大就MLE 最优解:是在方法2 阅读全文
posted @ 2017-12-02 04:48 apanda009 阅读(180) 评论(0) 推荐(0)
摘要:题目链接: https://leetcode.com/problems/nested-list-weight-sum/ Given a nested list of integers, return the sum of all integers in the list weighted by th 阅读全文
posted @ 2017-12-02 03:10 apanda009 阅读(1690) 评论(0) 推荐(0)
摘要:or this question, the key part is: what is the state of the game? Intuitively, to uniquely determine the result of any state, we need to know: A secon 阅读全文
posted @ 2017-12-02 01:49 apanda009 阅读(266) 评论(0) 推荐(0)
摘要:public class RetainBestCache { private Map cache;. 鍥磋鎴戜滑@1point 3 acres private Map> rankingOfObject;.鏈枃鍘熷垱鑷�1point3acres璁哄潧 private DataSource dataSource; private int maxSiz... 阅读全文
posted @ 2017-12-02 00:14 apanda009 阅读(6290) 评论(0) 推荐(0)
摘要:基本思路就是递归的把k sum化成k-1 sum,直到变成trivial的2sum来做。 可以用两个hashset 存元素,sum ,这样就是store O(n), get(O(1)) 阅读全文
posted @ 2017-12-02 00:09 apanda009 阅读(162) 评论(0) 推荐(0)
摘要:This was asked in LinkedIn Interview Given a list of child->parent relationships, build a binary tree out of it. All the element Ids inside the tree are unique. Example: Given the following relatio... 阅读全文
posted @ 2017-12-01 23:47 apanda009 阅读(391) 评论(0) 推荐(0)
摘要:public class TextFile implements Iterable{ private BufferedReader br; public TextFile(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileR... 阅读全文
posted @ 2017-12-01 22:07 apanda009 阅读(150) 评论(0) 推荐(0)
摘要:public class MyList { private static final int DEFAULT_CAPACITY = 10; private E[] elements; private int size = 0; public MyList() { elements = new E[DEFAULT_CAPACITY]; ... 阅读全文
posted @ 2017-12-01 12:49 apanda009 阅读(145) 评论(0) 推荐(0)
摘要:public static String intToRoman(int num) { String M[] = {"", "M", "MM", "MMM"}; String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; Strin 阅读全文
posted @ 2017-12-01 11:00 apanda009 阅读(92) 评论(0) 推荐(0)
摘要:input check, 正数? In every recursion, we at most try k groups, which means the search tree has k branches at most. And the depth of the search tree is 阅读全文
posted @ 2017-12-01 09:29 apanda009 阅读(297) 评论(0) 推荐(0)