- 博客(83)
- 收藏
- 关注
转载 Leetcode 415. Add Strings
public class Solution { public String addStrings(String num1, String num2) { StringBuilder sb = new StringBuilder(); int carry = 0; int i = num1.length()-1, j = num2.length
2017-01-29 10:26:59 119
转载 Leetcode 453. Minimum Moves to Equal Array Elements
/** * sum is the sum of the array, min is the minimum of the array, f is the final value of the m moves * m is # of moves, n is the size of the array * sum + m(n-1) = f*n (1) * min + m = f
2017-01-29 09:37:03 213
原创 Leetcode 28. Implement strStr()
// naive O(n^2) solutionpublic class Solution { public int strStr(String haystack, String needle) { if (needle.length() == 0) return 0; if (haystack.length() < needle.length()) re
2017-01-29 08:18:13 128
原创 Leetcode 125. Valid Palindrome
/** * Character methods. * isLetter(), isDigit(), isLetterOrDigit() * First check if characters are alphanumberic, * then check if characters are equal. */ public class Solution { public boo
2017-01-29 07:37:53 180
原创 Leetcode 234. Palindrome Linked List
O(n) time and space.public class Solution { public boolean isPalindrome(ListNode head) { // need a stack to save half of the list Stack stack = new Stack<>(); //
2017-01-27 12:57:39 141
原创 Leetcode 344. Reverse String
public class Solution { public String reverseString(String s) { StringBuilder sb = new StringBuilder(s); int low = 0, high = s.length()-1; while (low < high) {
2017-01-27 12:21:22 149
原创 Leetcode 345. Reverse Vowels of a String
public class Solution { public String reverseVowels(String s) { HashSet hs = new HashSet<>(); hs.add('a'); hs.add('e'); hs.add('i'); hs.add('o'); hs
2017-01-27 12:14:50 143
原创 Leetcode 141. Linked List Cycle
Two pointers, one is slow and the other is fast.Note that the stop condition is fast.next != null && fast.next.next != nullpublic class Solution { public boolean hasCycle(ListNode head) {
2017-01-27 06:22:40 171
原创 Leetcode 287. Find the Duplicate Number
/** * Pigenhold principle and binary search. * All integers are between [1, n], * choose mid as (1+n)/2, then count the number of integers that are <= mid in the array * if the number is greater t
2017-01-27 05:31:27 168
原创 Leetcode 230. Kth Smallest Element in a BST
O(n).public class Solution { public int kthSmallest(TreeNode root, int k) { // save the pre-order of the tree // from left to right, return kth element in the list List r
2017-01-27 04:33:24 141
原创 222. Count Complete Tree Nodes
T(n) = 2T(n/2) + logn = O(logn*logn)public class Solution { public int countNodes(TreeNode root) { if (root == null) return 0; // count the height for the left most leaf and righ
2017-01-27 04:19:39 169
原创 Leetcode 50. Pow(x, n)
Divide and conquer.public class Solution { public double myPow(double x, int n) { if (n == 0) return 1; double half = myPow(x, n/2); if (n%2 == 0) return half * half;
2017-01-26 12:44:00 137
原创 Leetcode 69. Sqrt(x)
public class Solution { public int mySqrt(int x) { long low = 0, high = x; while (low + 1 < high) { long mid = low + (high-low)/2; if (mid * mid > x)...
2017-01-26 12:23:43 142
原创 Leetcode 436. Find Right Interval
TreeMap is a Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys.The time complexity for containsKey, get, put and removeoperations ar
2017-01-26 09:32:11 265
原创 Leetcode 441. Arranging Coins
Math. Solve the quadratic function k/2(k+1) k public class Solution { public int arrangeCoins(int n) { // math return (int) (-1+Math.sqrt(1+8*(double)n))/2; }}Binary searc
2017-01-22 10:41:16 163
原创 Leetcode 374. Guess Number Higher or Lower
Note that the param for guess() is the number you need to guess.e.g. guess(5) = 1 when the number you need to guess is 6. /** * We can easily figure out a O(n) solution which calls guess() from
2017-01-22 09:43:00 177
原创 Leetcode 278. First Bad Version
public class Solution extends VersionControl { public int firstBadVersion(int n) { int low = 1, high = n, mid = 0; while (low + 1 < high) { mid = low + (high - low) / 2
2017-01-22 09:18:45 155
原创 Leetcode 215. Kth Largest Element in an Array
/** * How quick sort works. * First choose a pivot, say the first element of the array, * set two pointer low and high point to the start and end of the array, * from high to low, find a element t
2017-01-22 05:54:57 166
原创 Leetcode 240. Search a 2D Matrix II
/** * Start from the top-right corner, check * if target > curr means we need to move to the next row b/c curr is the largest element in the row * if target < curr means we need to move to the prev
2017-01-22 03:23:55 153
原创 Leetcode 199. Binary Tree Right Side View
public class Solution { public List rightSideView(TreeNode root) { List ret = new ArrayList<>(); // the depth of root is 0 rightView(0, root, ret); return ret;
2017-01-20 11:33:08 206
原创 Leetcode 108. Convert Sorted Array to Binary Search Tree
public class Solution { public TreeNode sortedArrayToBST(int[] nums) { return helper(0, nums.length-1, nums); } // requires a balanced binary search tree, every time choosing
2017-01-20 10:45:56 154
原创 Leetcode 113. Path Sum II
public class Solution { public List> pathSum(TreeNode root, int sum) { List> ret = new ArrayList<>(); helper(root, sum, 0, new ArrayList<>(), ret); return ret; }
2017-01-20 10:20:26 141
原创 Tail recursion and normal recursion
Tail recursive will return the result immediately when the recursion reach the base case.However, for normal recursion, when it reaches the base case it will return to the previous callagain and a
2017-01-20 08:55:59 201
原创 Leetcode 114. Flatten Binary Tree to Linked List
public class Solution { public void flatten(TreeNode root) { if (root == null) return; TreeNode left = root.left; TreeNode right = root.right; fla
2017-01-20 08:41:32 113
原创 Leetcode 116. Populating Next Right Pointers in Each Node
public class Solution { public void connect(TreeLinkNode root) { if (root == null) return; helper(root.left, root.right); } public static void helper(TreeLinkNode l, T
2017-01-20 04:23:42 137
转载 Leetcode 394. Decode String
public class Solution { public String decodeString(String s) { Stack intStack = new Stack<>(); Stack strStack = new Stack<>(); StringBuilder cur = new StringBuilder();
2017-01-20 03:57:58 167
原创 Leetcode 129. Sum Root to Leaf Numbers
public class Solution { public int sumNumbers(TreeNode root) { return helper(root, 0); } public static int helper(TreeNode r, int tmp) { if (r == null) return 0;
2017-01-20 01:29:27 146
原创 Partition Problem
Given a set of positive integers, find if the set can be partitioned into two subsets such that the sum of these two subsets are equal. DFS approach. Sort the array in an increasing order. Create tw
2017-01-19 02:15:26 904
转载 Leetcode 473. Matchsticks to Square
More information. public class Solution { public boolean makesquare(int[] nums) { if (nums == null || nums.length < 4) return false; int sum = 0; for (int num : nums) sum +=
2017-01-18 11:39:37 202
原创 Leetcode 200. Number of Islands
public class Solution { public int numIslands(char[][] grid) { int row = grid.length; if (row == 0) return 0; int col = grid[0].length; int count = 0;
2017-01-18 10:35:55 150
原创 Eularian Path
For Eulerian graph, every edge is visited exactly once.Eulerian graph, the number of odd degree edge is either 0 or 2.Eulerian circuit, all vertex's degree are even. Algorithm for finding a Eul
2017-01-18 05:21:15 236
转载 Leetcode 332. Reconstruct Itinerary
public class Solution { Map> flights; LinkedList path; public List findItinerary(String[][] tickets) { flights = new HashMap<>(); path = new LinkedList<>(); for (
2017-01-18 05:09:40 196
原创 Leetcode 337. House Robber III
Naive solution. There are many overlapping sub problems.e.g. if we rob(root), we need to know rob(left), rob(right), rob(left.left), rob(left.right), ...then, say if we want to rob(root.left), the
2017-01-18 03:20:42 133
原创 Leetcode 98. Validate Binary Search Tree
O(n) define minimum and maximum every recur.public class Solution { public boolean isValidBST(TreeNode root) { return helper(root, null, null); } public static boolean helpe
2017-01-16 10:31:45 139
原创 Leetcode 111. Minimum Depth of Binary Tree
public class Solution { public int minDepth(TreeNode root) { if (root == null) return 0; // take care of the trees that only have left or right child if (root.left == null)
2017-01-16 09:21:05 154
原创 Leetcode 110. Balanced Binary Tree
public class Solution { public boolean isBalanced(TreeNode root) { // do an inorder traverse if (root == null) return true; if (Math.abs(getHeight(root.left) - getHeight(r
2017-01-16 09:06:08 135
原创 Leetcode 112. Path Sum
public class Solution { public boolean hasPathSum(TreeNode root, int sum) { int tmp = 0; return helper(root, tmp, sum); } // inorder traversing public static boole
2017-01-16 08:42:11 141
原创 Leetcode 257. Binary Tree Paths
public class Solution { public List binaryTreePaths(TreeNode root) { List ret = new ArrayList<>(); helper(root, ret, ""); return ret; } public static void help
2017-01-15 13:44:27 162
原创 Leetcode 104. Maximum Depth of Binary Tree
public class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; }}
2017-01-15 11:43:18 181
转载 Leetcode 101. Symmetric Tree
public class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) return true; return helper(root.left, root.right); } public static boolean helper
2017-01-15 11:28:05 144
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人