自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(50)
  • 收藏
  • 关注

转载 [LeetCode] Text Justification (put words in lines with same length)

解答从这里来 比较麻烦的字符串细节实现题。需要解决以下几个问题: 首先要能判断多少个word组成一行: 这里统计读入的所有words的总长curLen,并需要计算空格的长度。假如已经读入words[0:i]。当curLen + i <=L 且加curLen + 1 + word[i+1].size() > L时,一行结束。 知道一行的所有n个...

2015-04-05 11:10:00 136

转载 [LinkedIn] Is Same Tree

/* * Check if two trees are the same */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null) { return...

2015-04-05 10:13:00 129

转载 [LinkedIn]Min cost of paint house with color

见过在onsite的居多 Given a list of houses and the cost of painting each house, the houses can be painted in three colors RED, GREEN and BLUE, two neighboring houses can’t be painted in the s...

2015-04-05 09:22:00 129

转载 [LeetCode] Longest Palindromic Substring

// O(n^2) time and O(1) space // Given a center, either one letter or two letter, // Find longest palindrome private String expandCenter(String s, int left, int right) { while (lef...

2015-04-04 13:58:00 105

转载 [LeetCode] Inplace random shuffling an array

public void shuffle(int[] A) { if (A == null || A.length == 0) return; for (int i = 0; i < A.length-1; ++i) { int random = Math.random()*(A.length-i) + i; int...

2015-04-04 13:28:00 110

转载 [LinkedIn] Find target number in 2D sorted array (matrix)

Say I’m given a 2d array where all the numbers in the array are in increasing order from left to right and top to bottom. What is the best way to search and determine if a target numbe...

2015-04-03 14:33:00 102

转载 [LeetCode] Permutations

/** Solution 1, using recursion. Construct a list of numbers by maintaining what you have now and what is left. Every level you take one element from the current "rest" array and appe...

2015-04-03 10:39:00 112

转载 [LeetCode] Search for a Range (sorted integers array,find start & end position of a target number)

/** O(n)的解法,从左到右遍历。 **/ public class Solution { public int[] searchRange(int[] A, int target) { if(A.length == 0) { return new int[]{-1,-1}; } in...

2015-04-03 10:01:00 86

转载 [LinkedIn] String Permutation

print all permutation of strings From Here public static void permutation(String str) { permutation("", str); } private static void permutation(String prefix, String str) { ...

2015-04-02 04:23:00 110

转载 [LeetCode]Two Sum

public class Solution { public int[] twoSum(int[] numbers, int target) { HashMap<Integer, Integer> num = new HashMap(); for(int i = 0; i < numbers.length; i...

2015-04-02 04:13:00 89

转载 [LinkedIn] Word Distance Finder

Example: WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList(“the”, “quick”, “brown”, “fox”, “quick”)); assert(finder.distance(“fox”,”the”) == 3); assert(finder.distance...

2015-04-02 04:05:00 112

转载 [LeetCode] Maximum path sum

From Here For each node, 4 conditions: 1. Node only (因为本题中的节点可能是负值!) 2. L-sub + Node 3. R-sub + Node 4. L-sub + Node + R-sub (in here the max value cannot be passed to the parent) ...

2015-04-01 13:37:00 119

转载 [LeetCode]Merge Interval

懒得自己写了。。代码这里来的:点击 总体来讲就是先按照x来sort,x一样sort y, 然后比较前面的y和后面的x public ArrayList<Interval> merge(ArrayList<Interval> intervals) { ArrayList<Interval> res = new Arra...

2015-04-01 13:29:00 128

转载 [LeetCode] Merge k Sorted List (priority queue, min heap, comparator)

public class Solution { public ListNode mergeKLists(List<ListNode> lists) { if(lists == null || lists.size() == 0) { return null; } Priorit...

2015-04-01 13:13:00 100

转载 [LeetCode]Maximum number of points on a straight line in 2d plane

From Here Solution: Remember that a line can be represented by y=kx+d, if p1 and p2 are in same line, then y1=x1k+d, y2=kx2+d, so y2-y1=kx2-kx1, so k=(y2-y1)/(x2-x1), then we ...

2015-04-01 12:28:00 126

转载 [LinkedIn]Implement Find and replace (find given pattern and replace it with a given string)

From Here public static String FindAndReplace(String GivenString, String Pattern, String ReplaceString) { int j = 0; int tempi; for (int i...

2015-04-01 12:07:00 174

转载 [LinkedIn]Serialize and deserialize a binary tree

From Here Assume we have a binary tree below: _30_ / \ 10 20 / / \ 50 45 35 Using pre-order traversal, the algorithm should write the following to a f...

2015-03-31 23:12:00 108

转载 [LinkedIn] Array of products of all other numbers (no division)

Given an array of numbers, nums, return an array of numbers products, where products[i] is the product of all nums[j], j != i. Input : [1, 2, 3, 4, 5] Output: [(2*3*4*5), (1*...

2015-03-31 23:09:00 95

转载 [LeetCode] Search in rotated sorted array

search for a target from a rotated sorted array non-recursion version from here public int search(int[] A, int target) { if(A==null || A.length==0) return -1; in...

2015-03-31 22:56:00 92

转载 [LinkedIn] Lowest Common Ancestor

From Here /** * Given two nodes of a tree, * method should return the deepest common ancestor of those nodes. * * A * / \ * B C...

2015-03-31 16:02:00 106

转载 [LinkedIn] Find K nearest (closest) neighbors from point (comparator/comparable, priority queue )

/** This is for finding k nearest neighbor from the original point using a MAX heap, each time if the dist is less than the MAX we put it into the q. **/ public Collection<Point>...

2015-03-31 15:37:00 133

转载 [LinkedIn] Find all triangles in an array

From Here Changed it a little public ArrayList<ArrayList<Integer>> valid2(int[] A) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList&l...

2015-03-31 15:26:00 86

转载 Binary Search Algorithm

Just a BS(bulls**t) algorithm, nothing special int binaryS (int[] array, int key, int min, int max) { if (max < min) { return -1;//can’t find } int mid = (min ...

2015-03-31 15:09:00 158

转载 [LinkedIn]Find top 10 urls

From Here Given a large network of computers, each keeping log files of visited urls, find the top ten most visited URLs. Ans: we will just mimic the actions of map-reduce: 1. pre-p...

2015-03-31 14:56:00 144

转载 [LeetCode] clone graph

/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x;...

2015-03-31 14:45:00 74

转载 [LinkedIn] Mirror of a binary tree

Given a binary tree, return the mirror of it. Very easy. Recursion TreeNode mirror(TreeNode root) { if (root == NULL) return NULL; else { TreeNode newNode = new TreeNode...

2015-03-31 14:25:00 92

转载 [LeetCode]Permutation sequence

From Here 数学找规律 public String getPermutation(int n, int k) { if(n<=0) return ""; k--; StringBuilder res = new StringBuilder(); int factorial = 1; ...

2015-03-31 14:22:00 83

转载 [LeetCode] Combination Sum

据说linkedin会考。。。没啥难的倒是 public class Solution { public List<List<Integer>> combinationSum2(int[] num, int target) { Arrays.sort(num); List<List<Inte...

2015-03-31 14:12:00 79

转载 [LinkedIn] Text File Iterable

Implement a (Java) Iterable object that iterates lines one by one from a text file.. /** A reference to a file. */ public class TextFile implements Iterable<String>. From 1poi...

2015-03-31 13:50:00 105

转载 [LinkedIn]Isomorphic Strings

From Here Given two (dictionary) words as Strings, determine if they are isomorphic. Two words are called isomorphic if the letters in one word can be remapped to get the second word....

2015-03-31 13:42:00 98

转载 [LinkedIn]Combination factors

From Here Linkedin 的一道题目 Keep track of the current remain factor to be decomposed, LargestFactor is the largest possible factor to decompose (so that we don’t need to try anything la...

2015-03-31 11:32:00 96

转载 [LinkedIn] Url shortening

Application layer shortening uses (original url => tiny url) expand url (tiny url => original url) Data Storage (hashtable tiny url’s hash code => original url) ...

2015-03-31 10:28:00 302

转载 [LinkedIn]find balance (equilibrium) point (index) in an array (unsorted)

Code From Here (g4g) /** 1) Initialize leftsum as 0 2) Get the total sum of the array as sum 3) Iterate through the array and for each index i, do following. a) Update sum to g...

2015-03-31 04:02:00 81

转载 [LinkedIn]Intersection of two sorted array

print intersection of two sorted array 思路from g4g: 1) Use two index variables i and j, initial values i = 0, j = 0 2) If arr1[i] is smaller than arr2[j] then increment i. 3) If arr1...

2015-03-31 03:48:00 133

转载 [LinkedIn]Merge Sorted Iterator (using comparator)

原帖地址:http://www.mitbbs.com/article_t/JobHunting/32909075.html 自己加了一点注释 public static Iterable<Integer> mergeKSortedIterators(List<Iterator<Integer>> Iters){ ...

2015-03-31 03:12:00 130

转载 [LinkedIn] Implement a Semaphore

From Here 这是一个带有upperbound的semaphore。 public class BoundedSemaphore { private int signals = 0; private int bound = 0; public BoundedSemaphore(int upperBound){ this.bound ...

2015-03-31 02:22:00 82

转载 [LeetCode]Min Stack

implement Stack, getMin() is always O(1) class MinStack { LinkedList<Integer> main = new LinkedList<Integer>(); LinkedList<Integer> min = new LinkedList<In...

2015-03-31 01:18:00 72

转载 [LinkedIn] singleton, thread safe

From here //thread safe singleton. static makes it guarantee that it only gets created at the first time public class Singleton { public final static Singleton INSTANCE = new Single...

2015-03-31 00:34:00 109

转载 [LeetCode] Maxium Subarray

dynamic programming 的一道题。curMax是包括当前元素的subarray的max, gloMax是当前元素之前的整个array的max public class Solution { public int maxSubArray(int[] A) { if(A.length == 1) { re...

2015-03-31 00:25:00 84

转载 [LeetCode] Pow(x,n) O(logN)

又一个Linkedin面试过的 public class Solution { public double pow(double x, int n) { int sign = 1; if(n < 0) { n = -n; sign = -1; } ...

2015-03-31 00:01:00 77

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除