- 博客(27)
- 收藏
- 关注
原创 Leetcode 17. Letter Combinations of a Phone Number
public class Solution { public List letterCombinations(String digits) { String[] mapping = {"", "", "abc", "def", "ghi", "jkl", "mon", "pqrs", "tuv", "wxyz"}; List res = new ArrayL
2017-03-31 02:06:58 240
转载 Leetcode 236. Lowest Common Ancestor of a Binary Tree
public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) return root; TreeNode
2017-03-30 08:10:38 184
原创 Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
// Recursive approach public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root.val > p.val && root.val > q.val) return lo
2017-03-30 02:53:03 185
原创 Leetcode 449. Serialize and Deserialize BST
public class Codec { // Encodes a tree to a single string. public String serialize(TreeNode root) { // If tree is empty just return a null string if (root == null)
2017-03-29 02:22:13 683
原创 Leetcode 297. Serialize and Deserialize Binary Tree
public class Codec { private static final String NULL = "null"; private static final String SPLT = " "; // BFS saves nodes level by level public String serialize(TreeNode root) {
2017-03-25 08:11:20 218
原创 Leetcode 496. Next Greater Element I
public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { if (nums == null || findNums == null) { return null; } // No dupl
2017-03-18 05:41:48 217
原创 Leetcode 412. Fizz Buzz
public class Solution { public List fizzBuzz(int n) { List res = new ArrayList<>(); for (int i=1; i<=n ; i++) { if (i%3==0 && i%5==0) { res.add("FizzBuz
2017-03-18 05:02:38 194
原创 Leetcode 476. Number Complement
public class Solution { public int findComplement(int num) { int res = 0; int carry = 1; while (num > 0) { int temp = (num%2 == 0) ? 1 : 0; res += t
2017-03-18 04:56:38 208
原创 Leetcode 461. Hamming Distance
public class Solution { private final int LENGTH = 32; public int hammingDistance(int x, int y) { int[] bits_x = new int[LENGTH]; int[] bits_y = new int[LENGTH];
2017-03-18 04:54:53 184
原创 Leetcode 500. Keyboard Row
public class Solution { public String[] findWords(String[] words) { HashMap map = new HashMap(); // put all characters along with their rows into the map map.put('q', 1);
2017-03-18 04:54:01 252
原创 O(n^1/2) isPrime
/** O(n^0.5) solution * Only need to consider if n can be divided by numbers from 2 to sqrt(n). * If n % even == 0, not prime; * If n % odd == 0, not prime; * 1 is not a prime
2017-03-16 05:55:28 904
原创 Java Arrays.sort() on objects
class Checker implements Comparator{ @Override public int compare(Player p1, Player p2 ) { if (p1.score == p2.score) { return p1.name.compareTo(p2.name); } else {
2017-03-15 12:59:29 217
原创 Leetcode 208. Implement Trie (Prefix Tree)
public class Trie { public static class TrieNode { private final int N = 26; private TrieNode[] links; private boolean isEnd = false;
2017-03-15 10:09:33 331
原创 Leetcode 378. Kth Smallest Element in a Sorted Matrix
O(mlogn), m is number of columns and n is number of rows. O(m*n) space. Merge the matrix into an array, return kth element. public class Solution { public int kthSmallest(int[][] matrix, int k)
2017-03-10 11:58:48 237
转载 Leetcode 295. Find Median from Data Stream
Two heap to save the smaller half and larger half number respectively. We need to keep heap as balanced while insertion, i.e., the different of two heaps' size must be If we find the insertion wil
2017-03-10 06:27:44 199
原创 Leetcode 23. Merge k Sorted Lists
Given a list array, for example, l1 l2 l3 l4 l5 l6 l7 l8. Intuitive solution, merge l1 and l2 into new list l1', then merge l1' and l3 into l2', similarly, merge l2' and l4 into l3'. Time complexity
2017-03-10 03:47:53 219
原创 Leetcode 264. Ugly Number II
An approach is to check every number if it is an ugly number, until we meet the nth ugly number. An efficient way is to construct and enumerate all n ugly number and return the last one. public clas
2017-03-10 02:19:46 168
原创 Leetcode 263. Ugly Number
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. Which means an ugly number is 2^i * 3^j * 5^k * 1 (i,j,k are non-negative integers). Therefore, we can keep dividing the g
2017-03-08 12:08:40 171
转载 Build a min heap for a given a given array
public class Solution { public void heapify(int[] A) { // only heapify the parent nodes which range is [0, A.length-1/2] // from bottom to up, call minHeapify() for (int i=
2017-03-08 11:44:00 281
原创 Leetcode 128. Longest Consecutive Sequence
A hashset to save all unique numbers in the array. left indicates the number that is 1 less that target, right indicate the number that is 1 greater than target, here, target is nums[i] keep decreas
2017-03-08 08:50:09 186
原创 Leetcode 138. Copy List with Random Pointer
Using a hashmap to save the pairs, then set next and random link for new created nodes. O(n) time and space complexity. public class Solution { public RandomListNode copyRandomList(RandomListN
2017-03-08 04:37:57 176
原创 Subarray Sum Closest
Step 1. Calculate the prefix sum for each position in the array. Step 2. Sort the prefix sum array. Step 3. Compare every prefix sum and distance between two adjacent prefix sum, choose the minimum
2017-03-08 03:34:59 188
原创 Sub Array Sum to 0
Using a hashmap to save the sum and index pair, initialize the map to sum the integers in the array, if we meet a sum that exits in the map, means we found a subarray that sums to 0, return the beg
2017-03-07 12:44:56 280
原创 Leetcode 146. LRU Cache
public class LRUCache { // double linked list private class Node { Node prev; Node next; int key; int value; public Node (int key, int value) {
2017-03-07 11:22:50 188
转载 Leetcode 155. Min Stack
Using two stacks, one to save elements, and the other to save the minimum. Therefore, there is a mapping between an element and current minimum. stack min -1
2017-03-07 04:37:30 160
转载 Leetcode 85. Maximal Rectangle
/** * Scan row by row. * Calculate the histogram for each row. e.g. for row 2 the histogram is 2, 0, 2, 1, 1 * Calculate the largest area of this histogram. */ public class Solution { public
2017-03-03 12:55:46 229
转载 Leetcode 84. Largest Rectangle in Histogram
/** * Basic idea is using a stack to record the ascending heights in the given array. * Every time meet a height that is smaller than the previous one, pop out the statck, find the maximum area from
2017-03-03 11:45:46 180
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人