自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 Leetcode 257. Binary Tree Paths

DFSclass Solution: """ @param root: the root of the binary tree @return: all root-to-leaf paths """ def binaryTreePaths(self, root): # write your code here if no...

2018-09-13 07:35:26 136

原创 Leetcode 165. Compare Version Numbers

class Solution: """ @param version1: the first given number @param version2: the second given number @return: the result of comparing Find out the largest bit of two version numbe...

2018-09-13 02:26:21 128

原创 Leetcode 191. Number of 1 Bits

"""Using bit operator AND(&) to compare the number with 1,if the last bit of the number is 1 which means we found a 1 bit, then update the count by 1;if the last bit of the number is 0, no nee...

2018-09-13 01:42:04 132

原创 Leetcode 565. Array Nesting

"""max = Max(current_max, max)Use a flag array to mark if current node has been visited before.If it is visited, then we can skip it. Complexity is O(n) as we only go through the array once."""...

2018-09-10 03:20:46 135

原创 Leetcode 396. Rotate Function

O(n) Math solution./** * F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1] * = 0 * Bk-1[n-1] + 1 * Bk-1[0] + ... + (n-1) * Bk-1[n-2] * F(k-1) = 0 * Bk-1[0] + 1 * Bk-1[1] + ... +

2017-04-06 12:24:25 273

原创 Leetcode 119. Pascal's Triangle II

public class Solution { public List getRow(int rowIndex) { List list = new ArrayList(); if (rowIndex < 0) return list; for (int i = 0; i < rowIndex + 1; i++) { list

2017-04-06 11:15:39 200

原创 Leetcode 118. Pascal's Triangle

public class Solution { public List> generate(int numRows) { List> res = new ArrayList>(); if (numRows < 0) { return res; } List level

2017-04-06 11:14:52 201

原创 Leetcode 347. Top K Frequent Elements

Data structure used, hash table and min heap.A hashmap to save every element along with its frequency for the input data.A min heap to save top k most frequent map entry, comparing by map.value th

2017-04-06 03:38:05 211

转载 Leetcode 160. Intersection of Two Linked Lists

Assume the length of the intersection part is x, length of list a l1, and length of list b is l2.Then l1 + l2 = l2 + l1 => l1 + l2 - x = l2 + l1 - x.If tow lists have no intersection, then two poi

2017-04-03 06:10:47 256

原创 Leetcode 414. Third Maximum Number

public class Solution { public int thirdMax(int[] nums) { // Hashset to remove all duplicate elements HashSet set = new HashSet(); for (int num : nums) set.add(

2017-04-03 00:40:25 196

原创 Leetcode 155. Min Stack

O(nlogN) PriorityQueue.public class MinStack { PriorityQueue minHeap; Stack stack; /** initialize your data structure here. */ public MinStack() { minHeap = new PriorityQueue

2017-04-02 04:34:57 313

原创 Leetcode 380. Insert Delete GetRandom O(1)

Using ArrayList's get function to implement getRandom() in constant time, and a hashmap to save the pair.So we need to make sure the index of the last element always equals to the size of the list

2017-04-02 02:54:23 401

原创 Leetcode 42. Trapping Rain Water

/** * To sum up total trapping water, we need to know how many water can be trapped by each bar after raining. * The amount of trapping water for each bar is calculated by min(maxSeenLeft, maxSeenRi

2017-04-01 06:36:35 250

原创 Leetcode 206. Reverse Linked List

Iterative approach.public class Solution { public ListNode reverseList(ListNode head) { ListNode newHead = null; while (head != null) { ListNode next = head.next;

2017-04-01 05:35:30 197

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

转载 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 175

原创 Leetcode 235. Lowest Common Ancestor of a Binary Search Tree

// Recursive approachpublic 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 177

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

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

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

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

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

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

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

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

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

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

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

转载 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 182

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

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

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

转载 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 269

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

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

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

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

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

转载 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 148

转载 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 218

空空如也

空空如也

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

TA关注的人

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