自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 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 213

原创 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 159

原创 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 150

原创 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 171

原创 Trapping Rain Water

public class Solution { public int trap(int[] A) { int[] maxL = new int[A.length]; int[] maxR = new int[A.length]; int max = 0; // Go left and find left max height

2014-10-14 09:57:46 229

原创 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 201

原创 Length of Last Word

public class Solution { public int lengthOfLastWord(String s) { int length = 0; for (int i=s.length()-1; i>=0; i--){ if (s.charAt(i) != ' '){ length++;

2014-10-13 13:35:29 185

原创 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 286

原创 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 221

原创 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 163

原创 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 193

原创 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 139

原创 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 166

原创 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 138

原创 Path Sum II

/** * 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 10:04:11 144

原创 Path Sum

/** * 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:41:00 153

原创 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 140

原创 Spiral Matrix II

public class Solution { public int[][] generateMatrix(int n) { // 0 up, 1 down, 2 left, 3 right int val = 1; int pre = 3; int i = 0; int j = 0; int[

2014-10-13 09:20:13 141

原创 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 206

原创 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 114

原创 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 119

原创 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 125

原创 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 132

原创 Binary Tree Level Order Traversal II

/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { // Tr

2014-10-13 05:52:44 180

原创 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 141

原创 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 136

原创 Pascal's Triangle

public class Solution { public List> generate(int numRows) { List> rows = new ArrayList>(); for(int i=0; i<numRows; i++){ List row = new ArrayList(); rows.a

2014-10-12 10:41:02 132

原创 Unique Paths

public class Solution { public int uniquePaths(int m, int n) { int[][] matrix = new int[m][n]; // Initialize for (int i=0; i<m; i++){ matrix[i][0] = 1;

2014-10-09 13:12:26 113

原创 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 161

原创 Sort Colors

public class Solution { public void sortColors(int[] A) { int[] count = new int[3]; for(int i=0; i<A.length; i++){ count[A[i]]++; } for(int i=0; i<count

2014-10-08 12:52:34 225

原创 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 122

原创 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 124

原创 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 195

原创 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 214

原创 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 142

原创 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 112

原创 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 137

原创 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 128

原创 Climbing Stairs

public class Solution { public int climbStairs(int n) { int[] count = new int[n+1]; count[0] = 1; count[1] = 1; for(int i=2; i<=n; i++){ count[i] = coun

2014-10-03 15:26:58 203

原创 Remove Duplicates from Sorted List

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public

2014-10-03 15:22:11 124

空空如也

空空如也

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

TA关注的人

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