
LeetCode Questions
文章平均质量分 72
LeetCode Questions Summary, including analysis, solution on Java & C++
Mavs
CSE
展开
-
[LeetCode 223] Rectangle Area
Find the total area covered by two rectilinear rectangles in a2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the total a原创 2015-09-24 03:27:17 · 3179 阅读 · 0 评论 -
[LeetCode 284] Peeking Iterator
Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be re原创 2015-09-23 07:30:31 · 3236 阅读 · 0 评论 -
[LeetCode 274] H-index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.According to the definition of h-index on Wikipedia: "A原创 2015-09-23 08:32:06 · 2881 阅读 · 0 评论 -
[LeetCode 275] H-Index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?Hint:Expected runtime complexity is inO(log n) and the input is sorted.原创 2015-09-23 08:48:02 · 1959 阅读 · 0 评论 -
[LeetCode 240] Search a 2D matrix II
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原创 2015-09-23 14:10:08 · 836 阅读 · 0 评论 -
[LeetCode 205] Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to gett.All occurrences of a character must be replaced with another原创 2015-09-24 02:50:32 · 1987 阅读 · 0 评论 -
[LeetCode 241] Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are+, - and *.Example 1I原创 2015-09-18 14:42:44 · 3064 阅读 · 0 评论 -
[LeetCode 279] Perfect Squares
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 because12 = 4 + 4 + 4; given n = 13,原创 2015-09-18 13:11:08 · 3500 阅读 · 1 评论 -
[LeetCode 230] Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find thekth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the BST原创 2015-09-18 12:04:52 · 2216 阅读 · 0 评论 -
[LeetCode 273] Integer to English Word
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.For example,123 -> "One Hundred Twenty Three"12345 -> "Twelve Thousand Thre原创 2015-09-18 07:21:27 · 6177 阅读 · 0 评论 -
[LeetCode 215] Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.N原创 2015-09-15 14:31:29 · 633 阅读 · 0 评论 -
[LeetCode 166] Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.For原创 2015-09-16 13:36:53 · 873 阅读 · 0 评论 -
[LeetCode 216] Combination Sum III
Find all possible combinations of k numbers that add up to a numbern, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers wit原创 2015-09-16 15:35:39 · 2430 阅读 · 0 评论 -
[LeetCode 238] Product of Array Except Self
Given an array of n integers where n > 1, nums, return an arrayoutput such that output[i] is equal to the product of all the elements ofnums except nums[i].Solve it without division and in O(n).原创 2015-09-17 03:57:10 · 1622 阅读 · 0 评论 -
[LeetCode 221] Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 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 0R原创 2015-09-17 07:27:23 · 1398 阅读 · 0 评论 -
[LeetCode206] Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as原创 2015-09-03 15:09:32 · 1495 阅读 · 0 评论 -
[LeetCode 207] Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as原创 2015-09-03 13:47:45 · 1880 阅读 · 0 评论 -
[LeetCode179] Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve原创 2015-09-02 13:24:36 · 1178 阅读 · 1 评论 -
[LeetCode202] Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2015-09-02 12:36:58 · 1892 阅读 · 3 评论 -
[LeetCode 191] Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000原创 2015-09-02 12:13:48 · 1478 阅读 · 1 评论 -
[Leetcode172]Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.solution:zero comes from 2*5, and number of 2 is less than 5. So原创 2015-09-02 07:01:25 · 912 阅读 · 1 评论 -
[LeetCode 229] Majority element II
Given an integer array of size n, find all elements that appear more than⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.Solution:At most has two elements in the r原创 2015-09-02 06:45:13 · 1687 阅读 · 0 评论 -
[Leetcode 264] Ugly Number II
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first原创 2015-09-02 00:23:05 · 2793 阅读 · 0 评论 -
[Leetcode 263] Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly si原创 2015-09-02 00:19:47 · 1204 阅读 · 0 评论 -
[LeetCode 153] Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element原创 2015-09-02 00:17:09 · 1250 阅读 · 0 评论 -
[LeetCode]Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest原创 2014-09-25 10:14:16 · 5469 阅读 · 1 评论 -
[LeetCode84]Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before c原创 2014-07-01 07:08:12 · 798 阅读 · 0 评论 -
[LeetCode44]Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover t原创 2014-07-01 06:54:32 · 1289 阅读 · 0 评论 -
[LeetCode]Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""3原创 2014-07-01 06:12:38 · 2896 阅读 · 1 评论 -
[LeetCode]Simplify Path
Analysis:利用栈的特性,如果sub string element1. 等于“/”,跳过,直接开始寻找下一个element2. 等于“.”,什么都不需要干,直接开始寻找下一个element3. 等于“..”,弹出栈顶元素,寻找下一个element4. 等于其他,插入当前elemnt为新的栈顶,寻找下一个element最后,再根据栈的内容,重新拼path。这原创 2014-07-01 05:54:58 · 850 阅读 · 0 评论 -
[LeetCode]Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr原创 2014-07-01 02:20:24 · 694 阅读 · 0 评论 -
[LeetCode132]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-07-01 00:34:13 · 781 阅读 · 0 评论 -
[LeetCode]Evaluate Reverse Polish Notation
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",原创 2014-06-30 23:56:33 · 784 阅读 · 0 评论 -
[LeetCode]Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.原创 2014-06-30 05:19:30 · 830 阅读 · 0 评论 -
[LeetCode]LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if原创 2014-06-30 01:24:35 · 2719 阅读 · 1 评论 -
[LeetCode]Word BreakII
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens = "原创 2014-06-27 00:03:28 · 903 阅读 · 0 评论 -
[LeetCode]Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2014-06-26 23:13:14 · 1043 阅读 · 0 评论 -
[LeetCode]Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using ext原创 2014-06-26 22:46:38 · 730 阅读 · 0 评论 -
[LeetCode]Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to原创 2014-06-26 03:49:29 · 2732 阅读 · 2 评论 -
[LeetCode]Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for ea原创 2014-06-26 01:38:32 · 3045 阅读 · 0 评论