leetCode Solutions
yangmicheal
这个作者很懒,什么都没留下…
展开
-
Find Minimum in Rotated Sorted Array
public class Solution { // Find the sorted part with smaller number public int findMin(int[] num) { int min = Integer.MAX_VALUE; int l = 0; int r = num.length-1;原创 2014-11-06 13:32:07 · 217 阅读 · 0 评论 -
Plus one
public class Solution { public int[] plusOne(int[] digits) { int c = 1; for (int i=digits.length-1; i>=0; i--){ digits[i] = digits[i] + c; if (digits[i] ==原创 2014-10-09 12:57:07 · 166 阅读 · 0 评论 -
Container With Most Water
public class Solution { public int maxArea(int[] height) { int maxArea = 0; int l = 0; int r = height.length-1; while(l<r){ maxArea = Math.max(maxArea,原创 2014-10-13 03:38:23 · 141 阅读 · 0 评论 -
Remove Nth Node From End of List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public原创 2014-10-13 12:36:08 · 169 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
public class Solution { public int removeDuplicates(int[] A) { int dup = 0; int dupTime=0; int newLength=0; for (int pos=0; pos<A.length; pos++){ if (A[原创 2014-10-13 10:53:34 · 170 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node II
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */public class原创 2014-10-13 10:26:50 · 146 阅读 · 0 评论 -
Set Matrix Zeroes
public class Solution { public void setZeroes(int[][] matrix) { if (matrix.length == 0) return; boolean setFirstRowZero = false; boolean setFirstColZero = false; //原创 2014-10-13 08:34:45 · 208 阅读 · 0 评论 -
search in Rotated Sorted Array
public class Solution { public int search(int[] A, int target) { int l = 0; int r = A.length-1; while(l<=r){ int m = (l+r)/2; if (target == A[m]){原创 2014-10-13 07:21:03 · 132 阅读 · 0 评论 -
Merge Sorted Array
public class Solution { public void merge(int A[], int m, int B[], int n) { int pos = m+n-1; int a = m-1; int b = n-1; while(a>=0 && b>=0){ if (A[a] > B原创 2014-10-08 12:40:30 · 125 阅读 · 0 评论 -
Palindrome Number
public class Solution { // Compare begin with end public boolean isPalindrome(int x) { if (x<0) return false; int d=1; while(x/d>=10){ d*=10; }原创 2014-10-13 13:56:10 · 204 阅读 · 0 评论 -
Symmetric Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-13 08:03:25 · 121 阅读 · 0 评论 -
Rotate Image
public class Solution { public void rotate(int[][] matrix) { // (i, j) -> ((i-n/2), (j-n/2)) -> ((j-n/2), (n/2-i)) -> (j, n-i) // (n-j, i) -> (i, j) int n=matrix[0].length-原创 2014-10-13 05:08:57 · 147 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
public class Solution { // Keep track of the minimum value public int maxProfit(int[] prices) { if (prices.length == 0) return 0; int min = Integer.MAX_VALUE; int maxPr原创 2014-10-13 06:14:14 · 137 阅读 · 0 评论 -
Binary Tree Level Order Traversal
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-13 09:31:31 · 148 阅读 · 0 评论 -
Longest Consecutive Sequence
public class Solution { // Store map(num[i], i) // find max length public int longestConsecutive(int[] num) { Map map = new HashMap(); boolean[] visited = new boolean[num.l原创 2014-11-06 12:11:59 · 164 阅读 · 0 评论 -
Swap Nodes in Pairs
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public原创 2014-10-05 04:21:24 · 130 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
public class Solution { public int removeDuplicates(int[] A) { int newLength = 0; int dup = 0; for (int pos = 0; pos<A.length; pos++){ if (A[pos] != dup || pos原创 2014-10-05 03:31:53 · 197 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-05 03:19:16 · 217 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { // Pr原创 2014-10-15 11:41:52 · 153 阅读 · 0 评论 -
Balanced Binary Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-05 02:29:41 · 150 阅读 · 0 评论 -
Merge Two Sorted Lists
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public原创 2014-10-05 02:01:46 · 116 阅读 · 0 评论 -
N-Queens II
public class Solution { public int totalNQueens(int n) { int[] matrix = new int[n+1]; count(matrix, n, 0); return matrix[n]; } public void count(int[] matrix,原创 2014-10-05 00:02:31 · 136 阅读 · 0 评论 -
Remove Element
public class Solution { public int removeElement(int[] A, int elem) { int newLength = 0; for (int pos=0; pos<A.length; pos++){ if (A[pos] != elem){ A[ne原创 2014-10-05 01:44:07 · 141 阅读 · 0 评论 -
Combinations
public class Solution { public List> combine(int n, int k) { List> result = new ArrayList>(); combineHelper(n, k, result, new ArrayList()); return result; } pu原创 2014-10-13 12:29:34 · 198 阅读 · 0 评论 -
Pascal's Triangle II
public class Solution { // From end to begin, A[j] = A[j] + A[j-1] => // [0,1,0,0,0,0] // [0,1,1,0,0,0] // [0,1,2,1,0,0] // [0,1,3,3,1,0] // [0,1,4,6,4,1] public List getRo原创 2014-10-13 11:26:16 · 143 阅读 · 0 评论 -
Same Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-03 13:03:07 · 120 阅读 · 0 评论 -
Binary Tree Preorder Traversal (Recursive solution)
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-03 11:31:00 · 129 阅读 · 0 评论 -
Binary Tree Inorder Traversal
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-03 14:19:08 · 174 阅读 · 0 评论 -
Reverse Integer
public class Solution { public int reverse(int x) { int sign = x>0 ? 1:-1; x = sign*x; int reversed = 0; while(x>0){ reversed = 10*reversed + x%10;原创 2014-10-03 13:16:45 · 139 阅读 · 0 评论 -
Maximum Depth of Binary Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-03 12:53:10 · 134 阅读 · 0 评论 -
Single Number II
public class Solution { public int singleNumber(int[] A) { int[] bits = new int[32]; int result = 0; for (int i=0; i<bits.length; i++){ for(int j=0; j<A.length;原创 2014-10-03 12:39:23 · 126 阅读 · 0 评论 -
Linked List Cycle II
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class原创 2014-10-03 11:48:26 · 127 阅读 · 0 评论 -
Linked List Cycle
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class原创 2014-10-03 11:47:19 · 147 阅读 · 0 评论 -
Binary Tree Postorder Traversal
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-03 11:46:08 · 194 阅读 · 0 评论 -
Single Number
public class Solution { public int singleNumber(int[] A) { int[] bits = new int[32]; int result = 0; for (int i=0; i<bits.length; i++){ for (int j=0; j<A.length原创 2014-10-03 12:46:30 · 169 阅读 · 0 评论 -
Valid Parentheses
public class Solution { public boolean isValid(String s) { if (s == null || s.isEmpty()) return false; Stack chars = new Stack(); chars.push(s.charAt(0)); for (int原创 2014-10-14 10:23:42 · 173 阅读 · 0 评论 -
Minimum Depth of Binary Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-13 13:15:55 · 291 阅读 · 0 评论 -
Sum Root to Leaf Numbers
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-10-13 12:58:29 · 226 阅读 · 0 评论 -
Search in Rotated Sorted Array II
public class Solution { public boolean search(int[] A, int target) { int l=0; int r=A.length-1; while(l<=r){ int m = (l+r)/2; if (A[m] == target) re原创 2014-10-13 07:47:15 · 124 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
public class Solution { public int maxProfit(int[] prices) { int profit = 0; for(int i=0; i<prices.length-1; i++){ if (prices[i]<prices[i+1]){ profit +=原创 2014-10-03 13:25:57 · 120 阅读 · 0 评论