Count of Smaller Numbers After Self

2018-05-20
阅读 2 分钟
1.5k
这道题第一反应用Brute force方法比较直接,两个For Loop解决,时间复杂度是O(n^2),很显然我们需要用时间复杂度更低的方法。

Subarray Sum Equals K

2018-04-03
阅读 2 分钟
1.4k
Given an array of integers and an integer k, you need to find the total number of >continuous subarrays whose sum equals to k. Example 1: {代码...} Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is >[-...

[LeetCode]Graph Valid Tree

2016-01-07
阅读 4 分钟
3.5k
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree. For example: Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true. Given n = 5 and edges = [[0, 1], [1, 2], [2,...

[LeetCode]Word Ladder, Word Ladder II

2016-01-04
阅读 5 分钟
3.6k
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a time Each intermediate word must exist in the word list For example, Given:beginWord = "hit"endWord ...

[LeetCode]Binary Tree Maximum Path Sum

2016-01-04
阅读 3 分钟
2k
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For example:Given the below binary tree, {代码...} Return 6.

[LeetCode]Maximal Rectangle

2016-01-03
阅读 3 分钟
3.4k
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

[LeetCode]Range Sum Query 2D - Mutable

2016-01-03
阅读 3 分钟
5.8k
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8. Example: ...

[LeetCode]Serialize and Deserialize Binary Tree

2016-01-03
阅读 3 分钟
4.2k
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serializ...

[LeetCode]Word Break, Word Break II

2016-01-02
阅读 4 分钟
6.4k
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"].

[LeetCode]Trapping Rain Water

2016-01-01
阅读 5 分钟
2.6k
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6...

[LeetCode]Read N Characters Given Read4

2016-01-01
阅读 3 分钟
2.9k
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the read4 API, implement the function int read(char *buf, int n) that reads n charac...

[LeetCode]Find Peak Element

2016-01-01
阅读 1 分钟
2.5k
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that num[-1] = num[n] = -∞....

[LeetCode]Zigzag Iterator

2015-12-31
阅读 2 分钟
2.1k
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given two 1d vectors: {代码...} By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 3, 2, 4, 5, 6]. Follow up: What if you are given k 1d vector...

[LeetCode]Unique Binary Search Trees

2015-12-31
阅读 2 分钟
1.9k
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. {代码...}

[LeetCode]Fraction to Recurring Decimal

2015-12-31
阅读 2 分钟
1.7k
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 example, Given numerator = 1, denominator = 2, return "0.5". Given numerator = 2, denominator = 1,...

[LeetCode]Sliding Window Maximum

2015-12-30
阅读 2 分钟
2.2k
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 moves right by one position. For example,Given nums = [1,3,-1,-3,5,3,6,7], and k = 3. {代码...} Th...

[Interview]规定间隔内,重新排列字符串中的字符

2015-12-30
阅读 2 分钟
4.1k
给定一个字符串及一个最小间隔,重新排列字符串中的字符,使得重排后的任意相同字符的间距要大于或等于给定的最小距离,返回一个任意符合规定的字符串即可。 Example: {代码...}

[LeetCode]Number of Connected Components in an Undirected Graph

2015-12-30
阅读 2 分钟
7.4k
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph. Example 1: {代码...} Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2. Example 2: {代码...} Given n = ...

[LintCode]LongestIncreasing Continuous Subsequence II

2015-12-29
阅读 2 分钟
2.6k
Give you an integer matrix (with row size n, column size m),find the longest increasing continuous subsequence in this matrix. (The definition of the longest increasing continuous subsequence here can start at any row or column and go up/down/right/left any direction). ExampleGiven a matrix: {代...

[LintCode]Longest Increasing Subsequence

2015-12-29
阅读 2 分钟
1.9k
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. ExampleFor [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3For [4, 2, 4, 5, 3, 7], the LIS is [4, 4, 5, 7], return 4

[LeetCode]Insert Interval

2015-12-29
阅读 2 分钟
2.1k
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9]. Example 2:Given [1,2],[...

[LeetCode]Minimum Window Substring

2015-12-28
阅读 2 分钟
2.4k
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC". Note:If there is no such window in S that covers all characters in T, return the empty string "". If there...

[LeetCode]Longest Substring with At Most Two Distinct Characters

2015-12-28
阅读 2 分钟
4.1k
Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example, Given s = “eceba”, T is "ece" which its length is 3.

[LeetCode]Paint Fence

2015-12-28
阅读 1 分钟
2.8k
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. Note:n and k are non-negative integers.

[LeetCode]Coin Change

2015-12-28
阅读 2 分钟
4.5k
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. Example 1:coins = [1, 2, 5], amount...

[LeetCode]Regular Expression Matching

2015-12-28
阅读 3 分钟
2.7k
Implement regular expression matching with support for '.' and '*'. {代码...}

[LeetCode]Alien Dictionary

2015-12-27
阅读 3 分钟
3.4k
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language. For example,...

[LeetCode]Perfect Squares

2015-12-25
阅读 1 分钟
2.3k
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 2 because 13 = 4 + 9.

[LeetCode]Sqrt(x)

2015-12-25
阅读 2 分钟
3.2k
Implement double sqrt(double x, int p)如果结果是res, 必须满足res*res与x直到小数点后p位结果都相同。

[LeetCode]Expression Add Operators

2015-12-25
阅读 2 分钟
2.2k
Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value. {代码...}