104. Maximum Depth of Binary Tree

2018-09-20
阅读 2 分钟
1.8k
思路 递归/深度优先搜索, 维持一个全局变量记录深度 复杂度 时间复杂度O(n) 代码 {代码...} 思路 分治法 复杂度 时间复杂度O(n) 代码 {代码...} 思路 广度优先搜索的方法, 用queue来记录树的节点 复杂度 时间复杂度O(n) 空间复杂度O(log n) 代码 {代码...}

102. Binary Tree Level Order Traversal

2018-09-14
阅读 1 分钟
1.6k
Given a binary tree, return the level order traversal of its nodes'values. (ie, from left to right, level by level).For example: Given binary tree [3,9,20,null,null,15,7],return its level order traversal as: [ [3], [9,20], [15,7] ]

Permutations I & II

2018-09-10
阅读 3 分钟
1.5k
Given a collection of distinct integers, return all possiblepermutations.Example: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]

Subsets I & II

2018-09-10
阅读 2 分钟
1.4k
Given a set of distinct integers, nums, return all possible subsets(the power set).Note: The solution set must not contain duplicate subsets. Example: Input: nums = [1,2,3] Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]

138. Copy List with Random Pointer

2018-09-07
阅读 2 分钟
2k
A linked list is given such that each node contains an additionalrandom pointer which could point to any node in the list or null.Return a deep copy of the list.

708. Insert into a Cyclic Sorted List

2018-09-07
阅读 3 分钟
4.3k
any single node in the list, and may not be necessarily the smallestvalue in the cyclic list.If there are multiple suitable places for insertion, you may chooseany place to insert the new value. After the insertion, the cycliclist should remain sorted. If the list is empty (i.e., given node is nu...

234. Palindrome Linked List

2018-09-06
阅读 1 分钟
1.6k
Given a singly linked list, determine if it is a palindrome.Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true

328. Odd Even Linked List

2018-09-06
阅读 2 分钟
1.8k
Given a singly linked list, group all odd nodes together followed bythe even nodes. Please note here we are talking about the node numberand not the value in the nodes.You should try to do it in place. The program should run in O(1) spacecomplexity and O(nodes) time complexity. Example 1: Input: ...

Intersection of Two Linked Lists

2018-09-05
阅读 2 分钟
1.3k
Write a program to find the node at which the intersection of twosingly linked lists begins. For example, the following two linked lists: A: a1 → a2 {代码...} Notes: If the two linked lists have no intersection at all, return null. Thelinked lists must retain their original structure after the fu...

209. Minimum Size Subarray Sum

2018-08-29
阅读 2 分钟
1.8k
Given an array of n positive integers and a positive integer s, findthe minimal length of a contiguous subarray of which the sum ≥ s. Ifthere isn't one, return 0 instead.Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarray[4,3] has the minimal length under the problem c...

485. Max Consecutive Ones

2018-08-29
阅读 1 分钟
1.5k
Given a binary array, find the maximum number of consecutive 1s inthis array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first twodigits or the last three digits are consecutive 1s. {代码...} The input array will only contain 0 and 1. The length of input arrayis a positive integer...

27 Remove Element

2018-08-29
阅读 2 分钟
1.4k
Given an array nums and a value val, remove all instances of thatvalue in-place and return the new length.Do not allocate extra space for another array, you must do this bymodifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you l...

561. Array Partition I

2018-08-28
阅读 1 分钟
1.3k
{代码...} 思路 因为要在两个数中取最小的值, 所以应该尽量找相邻的两个数一组, 这样才不会浪费一个大的数值。所以将数组排序, 找到两个中间大的那一个 复杂度 时间O(nlogn) 排序的复杂度空间O(1) 代码 {代码...}

344 Reverse String

2018-08-28
阅读 2 分钟
1.6k
Write a function that takes a string as input and returns the stringreversed.Example 1: Input: "hello" Output: "olleh" Example 2: Input: "A man, a plan, a canal: Panama" Output: "amanaP :lanac a ,nalpa ,nam A"

Pascal's Triangle I&II

2018-08-27
阅读 2 分钟
1.4k
把三角分为三种情况, 第一个1是每一行都有的, 结尾的1是从第二行才开始有, 然后中间的数字是从第三行开始, 对于i位置的数字是他前一行的i位置和i-1位置数字的和

Spiral Matrix I & II

2018-08-25
阅读 3 分钟
1.7k
Given a matrix of m x n elements (m rows, n columns), return allelements of the matrix in spiral order.Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output:[1,2,3,6,9,8,7,4,5] Example 2: Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Output:[1,2,3,4,8,12,11,10,9,5,6,7]

498. Diagonal Traverse

2018-08-24
阅读 2 分钟
1.9k
Given a matrix of M x N elements (M rows, N columns), return allelements of the matrix in diagonal order as shown in the below image.Example: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,4,7,5,3,6,8,9] Explanation: Note: The total number of elements of the given matrix will not e...

66 Plus One

2018-08-23
阅读 1 分钟
1.6k
Given a non-empty array of digits representing a non-negative integer,plus one to the integer. The digits are stored such that the most significant digit is at thehead of the list, and each element in the array contain a singledigit. You may assume the integer does not contain any leading zero, e...

747 Largest Number At Least Twice of Others

2018-08-22
阅读 1 分钟
1.3k
In a given integer array nums, there is always exactly one largestelement. Find whether the largest element in the array is at least twice asmuch as every other number in the array. If it is, return the index of the largest element, otherwise return-1. Note: nums will have a length in the range [...

724. Find Pivot Index

2018-08-21
阅读 1 分钟
1.3k
Given an array of integers nums, write a method that returns the"pivot" index of this array.

Longest Common Prefix

2017-05-16
阅读 1 分钟
1.9k
暴力解法, 先找到所有字符里面最小长度(最长的前缀肯定小于等于这个长度).之后循环, 每个以第零个为参照逐位扫描, 如果不相同则返回当前记录的前缀

Implement strStr()

2017-05-15
阅读 1 分钟
1.4k
Implement strStr() {代码...} 思路 用双重for循环遍历, 要注意cornercase 以及两个string相比较时候指针遍历是从0到i-j 而不是i-j-1 复杂度 时间O((m-n)*n) 代码 public class Solution { {代码...} }

Add Binary

2017-05-15
阅读 1 分钟
1.3k
Add Binary {代码...} 思路 String的思路通常都是从后往前处理, 这道题重点是要考虑两个字符串长度不同的情况和char转换为number 复杂度 时间O(max(m,n)),空间O(max(m,n)) 代码 public class Solution { {代码...} }

Valid Sudoku leetcode

2016-01-15
阅读 2 分钟
2k
Determine if a Sudoku is valid, according to: Sudoku Puzzles - TheRules. The Sudoku board could be partially filled, where empty cells arefilled with the character '.'.

Word Search leetcode

2016-01-15
阅读 2 分钟
2.9k
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacentcell, where "adjacent" cells are those horizontally or verticallyneighboring. The same letter cell may not be used more than once. For example, Given board = {代码......

N-Queens I & II leetcode

2016-01-14
阅读 3 分钟
2.2k
The n-queens puzzle is the problem of placing n queens on an n×nchessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queenspuzzle. Each solution contains a distinct board configuration of the n-queens'placement, where 'Q' and '.' both ind...

Palindrome Partitioning leetcode

2016-01-14
阅读 2 分钟
2.1k
Given a string s, partition s such that every substring of thepartition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ {代码...}

Restore IP Addresses leetcode

2016-01-14
阅读 2 分钟
2.4k
Given a string containing only digits, restore it by returning allpossible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

Permutations I & II leetcode

2016-01-14
阅读 3 分钟
2.2k
Given a collection of distinct numbers, return all possiblepermutations. 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].

Combination Sum I & II & IIIleetcode

2016-01-14
阅读 4 分钟
2.1k
Given a set of candidate numbers (C) and a target number (T), find allunique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number oftimes. Note: All numbers (including target) will be positive integers.Elements in a combination (a...