- 博客(108)
- 资源 (1)
- 收藏
- 关注
原创 475. Heaters
Question - Nested loop, time exceeded. - Binary search. - Note: whenever uses binary search, sort before search. public class Solution { public int findRadius(int[] houses, int[] heaters) {
2017-03-08 10:27:13 314
原创 [Leetcode] 506. Relative Ranks
Question First thought, copy array -> sort array -> reverse sorted array -> nested loop, outer loop go through sorted array, inner loop go through original array and assign value to result when ele
2017-02-21 01:42:07 344
原创 [LeetCode] 406. Queue Reconstruction by Height
1. Convert 2D array to people list 2. Customize a Comparator to sort the people list, which ensure people with lower k should be in the front of the queue + in the case of same k for multiple people,
2016-11-04 09:07:11 404
原创 [LeetCode] 398. Random Pick Index
QUESTION 1. Use a hash map > to store the input array 2. Every time pick method invoke, get the list of index for given target number, get the random list index by rand.((max-min)+1)+min public cla
2016-10-27 09:19:47 256
原创 [LeetCode] 396. Rotate Function
QUESTION O(n^2) approach: public class Solution { public int maxRotateFunction(int[] A) { if(A == null || A.length == 0){ return 0; } int max = Integ
2016-10-26 12:23:43 256
原创 [LeetCode] 395. Longest Substring with At Least K Repeating Characters
QUESTION Thought: Split string by the characters repeating less than k. Recursively call function with the sub string until sub string can not be split. Store the length of the longest un-split sub s
2016-10-25 09:46:09 275
原创 [LeetCode] 393. UTF-8 Validation
QUESTION Implement rules one by one. Keep calm, be patient.. public class Solution { public boolean validUtf8(int[] data) { if(data == null || data.length == 0){ return tr
2016-10-24 06:21:19 416
原创 [LeetCode] 392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, a
2016-10-23 01:35:47 251
原创 [LeetCode] 390. Elimination Game
public class Solution { public int lastRemaining(int n) { if(n < 1){ return 0; } if(n==1){ return 1; } int gap =
2016-10-23 00:53:38 397
原创 [leet code] Pow(x, n)
Question: Implement pow(x, n). 第二次做这道题仍然对以2为基数的解题方式感觉陌生. 直观做法, 超时. 于是只能考虑数值运算两大进阶解法: 1. 两分法 O(logn) 2. 2为基数: 将目标数字拆解成以2为基数的构成方式, 例如 123 = 2^6+2^5+2^4+2^3+2^1+2^0 O(logn) 对于第二种解法: Step1: n为负数
2016-01-03 01:18:43 673
原创 [leet code] Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word:
2014-02-22 03:15:12 3091
原创 [leet code] Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible
2014-02-21 08:15:23 805
原创 [leet code] Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1-
2014-02-20 01:11:29 683
原创 [leet code] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","
2014-02-19 23:07:28 880
原创 [leet code] Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". =========== Analysis: Idea is to implement the manual calculation process. Whic
2014-02-14 07:56:35 752
原创 [leet code] Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy t
2014-02-14 00:37:43 2869
原创 [leet code] Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. ============ Analysis: Exact the same idea as [leet cod
2014-02-13 01:55:25 831
原创 [leet code] Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. ================= A scary problem at the very beginning
2014-02-13 01:29:02 2017
原创 [leet code] Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.Th
2014-02-12 00:26:50 648
原创 [leet code] Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit st
2014-02-11 23:39:37 669
原创 [leet code] Insertion Sort List
Sort a linked list using insertion sort. ===================== Analysis: Idea is to implement the insertion sort from array to linked list. Basic idea of insertion sort is that, examine array el
2014-02-11 03:42:12 1001
原创 [leet code] Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3
2014-02-10 13:39:29 1709
原创 [leet code] Partition List Pow(x, n)
Implement pow(x, n). =========== Analysis: The most basic idea, x multiply itself n time and return. However, this strait forward approach got time excessed. O(n). Note that O(logn) Accordingl
2014-02-10 00:55:07 626
原创 [leet code] Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of
2014-02-09 06:46:36 1933
原创 [leet code] Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [
2014-02-08 10:25:40 919
原创 [leet code] Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited numb
2014-02-08 06:40:00 750
原创 [leet code] Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary
2014-02-08 00:57:21 1316
原创 [leet code] Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1
2014-02-08 00:13:35 952
原创 [leet code] Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].
2014-02-07 01:30:01 714
原创 [leet code] Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as
2014-02-06 04:54:46 790
原创 [leet code] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3
2014-02-05 07:48:58 815
原创 [CrackCode] 5.5 Determine the number of bits required to convert integer A to integer B
Write a function to determine the number of bits required to convert integer A tointeger B Input: 31, 14Output: 2 ========= Analysis: Basic idea of mine is to compare each digit
2014-02-05 06:23:14 669
原创 [CrackCode] 5.3 Print the next smallest and next largest number
Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation ================ Analysis: Assume that we are given 1
2014-02-05 05:57:16 873
原创 [CrackCode] 5.2 Print the binary representation
Given a (decimal - e g 3.72) number that is passed in as a string, print the binary rep-resentation If the number can not be represented accurately in binary, print “ERROR” ============ A
2014-02-05 00:18:55 1774
原创 [CrackCode] 5.1 Set all bits between i and j in N equal to M
You are given two 32-bit numbers, N and M, and two bit positions, i and j Write amethod to set all bits between i and j in N equal to M (e g , M becomes a substring ofN located at i and starting at
2014-02-04 07:27:42 997
原创 [leet code] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order.The solution set must not contain dupli
2014-02-04 02:57:33 658
原创 [CrackCode] 4.4 Creates a linked list of all the nodes at each depth of a given tree
Given a binary search tree, design an algorithm which creates a linked list of all thenodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists) ==============
2014-02-03 06:15:05 1489
原创 [leet code] Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially fille
2014-02-03 01:32:12 5687
原创 [leet code] Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine i
2014-01-29 08:10:09 800
原创 [leet code] Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the
2014-01-29 01:15:44 1559
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人