摘要:
GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]] 1 public class Solution { 2 public ArrayList> generate(int numRows) { 3 ArrayList> res = new ArrayList>(); 4 if(numRows==0) retu... 阅读全文
posted @ 2014-02-19 00:25
krunning
阅读(110)
评论(0)
推荐(0)
摘要:
Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL. 1 public class Solution { 2 public ListNode rotateRight(ListNode head, int n) { 3 if(head==null || n==0) return head; 4 int 阅读全文
posted @ 2014-02-17 05:50
krunning
阅读(112)
评论(0)
推荐(0)
摘要:
Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE" 阅读全文
posted @ 2014-02-17 02:18
krunning
阅读(127)
评论(0)
推荐(0)
摘要:
Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] 1 public class Solution { 2 public int[][] generateMatrix(int n) { 3 int []count = {1}; 4 ... 阅读全文
posted @ 2014-02-17 02:16
krunning
阅读(128)
评论(0)
推荐(0)
摘要:
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]... 阅读全文
posted @ 2014-02-17 02:15
krunning
阅读(235)
评论(0)
推荐(0)
摘要:
Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ≤m≤n≤ length of list. 1 public class Solution { 2 public ListNode reverseBe 阅读全文
posted @ 2014-02-17 02:14
krunning
阅读(122)
评论(0)
推荐(0)
摘要:
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 7After calling your function, the tree s... 阅读全文
posted @ 2014-02-17 02:07
krunning
阅读(207)
评论(0)
推荐(0)
摘要:
Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3 Return6. 1 public class Solution { 2 public int maxPathSum(TreeNode root) { 3 int res []={Integer.MIN_VALUE}; 4 he... 阅读全文
posted @ 2014-02-17 01:46
krunning
阅读(168)
评论(0)
推荐(0)
摘要:
The string"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY I RAnd then read line by line:"PAHNAPLSIIGYIR"Write the code that will take a string and 阅读全文
posted @ 2014-02-17 01:44
krunning
阅读(339)
评论(0)
推荐(0)
摘要:
1 public class Solution { 2 public String longestCommonPrefix(String[] strs) { 3 int len = strs.length; 4 if(len<=0) return ""; 5 if(len==1) return strs[0]; 6 String pre = strs[0]; 7 for(int i=1;i<len;i++){ 8 String cur = strs[i]; 9 ... 阅读全文
posted @ 2014-02-17 01:43
krunning
阅读(134)
评论(0)
推荐(0)
摘要:
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure. 1 public class Solution { 2 public void recoverTree(TreeNode root) { 3 if(root==null) return ; 4 ArrayList t = new ArrayList (); 5 ArrayList v = new ArrayLis... 阅读全文
posted @ 2014-02-17 01:40
krunning
阅读(237)
评论(0)
推荐(0)
摘要:
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse. 1 阅读全文
posted @ 2014-02-13 04:51
krunning
阅读(126)
评论(0)
推荐(0)
摘要:
Given a collection of integers that might contain duplicates,S, return all possible subsets.IfS=[1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []] 1 public class Solution{ 2 public ArrayList> subsetsWithDup(int[] num) { 3 Arrays.sort(num); 4 ArrayList> res = new Arra... 阅读全文
posted @ 2014-02-13 04:28
krunning
阅读(170)
评论(0)
推荐(0)
摘要:
1 public class Solution { 2 public ArrayList> fourSum(int[] num, int target) { 3 ArrayList> res = new ArrayList>(); 4 int len = num.length; 5 if(len temp = new ArrayList();15 temp.add(num[i]);16 temp.add(num[j]);17 ... 阅读全文
posted @ 2014-02-13 04:27
krunning
阅读(129)
评论(0)
推荐(0)
摘要:
Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice? 1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 int len = A.length; 4 if(len<=2) return len; 5 int count = 1; 6 int p1 =1, p2=1; 7 while(p2<len){ 8 ... 阅读全文
posted @ 2014-02-13 04:26
krunning
阅读(99)
评论(0)
推荐(0)
摘要:
1 public class Solution { 2 public ArrayList grayCode(int n) { 3 ArrayList res = new ArrayList(); 4 res.add(0); 5 for(int i=0;i=0;j--){ 9 res.add(highest+res.get(j));10 }11 }12 return res;13 }14 }View Code 阅读全文
posted @ 2014-02-13 04:25
krunning
阅读(114)
评论(0)
推荐(0)
摘要:
1 public class Solution { 2 public TreeNode buildTree(int[] inorder, int[] postorder) { 3 int inEnd = inorder.length-1; 4 int postEnd = postorder.length-1; 5 if(inEnd0)19 root.left = build(in,inStart,piv-1,post,postStart,postStart+leftLen-1);20 if(piv... 阅读全文
posted @ 2014-02-12 04:08
krunning
阅读(116)
评论(0)
推荐(0)
摘要:
1 public class Solution { 2 public TreeNode buildTree(int[] preorder, int[] inorder) { 3 int len =preorder.length; 4 if(lenpreE || inS>inE) return null; 9 int first = pre[preS];10 TreeNode p = new TreeNode(first);11 if(preS==preE) // don't forget this12 ... 阅读全文
posted @ 2014-02-12 04:01
krunning
阅读(120)
评论(0)
推荐(0)
摘要:
1 public class Solution { 2 public String countAndSay(int n) { 3 String res = "1"; 4 if(n==0) return res; 5 for(int i=1;i1){22 temp = temp+count+last;23 count = 1;24 }25 else{26 temp = temp+c... 阅读全文
posted @ 2014-02-12 04:01
krunning
阅读(130)
评论(0)
推荐(0)
摘要:
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S="ADOBECODEBANC"T="ABC"Minimum window is"BANC". 1 public class Solution { 2 public String minWindow(String S, String T) { 3 int sLen 阅读全文
posted @ 2014-02-12 03:58
krunning
阅读(155)
评论(0)
推荐(0)
摘要:
Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. 1 public class Solution { 2 publ... 阅读全文
posted @ 2014-02-12 03:56
krunning
阅读(146)
评论(0)
推荐(0)
摘要:
The Sudoku board could be partially filled, where empty cells are filled with the character'.'. 1 public class Solution { 2 public boolean isValidSudoku(char[][] board) { 3 for(int i=0;i<9;i++){ 4 int []row = new int[10]; 5 int []col = new int[10]; 6 f... 阅读全文
posted @ 2014-02-10 15:33
krunning
阅读(168)
评论(0)
推荐(0)
摘要:
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter) 1 public class Solution { 2 public ArrayList restoreIpAddresses(S 阅读全文
posted @ 2014-02-10 15:32
krunning
阅读(134)
评论(0)
推荐(0)
摘要:
Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4->3- 阅读全文
posted @ 2014-02-10 15:31
krunning
阅读(126)
评论(0)
推荐(0)
摘要:
Given an array of words and a lengthL, format the text such that each line has exactlyLcharacters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces' 'when necessary so that each line 阅读全文
posted @ 2014-02-10 15:30
krunning
阅读(237)
评论(0)
推荐(0)
摘要:
Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return[1,2,3,6,9,8,7,4,5]. 1 public class Solution { 2 public ArrayList spiralOrder(int[][] matrix) { 3 ... 阅读全文
posted @ 2014-02-10 15:29
krunning
阅读(132)
评论(0)
推荐(0)
摘要:
You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place? 1 public class Solution { 2 public void rotate(int[][] matrix) { 3 int len = matrix.length; 4 if(len<=0) return; 5 for(int i=0;i<len-1;i++){... 阅读全文
posted @ 2014-02-08 05:53
krunning
阅读(100)
评论(0)
推荐(0)
摘要:
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0 1 public class So 阅读全文
posted @ 2014-02-08 04:48
krunning
阅读(97)
评论(0)
推荐(0)
摘要:
Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, ... 阅读全文
posted @ 2014-02-08 04:32
krunning
阅读(221)
评论(0)
推荐(0)
摘要:
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1]. 1 public class Solution { 2 public ArrayList> permuteUnique(int[] num) { 3 ArrayList> res = new Arra... 阅读全文
posted @ 2014-02-08 04:30
krunning
阅读(128)
评论(0)
推荐(0)
摘要:
Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path. 1 public class Solution { 2 public int minPathSum(int[][] grid) { 3 int len1 = grid.length; 4 if(len1==0) return 0; 5 int len2 =... 阅读全文
posted @ 2014-02-07 03:52
krunning
阅读(146)
评论(0)
推荐(0)
摘要:
Determine whether an integer is a palindrome. Do this without extra space. 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 if(x=10){ 6 div *=10; 7 } 8 while(x>0){ 9 if(x/div!=x%10) return false;10 x = x%div/10;11 ... 阅读全文
posted @ 2014-02-07 03:49
krunning
阅读(120)
评论(0)
推荐(0)
摘要:
There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1,0], [0,0,0]] 1 public class Solution { 2 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 3 int len = obstacleGrid.length; 4 if(len==0) return 0; 5 int len2 = obstacleGrid... 阅读全文
posted @ 2014-02-07 03:48
krunning
阅读(108)
评论(0)
推荐(0)
摘要:
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A =[2,3,1,1,4]The minimum n 阅读全文
posted @ 2014-02-07 03:47
krunning
阅读(270)
评论(0)
推荐(0)
摘要:
Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of 阅读全文
posted @ 2014-02-07 03:46
krunning
阅读(346)
评论(0)
推荐(0)
摘要:
Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3], ... 阅读全文
posted @ 2014-02-06 15:31
krunning
阅读(122)
评论(0)
推荐(0)
摘要:
1 public class Solution { 2 public boolean isSameTree(TreeNode p, TreeNode q) { 3 if(p==null) return q==null; 4 if(q==null) return p==null; 5 if(p.val==q.val){ 6 return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right); 7 } 8 else return ... 阅读全文
posted @ 2014-02-06 15:22
krunning
阅读(113)
评论(0)
推荐(0)
摘要:
1 public class Solution { 2 public boolean isBalanced(TreeNode root) { 3 if(root==null) return true; 4 if(checkHeight(root)==-1) return false; 5 return true; 6 } 7 public int checkHeight(TreeNode root){ 8 if(root==null) return 0; 9 int left = chec... 阅读全文
posted @ 2014-02-06 15:20
krunning
阅读(109)
评论(0)
推荐(0)
摘要:
Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="aadbbbaccc", return false. 1 public class Solution { 2 public boolean isInterleave(String s1, String s2, Strin 阅读全文
posted @ 2014-02-06 15:18
krunning
阅读(116)
评论(0)
推荐(0)
摘要:
A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12", it cou 阅读全文
posted @ 2014-02-06 15:14
krunning
阅读(108)
评论(0)
推荐(0)
浙公网安备 33010602011771号