1、Convert Sorted Array to Binary Search Tree(将升序的数组转换成平衡二叉树):
2、Happy Number
3、Min Stack-实现返回最小元素的栈
4、Plus One
数组的值转成数值,加1之后,输出值的列表形式
5、判断一个数是否是3的幂
6、 Pascal's Triangle打印出帕斯卡三角形
7、Pascal's Triangle II
得到帕斯卡三角形里面的某一层
8、Number of 1 Bits
计算二进制中“1”的个数
9、n的阶乘尾部有几个0-Factorial Trailing Zeroes
10、count and say
11、Reverse Bits
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
Related problem: Reverse Integer
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
12、Merge Sorted Array 合并两个有序列表
13、Excel Sheet Column Number
Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
14、Longest Common Prefix 求列表字符串最长的公共前缀
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string ""
.
Example 1:
Input: ["flower","flow","flight"] Output: "fl"
Example 2:
Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings.
15、Implement strStr() 找到子串
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
18、Valid Palindrome 合法的回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama"
is a palindrome."race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
19、Roman to Integer
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II
in Roman numeral, just two one's added together. Twelve is written as, XII
, which is simply X
+ II
. The number twenty seven is written as XXVII
, which is XX
+ V
+ II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed before V
(5) and X
(10) to make 4 and 9. X
can be placed before L
(50) and C
(100) to make 40 and 90. C
can be placed before D
(500) and M
(1000) to make 400 and 900.Example 1:
Input: "III" Output: 3
Example 2:
Input: "IV" Output: 4
Example 3:
Input: "IX" Output: 9
Example 4:
Input: "LVIII" Output: 58 Explanation: C = 100, L = 50, XXX = 30 and III = 3.
Example 5:
Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
20、Sum of Two Integers 两数相加,不能用到+-*/
看到这题就想到学计算机组成原理时讲的加法器了。
看看51
和85
以二进制的形式相加:
00110011
+ 01010101
----------
10001000
注意看,由于二进制仅有0
和1
,那么出现进位的情况只能是1
和1
相加,并且进位其实相当于在其前一位加1
,那么进位产生的1
就可以表示为按位与的结果再往左移位一位,即
00110011
& 01010101
----------
00010001
<<
----------
00100010
那么不考虑进位的相加结果是什么了,其实就是按位异或了,即
00110011
^ 01010101
----------
01100110
来看看“按位与,移位”和“按位异或”相加的结果:
00100010
+ 01100110
----------
10001000
怎么样?就是最终的答案了。所以只需要重复这个按位与和按位异或的过程,直到按位与的值为0(不产生进位),那么就得到最终结果了。来模拟一下
00110011
01010101
----------
& -> 00010001 << 1 -> 00100010
^ -> 01100110
00100010
01100110
----------
& -> 00100010 << 1 -> 01000100
^ -> 01000100
01000100
01000100
----------
& -> 01000100 << 1 -> 10001000
^ -> 00000000
10001000
00000000
----------
& -> 00000000
^ -> 10001000
output: 10001000
21、Product of Array Except Self
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]
.
题目的意思是,输入一个数组,输出数组的每一位是输入数组除了自己本身之外,其他元素的乘积,题目要求不使用除法,在O(n)时间复杂度内完成此题目,并且空间复杂度是在常数范围内
首先想到的思路是计算全部数字的乘积,然后分别除以num数组中的每一个数(需要排除数字0)。然而,题目要求不能使用除法
我们以一个4个元素的数组为例,nums=[a1, a2, a3, a4]。
想在O(n)时间复杂度完成最终的数组输出,res=[a2*a3*a4, a1*a3*a4, a1*a2*a4, a2*a3*a4]。
比较好的解决方法是构造两个数组相乘:
22、Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
题目提示最好不要用递归
23、Top K Frequent Elements
求列表中元素个数topk的元素
25、 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l)
there are such that A[i] + B[j] + C[k] + D[l]
is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
Example:
Input: A = [ 1, 2] B = [-2,-1] C = [-1, 2] D = [ 0, 2] Output: 2 Explanation: The two tuples are: 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
四个长度相同的列表,从每个列表总取一个元素,加和为0的组合有多少个
26、排列组合Permutations
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
27、Shuffle an Array 随机打乱一个数组
用random.sample函数:
用shuffle函数
洗牌操作
28、Kth Smallest Element in a Sorted Matrix
Example:
matrix = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ], k = 8, return 13.
29、Kth Smallest Element in a BST
30、Subsets 输出列表的所有子集
深度遍历
31、Odd Even Linked List
31、Find the Duplicate Number
32、Flatten Nested List Iterator 平滑一个列表
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,[1,1]]
,
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]
.
Example 2:
Given the list [1,[4,[6]]]
,
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]
.
33、Unique Paths
搜索机器人从左上角走到右下角有多少种路径~它每一步只能往右或者往下
34、Binary Tree Level Order Traversal 二叉树层次遍历
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
35、Rotate Image
Example 1:
Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2], [9,6,3] ]
顺时针转换矩阵,题目要求直接修改原始矩阵
36、Kth Largest Element in an Array
37、 Insert Delete GetRandom O(1)
Design a data structure that supports all following operations in average O(1) time.
insert(val)
: Inserts an item val to the set if not already present.remove(val)
: Removes an item val from the set if present.getRandom
: Returns a random element from current set of elements. Each element must have the same probability of being returned.实现插入,删除和随机取一个数的操作
38、 Increasing Triplet Subsequence 查找list中是否存在长度为3的递增子序列
Your algorithm should run in O(n) time complexity and O(1) space complexity.
Examples:
Given [1, 2, 3, 4, 5]
,
return true
.
Given [5, 4, 3, 2, 1]
,
return false
.
39、 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:
For example,
Consider the following matrix:
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ]
Given target = 5
, return true
.
Given target = 20
, return false
.
40、Find Peak Element
寻找峰值数,左右的数小于它自己本身
41、Sort Colors
Example:
Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2]
每个数字代表一个颜色,将相同颜色的放在相邻的位置
相当于引入两个指针i,j将三种颜色区域分开
或者设置头尾指针,遇到0和2就和当前i交换位置,元素为1则继续向后遍历
42、Longest Increasing Subsequence 找到最大增长序列,返回序列长度
没怎么搞懂,记住吧先....
43、Group Anagrams
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
44、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
because 12 = 4 + 4 + 4
; given n = 13
, return 2
because 13 = 4 + 9
.
45、 Valid Sudoku
判断是否是合法的数独
每行必须包含数字1-9而不重复。
每列必须包含数字1-9而不重复。
网格的9个3x3子框中的每一个都必须包含数字1-9而不重复
46、Game of Life
47、Container With Most Water
48、Populating Next Right Pointers in Each Node
Example:
Given the following perfect binary tree,
1 / \ 2 3 / \ / \ 4 5 6 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
49、Number of Islands
Example 1:
11110 11010 11000 00000
Answer: 1
Example 2:
11000 11000 00100 00011给定“1(陆地)”和“0(水)”的二维网格图,计算岛的数量。 一个岛被水(0)包围,并且通过水平或垂直连接相邻的陆地(1)而形成。
50、Letter Combinations of a Phone Number
Given a string containing digits from 2-9
inclusive, 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. Note that 1 does not map to any letters.
Example:
Input: "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
51、Set Matrix Zeroes
若矩阵某元素为0,则将其所在的行和列均置为0
Example 1:
Input: [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], [0,0,0], [1,0,1] ]
52、Palindrome Partitioning
输入字符串,输出其子串为回文的结果列表
Example:
Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ]
53、 Longest Substring with At Least K Repeating Characters
Example 1:
Input: s = "aaabb", k = 3 Output: 3 The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2:
Input: s = "ababbc", k = 2 Output: 5 The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
54、Construct Binary Tree from Preorder and Inorder Traversal
根据中序遍历和前序遍历构建二叉树
For example, given
preorder = [3,9,20,15,7] inorder = [9,3,15,20,7]
Return the following binary tree:
3 / \ 9 20 / \ 15 7
55、Remove Nth Node From End of List
Example:
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
删除链表倒数第n个位置的数
56、Course Schedule
本题很明显是一个关于是否有回路的检测问题。因而想起本科的课程中提到过一个类似的问题,解题方法如下:
57、Merge Intervals
Example 1:
Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
合并列表里面的元素,若当前元素的第一个元素小于之前元素的第二个元素,那么合并
58、Search in Rotated Sorted Array
在旋转之后的列表中查找
Example 1:
Input: nums = [4,5,6,7,0,1,2]
, target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2]
, target = 3
Output: -1
59、Search for a Range
Example 1:
Input: nums = [5,7,7,8,8,10]
, target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10]
, target = 6
Output: [-1,-1]
查找数在数组中的边界
60、 Word Break
字符串能否划分成字典里面的词
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"] Output: true Explanation: Return true because"leetcode"
can be segmented as"leet code"
.
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"] Output: true Explanation: Return true because"
applepenapple"
can be segmented as"
apple pen apple"
. Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] Output: false
61、 Implement Trie (Prefix Tree)
实现一个前缀树
62、Course Schedule II
已知课程数以及课程依赖关系,输出一种合适的上课的先后顺序
4, [[1,0],[2,0],[3,1],[3,2]]
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]
. Another correct ordering is[0,2,1,3]
.
63、Basic Calculator II
"3+2*2" = 7 " 3/2 " = 1 " 3+5 / 2 " = 5
输入字符串,输出计算结果
64、Add Two Numbers
链表相加,
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
65、 Longest Substring Without Repeating Characters
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
66、Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd" Output: "bb"
Example 1:
Input: "42" Output: 42
Example 2:
Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned.