摘要: Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surrounded region .For example,X X X XX O O XX X O XX O X XAfter running your function, the board should be:X X X XX X X XX X X 阅读全文
posted @ 2014-02-06 15:06 krunning 阅读(226) 评论(0) 推荐(0)
摘要: Given two words (startandend), and a dictionary, find the length of shortest transformation sequence 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",&q 阅读全文
posted @ 2014-02-06 15:05 krunning 阅读(143) 评论(0) 推荐(0)
摘要: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"isnota palindrome. 1 public class Solution { 2 public boolean isPalindrome(String s) { 3 int len = 阅读全文
posted @ 2014-02-06 15:04 krunning 阅读(221) 评论(0) 推荐(0)
摘要: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.Your algorithm should run in O(n) complexity. 1 public class Solution { 2 public... 阅读全文
posted @ 2014-02-06 15:03 krunning 阅读(255) 评论(0) 推荐(0)
摘要: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.T 阅读全文
posted @ 2014-02-06 15:02 krunning 阅读(78) 评论(0) 推荐(0)
摘要: Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.For example, givens="aab",Return [ ["aa","b"], ["a","a","b"] ] 1 public class Solution { 2 public ArrayLis 阅读全文
posted @ 2014-02-06 15:01 krunning 阅读(130) 评论(0) 推荐(0)
摘要: Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs.For example, givens="aab",Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut. 1 public cla 阅读全文
posted @ 2014-02-06 15:00 krunning 阅读(239) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public ArrayList inorderTraversal(TreeNode root) { 3 ArrayList res = new ArrayList (); 4 Stack stack = new Stack(); 5 TreeNode cur = root; 6 while(!stack.isEmpty()||cur!=null){ 7 if(cur!=null){ 8 stack.push(cur); 9 ... 阅读全文
posted @ 2014-02-06 14:59 krunning 阅读(100) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public int climbStairs(int n) { 3 int f1 = 2; 4 int f2 = 1; 5 if(n<=0) return 0; 6 if(n==1) return f2; 7 if(n==2) return f1; 8 int fn=0; 9 for(int i=2;i<n;i++){10 fn=f1+f2;11 f2=f1;12 f1=fn;13 }14 return fn;15 }16 }View Code 阅读全文
posted @ 2014-02-06 14:59 krunning 阅读(89) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public boolean isValidBST(TreeNode root) { 3 return is(root,Integer.MAX_VALUE,Integer.MIN_VALUE); 4 } 5 public boolean is(TreeNode root,int max,int min){ 6 if(root==null) 7 return true; 8 if(root.valmin){ 9 retur... 阅读全文
posted @ 2014-02-06 14:58 krunning 阅读(87) 评论(0) 推荐(0)
摘要: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3 1 public class Solution { 2 public boolean isSymmetric(TreeNode... 阅读全文
posted @ 2014-02-06 14:57 krunning 阅读(102) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public ArrayList> levelOrder(TreeNode root) { 3 ArrayList> res = new ArrayList>(); 4 LinkedList cur = new LinkedList(); 5 if(root==null) return res; 6 cur.offer(root); 7 while(!cur.isEmpty()){ 8 LinkedList next = ne... 阅读全文
posted @ 2014-02-06 14:56 krunning 阅读(103) 评论(0) 推荐(0)
摘要: 1 public class Solution {2 public int maxDepth(TreeNode root) {3 if(root==null)4 return 0;5 return Math.max(maxDepth(root.left),maxDepth(root.right))+1;6 }7 }View Code 阅读全文
posted @ 2014-02-06 14:55 krunning 阅读(90) 评论(0) 推荐(0)
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 1 // We shuould use a static variable int the recursive function!!!! 2 public class Solution { 3 static ListNode h; 4 public TreeNode sortedListToBST(ListNode head) { 5... 阅读全文
posted @ 2014-02-06 14:54 krunning 阅读(109) 评论(0) 推荐(0)
摘要: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 1 public class Solution { 2 public int minDepth(TreeNode root) { 3 if(root==null) return 0; 4 int left = minDepth(root.le... 阅读全文
posted @ 2014-02-06 14:53 krunning 阅读(106) 评论(0) 推荐(0)
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
posted @ 2014-02-06 14:52 krunning 阅读(114) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public int sqrt(int x) { 3 if(x==0 ||x==1) return x; 4 long start = 1; 5 long end = x-1; 6 while(startx){11 end = mid-1;12 }13 else{14 start = mid+1;15 }16 }17 ... 阅读全文
posted @ 2014-02-06 14:51 krunning 阅读(130) 评论(0) 推荐(0)
摘要: Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []] 1 public class Solution { 2 ... 阅读全文
posted @ 2014-02-06 14:50 krunning 阅读(143) 评论(0) 推荐(0)
摘要: Given two sorted integer arrays A and B, merge B into A as one sorted array. 1 public class Solution { 2 public void merge(int A[], int m, int B[], int n) { 3 int p3 = m+n-1; 4 int p1 = m-1; 5 int p2 = n-1; 6 while(p1>=0 && p2>=0){ 7 if(A[p1]>=B[p2]){ ... 阅读全文
posted @ 2014-02-06 14:49 krunning 阅读(95) 评论(0) 推荐(0)
摘要: Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation ofs1="great": great / \ gr eat / \ / \g r e at / \ a tTo scramble the string, we may choose any non-leaf no... 阅读全文
posted @ 2014-02-06 14:48 krunning 阅读(272) 评论(0) 推荐(0)
摘要: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 1 public class Solution { 2 public int maximalRectangle(char[][] matrix) { 3 int row = matrix.length; 4 if(row=0;j--){28 if(matrix[i][j]=='1'){29 ... 阅读全文
posted @ 2014-02-06 14:47 krunning 阅读(138) 评论(0) 推荐(0)
摘要: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3. 1 public class Solution { 2 public ListNode deleteDu 阅读全文
posted @ 2014-02-06 14:45 krunning 阅读(112) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public void setZeroes(int[][] matrix) { 3 boolean row = false; 4 boolean col = false; 5 for(int i=0;i<matrix.length;i++){ 6 if(matrix[i][0]==0){ 7 row =true; 8 break; 9 }10 }11 ... 阅读全文
posted @ 2014-02-06 14:43 krunning 阅读(136) 评论(0) 推荐(0)
摘要: Given a sorted linked list, delete all duplicates such that each element appear onlyonce. 1 public class Solution { 2 public ListNode deleteDuplicates(ListNode head) { 3 ListNode cur =head; 4 while(cur!=null&&cur.next!=null){ 5 if(cur.val==cur.next.val){ 6 ... 阅读全文
posted @ 2014-02-06 14:42 krunning 阅读(83) 评论(0) 推荐(0)
摘要: Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. 1 public class Solution { 2 public int largestRectangleArea(int[] height) { 3 int length = height.length; 4 if(leng... 阅读全文
posted @ 2014-02-06 14:41 krunning 阅读(225) 评论(0) 推荐(0)
摘要: Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array. 1 public class Solution { 2 //rotated array all is A[start]){10 if(target>=A[start] ... 阅读全文
posted @ 2014-02-06 14:40 krunning 阅读(74) 评论(0) 推荐(0)
摘要: Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.For example,Givenboard=[ [&q 阅读全文
posted @ 2014-02-06 14:39 krunning 阅读(126) 评论(0) 推荐(0)
摘要: Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],] 1 public class Solution { 2 public ArrayList> combine(int n, int k) { 3 ArrayList> res = new ArrayList>(); 4 ... 阅读全文
posted @ 2014-02-06 14:38 krunning 阅读(118) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public int[] plusOne(int[] digits) { 3 int carry = 1; 4 for(int i=digits.length-1;i>=0;i--){ 5 digits[i] +=carry; 6 carry = digits[i]/10; 7 digits[i] %=10; 8 } 9 if(carry>0){10 int []num ... 阅读全文
posted @ 2014-02-06 14:37 krunning 阅读(124) 评论(0) 推荐(0)
摘要: 1 public class Solution { // don't forget 'break' 'if(s.length()<=0) return false;' should be after trim 2 public boolean isNumber(String s) { 3 s = s.trim(); 4 if(s.length()<=0) return false; 5 boolean eFound = false; 6 boolean dFound = false; 7 int end ... 阅读全文
posted @ 2014-02-06 14:34 krunning 阅读(115) 评论(0) 推荐(0)
摘要: Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a character 1 public class Solution { 2 public int mi... 阅读全文
posted @ 2014-02-06 14:33 krunning 阅读(138) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public String addBinary(String a, String b) { 3 int len1 = a.length(); 4 int len2 = b.length(); 5 StringBuilder sb = new StringBuilder(); 6 int p1 = len1-1; 7 int p2 = len2 -1; 8 int carry = 0; 9 while(p1>=0&&p2... 阅读全文
posted @ 2014-02-06 14:32 krunning 阅读(102) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 3 ListNode safe = new ListNode(-1); 4 ListNode pre = safe; 5 while(l1!=null && l2!=null){ 6 if(l1.val<l2.val){ 7 pre.next = l1; 8 l1 = l1.nex... 阅读全文
posted @ 2014-02-06 14:31 krunning 阅读(220) 评论(0) 推荐(0)
摘要: A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni 阅读全文
posted @ 2014-02-06 14:29 krunning 阅读(98) 评论(0) 推荐(0)
摘要: Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18]. 1 public class Solution { 2 public ArrayList merge(ArrayList intervals) { 3 if(intervals.size()==0) 4 return intervals; 5 6 ... 阅读全文
posted @ 2014-02-06 14:28 krunning 阅读(148) 评论(0) 推荐(0)
摘要: The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123""132""213""231""312""321"Givennandk, return thekthpermutation sequence. 1 阅读全文
posted @ 2014-02-06 14:27 krunning 阅读(105) 评论(0) 推荐(0)
摘要: Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0. 1 public class Solution { 2 public int lengthOfLastWord(String s) { 3 int len = s.length(); 4 int count=0; ... 阅读全文
posted @ 2014-02-06 14:24 krunning 阅读(78) 评论(0) 推荐(0)
摘要: Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times. 1 public class Solution { 2 public ArrayList insert(ArrayList intervals, Interval newInterval) { 3 ... 阅读全文
posted @ 2014-02-06 14:23 krunning 阅读(129) 评论(0) 推荐(0)
摘要: Now, instead outputting board configurations, return the total number of distinct solutions. 1 public class Solution { 2 public int totalNQueens(int n) { 3 int []total = new int [1]; 4 get(new int[n],0,n,total); 5 return total[0]; 6 } 7 public void get(int []queen... 阅读全文
posted @ 2014-02-06 14:21 krunning 阅读(129) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public ArrayList anagrams(String[] strs) { 3 int len = strs.length; 4 HashMap> hm = new HashMap>(); 5 ArrayList res = new ArrayList(); 6 for(int i=0;i temp = new ArrayList();13 temp.add(strs[i]);14 hm.pu... 阅读全文
posted @ 2014-02-06 14:20 krunning 阅读(130) 评论(0) 推荐(0)