摘要: 1 public class HashMap { 2 private static final int SIZE = 16; 3 private Entry table [] = new Entry [SIZE] ; 4 class Entry{ 5 private final String key; 6 String value; 7 Entry next; 8 Entry(String k,String v){ 9 key = k;10 value = ... 阅读全文
posted @ 2014-04-10 03:13 krunning 阅读(252) 评论(0) 推荐(0) 编辑
摘要: 1 public static TreeNode DeSerilize(ArrayListres,int[] i){ 2 if(res.get(i[0]).equals("#")||i[0]==res.size()) return null; 3 TreeNode ... 阅读全文
posted @ 2014-03-22 06:32 krunning 阅读(362) 评论(0) 推荐(0) 编辑
摘要: 1 public int DP(int[] stamp,int sum){ 2 int maxValue =0; 3 for(int v :stamp){ 4 maxValue = Math.max(maxValue, v); 5 } 6 int values []= new int[maxValue+1]; 7 values[0]=0; 8 for(int request=1;request dp){ 2 3 if(target==0) ... 阅读全文
posted @ 2014-03-02 08:35 krunning 阅读(442) 评论(0) 推荐(0) 编辑
摘要: 1 public static ListNode add(ListNode l1, ListNode l2){ 2 3 Stack s1 = new Stack(); 4 Stack s2 = new Stack(); 5 while(l1!=null){ 6 s1.push(l1.val); 7 l1 = l1.next; 8 } 9 while(l2!=null){10 s2.push(l2.val);11 ... 阅读全文
posted @ 2014-02-26 02:49 krunning 阅读(159) 评论(0) 推荐(0) 编辑
摘要: 1 public static boolean(ListNode l1,ListNode l2){ 2 boolean res = false; 3 int [] check = {-1}; 4 ListNode mid = getMid(head,check); 5 if( check[0]==1){ 6 ListNode next = mid.next; 7 ListNode newHead =reverse(next); 8 res=checkPalidrom(head, newHead ); 9 newHead =re... 阅读全文
posted @ 2014-02-26 02:47 krunning 阅读(306) 评论(0) 推荐(0) 编辑
摘要: Given two words (startandend), and a dictionary, find all shortest transformation sequence(s) fromstarttoend, such that:Only one letter can be changed at a timeEach intermediate word must exist in the dictionaryFor example,Given:start="hit"end="cog"dict=["hot","dot 阅读全文
posted @ 2014-02-24 05:01 krunning 阅读(224) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.F... 阅读全文
posted @ 2014-02-24 04:56 krunning 阅读(200) 评论(0) 推荐(0) 编辑
摘要: 1 public class Solution { 2 public ListNode insertionSortList(ListNode head) { 3 if(head==null) return head; 4 ListNode safe = new ListNode(Integer.MIN_VALUE); 5 ListNode p2 = head; 6 while(p2!=null){ 7 ListNode pre = find(safe,p2); 8 List... 阅读全文
posted @ 2014-02-24 04:53 krunning 阅读(190) 评论(0) 推荐(0) 编辑
摘要: 1 public class Solution { 2 public ListNode sortList(ListNode head) { 3 if(head==null || head.next==null) return head; 4 ListNode fast = head, slow = head; 5 while(true){ 6 fast = fast.next; 7 if(fast==null) break; 8 fast = fast.next; ... 阅读全文
posted @ 2014-02-24 04:50 krunning 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 1 public class Solution { 2 public int maxPoints(Point[] points) { 3 HashMap map = new HashMap(); 4 int res = 0; 5 for(int i=0;i<points.length;i++){ 6 map.clear(); 7 int curMax = 1; 8 int sameP =0; 9 for(int j=i+1;j<points.... 阅读全文
posted @ 2014-02-24 04:06 krunning 阅读(229) 评论(0) 推荐(0) 编辑
摘要: Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors. 1 public class Solution { 2 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 3 if(node==null) return node; 4 UndirectedGraphNode n1 = new UndirectedGraphNode(node.label);... 阅读全文
posted @ 2014-02-24 03:59 krunning 阅读(234) 评论(0) 推荐(0) 编辑
摘要: There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationito its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the starting gas 阅读全文
posted @ 2014-02-24 03:45 krunning 阅读(290) 评论(0) 推荐(0) 编辑
摘要: 1 public class Solution { 2 public int evalRPN(String[] tokens) { 3 String operations ="+-*/"; 4 Stack stack = new Stack(); 5 for(String s:tokens){ 6 if(!operations.contains(s)){ 7 stack.push(s); 8 } 9 else{10 ... 阅读全文
posted @ 2014-02-24 03:44 krunning 阅读(210) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}. 1 public class Solution { 2 public void reorderList(ListNode head) { 3 if(head==null) return; ... 阅读全文
posted @ 2014-02-22 16:23 krunning 阅读(250) 评论(0) 推荐(0) 编辑
摘要: 1 public class LRUCache { 2 HashMap ht; 3 int curSize; 4 int size; 5 entry first=null; 6 entry last=null; 7 public LRUCache(int capacity) { 8 size = capacity; 9 ht = new HashMap();10 curSize = 0;11 }12 13 public int get(int key) {14 ... 阅读全文
posted @ 2014-02-22 16:20 krunning 阅读(155) 评论(0) 推荐(0) 编辑
摘要: public class Solution { public int singleNumber(int[] A) { int left = A[0]; for(int i=1;i<A.length;i++){ left ^=A[i]; } return left; }} 阅读全文
posted @ 2014-02-22 13:25 krunning 阅读(104) 评论(0) 推荐(0) 编辑
摘要: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1. 阅读全文
posted @ 2014-02-22 13:21 krunning 阅读(139) 评论(0) 推荐(0) 编辑
摘要: Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, givens="leetcode",dict=["leet", "code"].Return true because"leetcode"can be segmented as"leet code&quo 阅读全文
posted @ 2014-02-22 13:13 krunning 阅读(159) 评论(0) 推荐(0) 编辑
摘要: Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.Note: You 阅读全文
posted @ 2014-02-22 13:12 krunning 阅读(219) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],] 1 p... 阅读全文
posted @ 2014-02-22 13:11 krunning 阅读(160) 评论(0) 推荐(0) 编辑
摘要: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11). 1 public class Solution { 2 ... 阅读全文
posted @ 2014-02-22 13:10 krunning 阅读(175) 评论(0) 推荐(0) 编辑
摘要: 1 public class Solution { 2 public ListNode detectCycle(ListNode head) { 3 if(head==null) return head; 4 ListNode fast = head; 5 ListNode slow = head; 6 while(fast!=null){ 7 slow = slow.next; 8 fast = fast.next; 9 if(fast!=null... 阅读全文
posted @ 2014-02-22 13:09 krunning 阅读(95) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, determine if it has a cycle in it. 1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 if(head==null) return false; 4 ListNode fast = head; 5 ListNode slow = head; 6 while(fast!=null){ 7 slow = slow.next; 8 ... 阅读全文
posted @ 2014-02-22 13:08 krunning 阅读(135) 评论(0) 推荐(0) 编辑
摘要: Given a stringsand a dictionary of wordsdict, add spaces insto construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens="catsanddog",dict=["cat", "cats", "and", "sand", "dog"].A 阅读全文
posted @ 2014-02-22 13:07 krunning 阅读(261) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appearsthreetimes except for one. Find that single one. 1 public class Solution { 2 public int singleNumber(int[] A) { 3 int bit [] = new int[32]; 4 for(int i=0;i>j; 7 if(rotate==0) break; 8 else bit[j] += ... 阅读全文
posted @ 2014-02-22 13:06 krunning 阅读(157) 评论(0) 推荐(0) 编辑
摘要: There areNchildren standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than their neighbors. 1 public class Solution { 2 p... 阅读全文
posted @ 2014-02-22 13:05 krunning 阅读(187) 评论(0) 推荐(0) 编辑
摘要: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. 1 public class Solution { 2 public RandomListNode copyRandomList(RandomListNode head) { 3 if(head==null) return head; 4 //insert 5 RandomLis... 阅读全文
posted @ 2014-02-22 13:04 krunning 阅读(143) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 1 public class Solution { 2 public int maxProfit(int[]... 阅读全文
posted @ 2014-02-19 05:33 krunning 阅读(212) 评论(0) 推荐(0) 编辑
摘要: 1 public class Solution { 2 public ArrayList preorderTraversal(TreeNode root) { 3 ArrayList res = new ArrayList(); 4 if(root==null) return res; 5 TreeNode cur = root; 6 Stackstack = new Stack(); 7 while(!stack.isEmpty() || cur!=null){ 8 if(cur!... 阅读全文
posted @ 2014-02-19 05:31 krunning 阅读(101) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions. 1 public class Solution { 2 public int maxProfit(int[] prices) { 3 int len = prices.length; 4 if(len==0) re... 阅读全文
posted @ 2014-02-19 05:30 krunning 阅读(132) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 1 public class Solution { 2 ... 阅读全文
posted @ 2014-02-19 05:29 krunning 阅读(130) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at 阅读全文
posted @ 2014-02-19 05:27 krunning 阅读(128) 评论(0) 推荐(0) 编辑
摘要: Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"Did you consider the case wherepath="/../"?In this case, you should return"/".Another corner case is the pat 阅读全文
posted @ 2014-02-19 05:26 krunning 阅读(262) 评论(0) 推荐(0) 编辑
摘要: 1 public class Solution { 2 public ArrayList postorderTraversal(TreeNode root) { 3 ArrayList res = new ArrayList(); 4 if(root==null) return res; 5 TreeNode cur = null, pre = null; 6 Stackstack = new Stack(); 7 stack.push(root); 8 while(!stack.isEm... 阅读全文
posted @ 2014-02-19 05:25 krunning 阅读(204) 评论(0) 推荐(0) 编辑
摘要: Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文
posted @ 2014-02-19 00:38 krunning 阅读(262) 评论(0) 推荐(0) 编辑
摘要: Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(k) extra space? 1 public class Solution { 2 public ArrayList getRow(int rowIndex) { 3 ArrayList res = new ArrayList(); 4 int num [... 阅读全文
posted @ 2014-02-19 00:32 krunning 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to ... 阅读全文
posted @ 2014-02-19 00:30 krunning 阅读(162) 评论(0) 推荐(0) 编辑
摘要: Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has length = 2.Another example is")()())", where the longest 阅读全文
posted @ 2014-02-19 00:28 krunning 阅读(149) 评论(0) 推荐(0) 编辑
摘要: Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ... 阅读全文
posted @ 2014-02-19 00:27 krunning 阅读(174) 评论(0) 推荐(0) 编辑
摘要: You are given a string,S, and a list of words,L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.For example, given:S:"barfoothefoobarman"L:["foo", " 阅读全文
posted @ 2014-02-19 00:26 krunning 阅读(172) 评论(0) 推荐(0) 编辑