- 博客(237)
- 收藏
- 关注
原创 LeetCode 299. Bulls and Cows(java)
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that
2018-02-05 10:47:47 202
原创 LeetCode 163. Missing Ranges(java)
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges.For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2"
2018-02-05 10:45:41 686
原创 LeetCode 228. Summary Ranges(java)
Given a sorted integer array without duplicates, return the summary of its ranges.Example 1:Input: [0,1,2,4,5,7]Output: ["0->2","4->5","7"]Example 2:Input: [0,2,3,4,6,8,9]Output: ["0","2->4","6","8
2018-02-05 09:50:13 602
原创 LeetCode 38. Count and Say(java)
The count-and-say sequence is the sequence of integers with the first five terms as following:111211211111221 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as
2018-02-05 09:16:36 831
原创 LeetCode 80. Remove Duplicates from Sorted Array II(java)
Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five elemen
2018-02-04 09:51:52 205
原创 LeetCode 26. Remove Duplicates from Sorted Array(java)
Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this by modifying the i
2018-02-04 09:50:05 114
原创 LeetCode 383. Ransom Note(java)
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; oth
2018-02-04 09:46:32 322
原创 LeetCode 242. Valid Anagram(java)
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note: You may assume the str
2018-02-04 09:36:02 306
原创 LeetCode 621. Task Scheduler(java)
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be
2018-02-04 09:22:54 753
原创 532. K-diff Pairs in an Array(java)
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in
2018-02-04 09:07:33 256
原创 LeetCode 341. Flatten Nested List Iterator(java)
Given a nested list of integers, implement an iterator to flatten it.Each element is either an integer, or a list – whose elements may also be integers or other lists.Example 1:Given the list [[1,1],2
2018-02-04 06:52:31 753
原创 LeetCode 150. Evaluate Reverse Polish Notation(java)
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1", "+", "3", "*"]
2018-02-04 06:11:03 377
原创 LeetCode Maximum Product of Three Number s(java)
Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6Example 2:Input: [1,2,3,4]Output: 24思路:为了找找到最大的乘积,我们需要在遍历数组的同时mai
2018-02-04 05:22:26 156
原创 LeetCode 724. Find Pivot Index(java)
Given an array of integers nums, write a method that returns the “pivot” index of this array.We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the
2018-02-04 04:42:22 431
原创 LeetCode 205. Isomorphic Strings(java)
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another chara
2018-02-03 14:12:03 504
原创 LeetCode 295. Find Median from Data Stream(java)
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.Examples: [2,3,4] , the median is 3
2018-02-03 13:48:34 631
原创 LeetCode 239. Sliding Window Maximum(java)
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window mo
2018-02-03 10:38:56 677
原创 LeetCode 346. Moving Average from Data Stream(java)
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.For example,MovingAverage m = new MovingAverage(3);m.next(1) = 1m.next(10) = (1 + 10)
2018-02-03 06:56:33 541
原创 LeetCode 283. Move Zeroes(java)
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your funct
2018-01-30 15:06:55 305
原创 LeetCode 80. Remove Duplicates from Sorted Array II(java)
Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five element
2018-01-30 14:50:46 298
原创 LeetCode 26. Remove Duplicates from Sorted Array(java)
Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this by modifying the i
2018-01-30 14:34:54 98
原创 LeetCode 27. Remove Element(java)
Given an array and a value, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place w
2018-01-30 13:47:29 321
原创 LeetCode 62. Unique Paths(java)
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the botto
2018-01-27 11:00:54 629
原创 LeetCode 312. Burst Balloons(java)
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[lef
2018-01-27 10:46:05 488
原创 LeetCode 218. The Skyline Problem(java)
A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as
2018-01-27 08:41:31 934
原创 LeetCode 204. Count Primes(java)
Description:Count the number of prime numbers less than a non-negative number, n.解法一:每次判断i是不是prime,然后用一个dp[]数组来存结果,然后由于每次都要判断prime的时间太长,所以被TLE了。时间复杂度为O(n^2),空间复杂度为O(n)。public int countPrimes(int n) {
2018-01-26 09:24:49 622
原创 LeetCode 240. Search a 2D Matrix II(java)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right. Integers in each co
2018-01-26 08:49:16 235
原创 LeetCode 406. Queue Reconstruction by Height(java)
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this p
2018-01-26 08:29:30 467
原创 LeetCode 279. Perfect Squares(java)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return
2018-01-26 07:20:27 512
原创 LeetCode 448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could you
2018-01-26 06:32:42 88
原创 LeetCode 142. Linked List Cycle II(java)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up: Can you solve it without using extra space?解法:快慢指针,每次慢指针走一
2018-01-25 12:08:31 217
原创 非比较排序之桶排序(bucket sort)
1. 桶排序如果输入的数组里没有重复,那么一个普通数组就可以,这个数组的大小为原数组中的max - min + 1。代码如下。public static int[] bucketSort(int[] nums) { if (nums.length <= 1) return nums; int min = Integer.MAX_VALUE, max = Integer
2018-01-25 08:27:45 197
原创 LeetCode 347. Top K Frequent Elements(java)
Given a non-empty array of integers, return the k most frequent elements.For example, Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is always valid, 1 ≤ k ≤ number of unique ele
2018-01-25 07:09:30 832
原创 LeetCode 95. Unique Binary Search Trees II
Given an integer 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
2018-01-25 03:08:43 149
原创 LeetCode 96. Unique Binary Search Trees(java)
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ /
2018-01-24 07:54:50 127
原创 LeetCode 85. Maximal Rectangle(java)
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Retu
2018-01-23 06:45:56 460
原创 LeetCode 84. Largest Rectangle in Histogram(java)
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each
2018-01-22 09:44:12 459
原创 LeetCode 221. Maximal Square(java)
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Return 4
2018-01-22 08:24:22 524
原创 LeetCode 161. One Edit Distance(java)
Given two strings S and T, determine if they are both one edit distance apart. 注意:当s和t完全相同时,需要返回false.最快的解法:先调整s和t的长度,使得s的长度小于t,解法中的解决方法很巧妙,通过交换参数,再次调用函数实现~。然后开始判断m和n的长度,如果相差大于1则返回false。遍历两个字符串,当出现两个字
2018-01-22 07:51:43 326
原创 LeetCode 72. Edit Distance(java)
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:a) In
2018-01-22 07:13:07 555
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人