[Leetcode] Graph Valid Tree 图与树

2015-09-25
阅读 3 分钟
14.3k
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] Longest Substring with At Most 2 Distinct Characters

2015-09-25
阅读 3 分钟
4.3k
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-09-25
阅读 1 分钟
16k
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] Missing Ranges 缺失区间

2015-09-24
阅读 2 分钟
11.1k
Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"].

[Leetcode] Palindrome Permutation 回文变换

2015-09-24
阅读 2 分钟
8.8k
Given a string, determine if a permutation of the string could form a palindrome. For example, "code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even length. What difference do you notice? Count the frequency of each character. If each character ...

[Leetcode] Search a 2D Matrix 搜索二维矩阵

2015-09-24
阅读 4 分钟
3.8k
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 from left to right. The first integer of each row is greater than the last integer of the previous row. For example, Consider the following matrix: ...

[Leetcode] Strobogrammatic Number 对称数

2015-09-24
阅读 3 分钟
10.9k
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is represented as a string. For example, the numbers "69", "88", and "818" are all strobogrammatic.

[Leetcode] The Skyline Problem 天际线问题

2015-09-24
阅读 4 分钟
17.9k
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 shown on a cityscape photo (Figure A), write a program to output the skyline formed by these bui...

[Leetcode] Wildcard Matching 通配符匹配

2015-09-24
阅读 2 分钟
11.2k
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 the entire input string (not partial). The function prototype should be: bool isMatch(const char *s,...

[Leetcode] Zigzag Iterator Z形迭代器

2015-09-24
阅读 3 分钟
9.8k
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] Wiggle Sort 摇摆排序

2015-09-23
阅读 2 分钟
23.4k
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].

[CS101] 转载:浅议Fibonacci(斐波纳契)数列求解

2015-09-20
阅读 4 分钟
4.4k
描述了动物繁殖数量、植物花序变化等自然规律。作为一个经典的数学问题,Fibonacci数列常作为例子出现在程序设计、数据结构与算法等多个相关学科中。

[Leetcode] Summary Ranges 统计区间

2015-09-20
阅读 2 分钟
5k
Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

[Leetcode] Merge Intervals and Insert Interval 合并间隔与插入间隔

2015-09-20
阅读 4 分钟
3.5k
Given a collection of intervals, merge all overlapping intervals.For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].

[Leetcode] First Bad Version 第一个错误版本

2015-09-20
阅读 2 分钟
5.4k
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have n versions...

[Leetcode] Perfect Squares 完美平方数

2015-09-20
阅读 1 分钟
17.2k
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] Move Zeroes 移动零

2015-09-20
阅读 1 分钟
12.3k
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 function, nums should be [1, 3, 12, 0, 0].

[Leetcode] Product of Array Except Self 自身以外的数组乘积

2015-09-19
阅读 2 分钟
3.6k
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Follow up: Could you solve it with constant sp...

[Leetcode] Permutation Sequence 全排列序列

2015-09-19
阅读 2 分钟
7k
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" "312" "321" Given n and k, return the kth permutation sequence. Note: Given n will be between 1 and 9 in...

[Leetcode] Letter Combinations of a Phone Number 电话号码组合

2015-09-19
阅读 2 分钟
6.7k
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 string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. Note: Although the above answer ...

[Leetcode] Next Permutation 下一个排列

2015-09-19
阅读 2 分钟
6.5k
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 order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memo...

[Leetcode] Permutations 全排列

2015-09-19
阅读 4 分钟
9.7k
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].

[Leetcode] N-Queens N皇后

2015-09-18
阅读 6 分钟
7.1k
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both...

[Leetcode] Sort Colors 颜色排序

2015-09-18
阅读 2 分钟
6.2k
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

[Leetcode] Reverse Words in a String 反转单词顺序

2015-09-18
阅读 3 分钟
6.6k
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space

[Leetcode] Same Tree Symmetric Tree 相同树 对称树

2015-09-17
阅读 4 分钟
2.7k
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

[Leetcode] Flatten Binary Tree to Linked List 整平二叉树

2015-09-17
阅读 3 分钟
3.7k
Given a binary tree, flatten it to a linked list in-place. For example, Given {代码...} The flattened tree should look like: {代码...}

[Leetcode] Pow(x, n) 实现乘方函数

2015-09-17
阅读 1 分钟
7.8k
通过一点点数学推导我们可以知道,如果n是偶数$$ x^nx^n = x^{2n}$$如果n是奇数$$ x^nx^nx = x^{2n+1}$$根据这几条原则递归,我们就不用将x相乘n次,而只要logN次就行了

[Leetcode] Construct Binary Tree from Traversal 根据遍历序列建树

2015-09-16
阅读 3 分钟
5.6k
Given preorder and inorder traversal of a tree, construct the binary tree.

[Leetcode] Palindrome Partitioning 回文分割

2015-09-16
阅读 2 分钟
4.1k
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 {代码...}