auto commit

This commit is contained in:
CyC2018 2019-10-27 00:52:52 +08:00
parent 898bff377b
commit 39e10a02d9
30 changed files with 1050 additions and 350 deletions

View File

@ -96,7 +96,9 @@ l m h
# 1. 求开方
[69. Sqrt(x) (Easy)](https://leetcode.com/problems/sqrtx/description/)
69\. Sqrt(x) (Easy)
[Leetcode](https://leetcode.com/problems/sqrtx/description/) / [力扣](https://leetcode-cn.com/problems/sqrtx/description/)
```html
Input: 4
@ -134,7 +136,9 @@ public int mySqrt(int x) {
# 2. 大于给定元素的最小元素
[744. Find Smallest Letter Greater Than Target (Easy)](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/)
744\. Find Smallest Letter Greater Than Target (Easy)
[Leetcode](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/) / [力扣](https://leetcode-cn.com/problems/find-smallest-letter-greater-than-target/description/)
```html
Input:
@ -168,7 +172,9 @@ public char nextGreatestLetter(char[] letters, char target) {
# 3. 有序数组的 Single Element
[540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/)
540\. Single Element in a Sorted Array (Medium)
[Leetcode](https://leetcode.com/problems/single-element-in-a-sorted-array/description/) / [力扣](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/description/)
```html
Input: [1, 1, 2, 3, 3, 4, 4, 8, 8]
@ -205,7 +211,9 @@ public int singleNonDuplicate(int[] nums) {
# 4. 第一个错误的版本
[278. First Bad Version (Easy)](https://leetcode.com/problems/first-bad-version/description/)
278\. First Bad Version (Easy)
[Leetcode](https://leetcode.com/problems/first-bad-version/description/) / [力扣](https://leetcode-cn.com/problems/first-bad-version/description/)
题目描述给定一个元素 n 代表有 [1, 2, ..., n] 版本在第 x 位置开始出现错误版本导致后面的版本都错误可以调用 isBadVersion(int x) 知道某个版本是否错误要求找到第一个错误的版本
@ -230,7 +238,9 @@ public int firstBadVersion(int n) {
# 5. 旋转数组的最小数字
[153. Find Minimum in Rotated Sorted Array (Medium)](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/)
153\. Find Minimum in Rotated Sorted Array (Medium)
[Leetcode](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/) / [力扣](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description/)
```html
Input: [3,4,5,1,2],
@ -254,7 +264,9 @@ public int findMin(int[] nums) {
# 6. 查找区间
[34. Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
34\. Find First and Last Position of Element in Sorted Array
[Leetcode](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) / [力扣](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
```html
Input: nums = [5,7,7,8,8,10], target = 8

View File

@ -61,7 +61,9 @@ static String toBinaryString(int i); // 转换为二进制表示的字符串
# 1. 统计两个数的二进制表示有多少位不同
[461. Hamming Distance (Easy)](https://leetcode.com/problems/hamming-distance/)
461. Hamming Distance (Easy)
[Leetcode](https://leetcode.com/problems/hamming-distance/) / [力扣](https://leetcode-cn.com/problems/hamming-distance/)
```html
Input: x = 1, y = 4
@ -114,7 +116,9 @@ public int hammingDistance(int x, int y) {
# 2. 数组中唯一一个不重复的元素
[136. Single Number (Easy)](https://leetcode.com/problems/single-number/description/)
136\. Single Number (Easy)
[Leetcode](https://leetcode.com/problems/single-number/description/) / [力扣](https://leetcode-cn.com/problems/single-number/description/)
```html
Input: [4,1,2,1,2]
@ -133,7 +137,9 @@ public int singleNumber(int[] nums) {
# 3. 找出数组中缺失的那个数
[268. Missing Number (Easy)](https://leetcode.com/problems/missing-number/description/)
268\. Missing Number (Easy)
[Leetcode](https://leetcode.com/problems/missing-number/description/) / [力扣](https://leetcode-cn.com/problems/missing-number/description/)
```html
Input: [3,0,1]
@ -154,7 +160,9 @@ public int missingNumber(int[] nums) {
# 4. 数组中不重复的两个元素
[260. Single Number III (Medium)](https://leetcode.com/problems/single-number-iii/description/)
260\. Single Number III (Medium)
[Leetcode](https://leetcode.com/problems/single-number-iii/description/) / [力扣](https://leetcode-cn.com/problems/single-number-iii/description/)
两个不相等的元素在位级表示上必定会有一位存在不同
@ -178,7 +186,9 @@ public int[] singleNumber(int[] nums) {
# 5. 翻转一个数的比特位
[190. Reverse Bits (Easy)](https://leetcode.com/problems/reverse-bits/description/)
190\. Reverse Bits (Easy)
[Leetcode](https://leetcode.com/problems/reverse-bits/description/) / [力扣](https://leetcode-cn.com/problems/reverse-bits/description/)
```java
public int reverseBits(int n) {
@ -233,7 +243,9 @@ a = a ^ b;
# 7. 判断一个数是不是 2 n 次方
[231. Power of Two (Easy)](https://leetcode.com/problems/power-of-two/description/)
231\. Power of Two (Easy)
[Leetcode](https://leetcode.com/problems/power-of-two/description/) / [力扣](https://leetcode-cn.com/problems/power-of-two/description/)
二进制表示只有一个 1 存在
@ -253,7 +265,9 @@ public boolean isPowerOfTwo(int n) {
# 8. 判断一个数是不是 4 n 次方
[342. Power of Four (Easy)](https://leetcode.com/problems/power-of-four/)
342\. Power of Four (Easy)
[Leetcode](https://leetcode.com/problems/power-of-four/) / [力扣](https://leetcode-cn.com/problems/power-of-four/)
这种数在二进制表示中有且只有一个奇数位为 1例如 1610000
@ -273,7 +287,9 @@ public boolean isPowerOfFour(int num) {
# 9. 判断一个数的位级表示是否不会出现连续的 0 1
[693. Binary Number with Alternating Bits (Easy)](https://leetcode.com/problems/binary-number-with-alternating-bits/description/)
693\. Binary Number with Alternating Bits (Easy)
[Leetcode](https://leetcode.com/problems/binary-number-with-alternating-bits/description/) / [力扣](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/description/)
```html
Input: 10
@ -298,7 +314,9 @@ public boolean hasAlternatingBits(int n) {
# 10. 求一个数的补码
[476. Number Complement (Easy)](https://leetcode.com/problems/number-complement/description/)
476\. Number Complement (Easy)
[Leetcode](https://leetcode.com/problems/number-complement/description/) / [力扣](https://leetcode-cn.com/problems/number-complement/description/)
```html
Input: 5
@ -353,7 +371,9 @@ public int findComplement(int num) {
# 11. 实现整数的加法
[371. Sum of Two Integers (Easy)](https://leetcode.com/problems/sum-of-two-integers/description/)
371\. Sum of Two Integers (Easy)
[Leetcode](https://leetcode.com/problems/sum-of-two-integers/description/) / [力扣](https://leetcode-cn.com/problems/sum-of-two-integers/description/)
a ^ b 表示没有考虑进位的情况下两数的和(a & b) << 1 就是进位
@ -367,7 +387,9 @@ public int getSum(int a, int b) {
# 12. 字符串数组最大乘积
[318. Maximum Product of Word Lengths (Medium)](https://leetcode.com/problems/maximum-product-of-word-lengths/description/)
318\. Maximum Product of Word Lengths (Medium)
[Leetcode](https://leetcode.com/problems/maximum-product-of-word-lengths/description/) / [力扣](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/description/)
```html
Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
@ -402,7 +424,9 @@ public int maxProduct(String[] words) {
# 13. 统计从 0 \~ n 每个数的二进制表示中 1 的个数
[338. Counting Bits (Medium)](https://leetcode.com/problems/counting-bits/description/)
338\. Counting Bits (Medium)
[Leetcode](https://leetcode.com/problems/counting-bits/description/) / [力扣](https://leetcode-cn.com/problems/counting-bits/description/)
对于数字 6(110)它可以看成是 4(100) 再加一个 2(10)因此 dp[i] = dp[i&(i-1)] + 1;

View File

@ -6,7 +6,9 @@
# 1. 给表达式加括号
[241. Different Ways to Add Parentheses (Medium)](https://leetcode.com/problems/different-ways-to-add-parentheses/description/)
241\. Different Ways to Add Parentheses (Medium)
[Leetcode](https://leetcode.com/problems/different-ways-to-add-parentheses/description/) / [力扣](https://leetcode-cn.com/problems/different-ways-to-add-parentheses/description/)
```html
Input: "2-1-1".
@ -51,7 +53,9 @@ public List<Integer> diffWaysToCompute(String input) {
# 2. 不同的二叉搜索树
[95. Unique Binary Search Trees II (Medium)](https://leetcode.com/problems/unique-binary-search-trees-ii/description/)
95\. Unique Binary Search Trees II (Medium)
[Leetcode](https://leetcode.com/problems/unique-binary-search-trees-ii/description/) / [力扣](https://leetcode-cn.com/problems/unique-binary-search-trees-ii/description/)
给定一个数字 n要求生成所有值为 1...n 的二叉搜索树

View File

@ -46,7 +46,9 @@
## 1. 爬楼梯
[70. Climbing Stairs (Easy)](https://leetcode.com/problems/climbing-stairs/description/)
70\. Climbing Stairs (Easy)
[Leetcode](https://leetcode.com/problems/climbing-stairs/description/) / [力扣](https://leetcode-cn.com/problems/climbing-stairs/description/)
题目描述 N 阶楼梯每次可以上一阶或者两阶求有多少种上楼梯的方法
@ -77,7 +79,9 @@ public int climbStairs(int n) {
## 2. 强盗抢劫
[198. House Robber (Easy)](https://leetcode.com/problems/house-robber/description/)
198\. House Robber (Easy)
[Leetcode](https://leetcode.com/problems/house-robber/description/) / [力扣](https://leetcode-cn.com/problems/house-robber/description/)
题目描述抢劫一排住户但是不能抢邻近的住户求最大抢劫量
@ -103,7 +107,9 @@ public int rob(int[] nums) {
## 3. 强盗在环形街区抢劫
[213. House Robber II (Medium)](https://leetcode.com/problems/house-robber-ii/description/)
213\. House Robber II (Medium)
[Leetcode](https://leetcode.com/problems/house-robber-ii/description/) / [力扣](https://leetcode-cn.com/problems/house-robber-ii/description/)
```java
public int rob(int[] nums) {
@ -159,7 +165,9 @@ private int rob(int[] nums, int first, int last) {
## 1. 矩阵的最小路径和
[64. Minimum Path Sum (Medium)](https://leetcode.com/problems/minimum-path-sum/description/)
64\. Minimum Path Sum (Medium)
[Leetcode](https://leetcode.com/problems/minimum-path-sum/description/) / [力扣](https://leetcode-cn.com/problems/minimum-path-sum/description/)
```html
[[1,3,1],
@ -195,7 +203,9 @@ public int minPathSum(int[][] grid) {
## 2. 矩阵的总路径数
[62. Unique Paths (Medium)](https://leetcode.com/problems/unique-paths/description/)
62\. Unique Paths (Medium)
[Leetcode](https://leetcode.com/problems/unique-paths/description/) / [力扣](https://leetcode-cn.com/problems/unique-paths/description/)
题目描述统计从矩阵左上角到右下角的路径总数每次只能向右或者向下移动
@ -232,7 +242,9 @@ public int uniquePaths(int m, int n) {
## 1. 数组区间和
[303. Range Sum Query - Immutable (Easy)](https://leetcode.com/problems/range-sum-query-immutable/description/)
303\. Range Sum Query - Immutable (Easy)
[Leetcode](https://leetcode.com/problems/range-sum-query-immutable/description/) / [力扣](https://leetcode-cn.com/problems/range-sum-query-immutable/description/)
```html
Given nums = [-2, 0, 3, -5, 2, -1]
@ -264,7 +276,9 @@ class NumArray {
## 2. 数组中等差递增子区间的个数
[413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/)
413\. Arithmetic Slices (Medium)
[Leetcode](https://leetcode.com/problems/arithmetic-slices/description/) / [力扣](https://leetcode-cn.com/problems/arithmetic-slices/description/)
```html
A = [0, 1, 2, 3, 4]
@ -323,7 +337,9 @@ public int numberOfArithmeticSlices(int[] A) {
## 1. 分割整数的最大乘积
[343. Integer Break (Medim)](https://leetcode.com/problems/integer-break/description/)
343\. Integer Break (Medim)
[Leetcode](https://leetcode.com/problems/integer-break/description/) / [力扣](https://leetcode-cn.com/problems/integer-break/description/)
题目描述For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
@ -342,7 +358,9 @@ public int integerBreak(int n) {
## 2. 按平方数来分割整数
[279. Perfect Squares(Medium)](https://leetcode.com/problems/perfect-squares/description/)
279\. Perfect Squares(Medium)
[Leetcode](https://leetcode.com/problems/perfect-squares/description/) / [力扣](https://leetcode-cn.com/problems/perfect-squares/description/)
题目描述For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
@ -378,7 +396,9 @@ private List<Integer> generateSquareList(int n) {
## 3. 分割整数构成字母字符串
[91. Decode Ways (Medium)](https://leetcode.com/problems/decode-ways/description/)
91\. Decode Ways (Medium)
[Leetcode](https://leetcode.com/problems/decode-ways/description/) / [力扣](https://leetcode-cn.com/problems/decode-ways/description/)
题目描述Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
@ -426,7 +446,9 @@ public int numDecodings(String s) {
## 1. 最长递增子序列
[300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence/description/)
300\. Longest Increasing Subsequence (Medium)
[Leetcode](https://leetcode.com/problems/longest-increasing-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-increasing-subsequence/description/)
```java
public int lengthOfLIS(int[] nums) {
@ -508,7 +530,9 @@ private int binarySearch(int[] tails, int len, int key) {
## 2. 一组整数对能够构成的最长链
[646. Maximum Length of Pair Chain (Medium)](https://leetcode.com/problems/maximum-length-of-pair-chain/description/)
646\. Maximum Length of Pair Chain (Medium)
[Leetcode](https://leetcode.com/problems/maximum-length-of-pair-chain/description/) / [力扣](https://leetcode-cn.com/problems/maximum-length-of-pair-chain/description/)
```html
Input: [[1,2], [2,3], [3,4]]
@ -540,7 +564,9 @@ public int findLongestChain(int[][] pairs) {
## 3. 最长摆动子序列
[376. Wiggle Subsequence (Medium)](https://leetcode.com/problems/wiggle-subsequence/description/)
376\. Wiggle Subsequence (Medium)
[Leetcode](https://leetcode.com/problems/wiggle-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/wiggle-subsequence/description/)
```html
Input: [1,7,4,9,2,5]
@ -697,7 +723,9 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
## 1. 划分数组为和相等的两部分
[416. Partition Equal Subset Sum (Medium)](https://leetcode.com/problems/partition-equal-subset-sum/description/)
416\. Partition Equal Subset Sum (Medium)
[Leetcode](https://leetcode.com/problems/partition-equal-subset-sum/description/) / [力扣](https://leetcode-cn.com/problems/partition-equal-subset-sum/description/)
```html
Input: [1, 5, 11, 5]
@ -737,7 +765,9 @@ private int computeArraySum(int[] nums) {
## 2. 改变一组数的正负号使得它们的和为一给定数
[494. Target Sum (Medium)](https://leetcode.com/problems/target-sum/description/)
494\. Target Sum (Medium)
[Leetcode](https://leetcode.com/problems/target-sum/description/) / [力扣](https://leetcode-cn.com/problems/target-sum/description/)
```html
Input: nums is [1, 1, 1, 1, 1], S is 3.
@ -809,7 +839,9 @@ private int findTargetSumWays(int[] nums, int start, int S) {
## 3. 01 字符构成最多的字符串
[474. Ones and Zeroes (Medium)](https://leetcode.com/problems/ones-and-zeroes/description/)
474\. Ones and Zeroes (Medium)
[Leetcode](https://leetcode.com/problems/ones-and-zeroes/description/) / [力扣](https://leetcode-cn.com/problems/ones-and-zeroes/description/)
```html
Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
@ -847,7 +879,9 @@ public int findMaxForm(String[] strs, int m, int n) {
## 4. 找零钱的最少硬币数
[322. Coin Change (Medium)](https://leetcode.com/problems/coin-change/description/)
322\. Coin Change (Medium)
[Leetcode](https://leetcode.com/problems/coin-change/description/) / [力扣](https://leetcode-cn.com/problems/coin-change/description/)
```html
Example 1:
@ -888,7 +922,9 @@ public int coinChange(int[] coins, int amount) {
## 5. 找零钱的硬币数组合
[518\. Coin Change 2 (Medium)](https://leetcode.com/problems/coin-change-2/description/)
518\. Coin Change 2 (Medium)
[Leetcode](https://leetcode.com/problems/coin-change-2/description/) / [力扣](https://leetcode-cn.com/problems/coin-change-2/description/)
```text-html-basic
Input: amount = 5, coins = [1, 2, 5]
@ -920,7 +956,9 @@ public int change(int amount, int[] coins) {
## 6. 字符串按单词列表分割
[139. Word Break (Medium)](https://leetcode.com/problems/word-break/description/)
139\. Word Break (Medium)
[Leetcode](https://leetcode.com/problems/word-break/description/) / [力扣](https://leetcode-cn.com/problems/word-break/description/)
```html
s = "leetcode",
@ -957,7 +995,9 @@ public boolean wordBreak(String s, List<String> wordDict) {
## 7. 组合总和
[377. Combination Sum IV (Medium)](https://leetcode.com/problems/combination-sum-iv/description/)
377\. Combination Sum IV (Medium)
[Leetcode](https://leetcode.com/problems/combination-sum-iv/description/) / [力扣](https://leetcode-cn.com/problems/combination-sum-iv/description/)
```html
nums = [1, 2, 3]
@ -1000,7 +1040,9 @@ public int combinationSum4(int[] nums, int target) {
## 1. 需要冷却期的股票交易
[309. Best Time to Buy and Sell Stock with Cooldown(Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
309\. Best Time to Buy and Sell Stock with Cooldown(Medium)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
题目描述交易之后需要有一天的冷却时间
@ -1030,7 +1072,9 @@ public int maxProfit(int[] prices) {
## 2. 需要交易费用的股票交易
[714. Best Time to Buy and Sell Stock with Transaction Fee (Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
714\. Best Time to Buy and Sell Stock with Transaction Fee (Medium)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
```html
Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
@ -1069,7 +1113,9 @@ public int maxProfit(int[] prices, int fee) {
## 3. 只能进行两次的股票交易
[123. Best Time to Buy and Sell Stock III (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/)
123\. Best Time to Buy and Sell Stock III (Hard)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/description/)
```java
public int maxProfit(int[] prices) {
@ -1095,7 +1141,9 @@ public int maxProfit(int[] prices) {
## 4. 只能进行 k 次的股票交易
[188. Best Time to Buy and Sell Stock IV (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/)
188\. Best Time to Buy and Sell Stock IV (Hard)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/description/)
```java
public int maxProfit(int k, int[] prices) {
@ -1125,7 +1173,9 @@ public int maxProfit(int k, int[] prices) {
## 1. 删除两个字符串的字符使它们相等
[583. Delete Operation for Two Strings (Medium)](https://leetcode.com/problems/delete-operation-for-two-strings/description/)
583\. Delete Operation for Two Strings (Medium)
[Leetcode](https://leetcode.com/problems/delete-operation-for-two-strings/description/) / [力扣](https://leetcode-cn.com/problems/delete-operation-for-two-strings/description/)
```html
Input: "sea", "eat"
@ -1154,7 +1204,9 @@ public int minDistance(String word1, String word2) {
## 2. 编辑距离
[72. Edit Distance (Hard)](https://leetcode.com/problems/edit-distance/description/)
72\. Edit Distance (Hard)
[Leetcode](https://leetcode.com/problems/edit-distance/description/) / [力扣](https://leetcode-cn.com/problems/edit-distance/description/)
```html
Example 1:
@ -1207,7 +1259,9 @@ public int minDistance(String word1, String word2) {
## 3. 复制粘贴字符
[650. 2 Keys Keyboard (Medium)](https://leetcode.com/problems/2-keys-keyboard/description/)
650\. 2 Keys Keyboard (Medium)
[Leetcode](https://leetcode.com/problems/2-keys-keyboard/description/) / [力扣](https://leetcode-cn.com/problems/2-keys-keyboard/description/)
题目描述最开始只有一个字符 A问需要多少次操作能够得到 n 个字符 A每次操作可以复制当前所有的字符或者粘贴

View File

@ -10,12 +10,16 @@
- Java 中的 **HashSet** 用于存储一个集合可以查找元素是否在集合中如果元素有穷并且范围不大那么可以用一个布尔数组来存储一个元素是否存在例如对于只有小写字符的元素就可以用一个长度为 26 的布尔数组来存储一个字符集合使得空间复杂度降低为 O(1)
- Java 中的 **HashMap** 主要用于映射关系从而把两个元素联系起来HashMap 也可以用来对元素进行计数统计此时键为元素值为计数 HashSet 类似如果元素有穷并且范围不大可以用整型数组来进行统计在对一个内容进行压缩或者其它转换时利用 HashMap 可以把原始内容和转换后的内容联系起来例如在一个简化 url 的系统中 [Leetcdoe : 535. Encode and Decode TinyURL (Medium)](https://leetcode.com/problems/encode-and-decode-tinyurl/description/)利用 HashMap 就可以存储精简后的 url 到原始 url 的映射使得不仅可以显示简化的 url也可以根据简化的 url 得到原始 url 从而定位到正确的资源
Java 中的 **HashMap** 主要用于映射关系从而把两个元素联系起来HashMap 也可以用来对元素进行计数统计此时键为元素值为计数 HashSet 类似如果元素有穷并且范围不大可以用整型数组来进行统计在对一个内容进行压缩或者其它转换时利用 HashMap 可以把原始内容和转换后的内容联系起来例如在一个简化 url 的系统中 [Leetcdoe : 535. Encode and Decode TinyURL (Medium)
[Leetcode](https://leetcode.com/problems/encode-and-decode-tinyurl/description/)利用 HashMap 就可以存储精简后的 url 到原始 url 的映射使得不仅可以显示简化的 url也可以根据简化的 url 得到原始 url 从而定位到正确的资源<EFBFBD>) / [力扣](https://leetcode-cn.com/problems/encode-and-decode-tinyurl/description/)利用 HashMap 就可以存储精简后的 url 到原始 url 的映射使得不仅可以显示简化的 url也可以根据简化的 url 得到原始 url 从而定位到正确的资源<EFBFBD>)
# 1. 数组中两个数的和为给定值
[1. Two Sum (Easy)](https://leetcode.com/problems/two-sum/description/)
1\. Two Sum (Easy)
[Leetcode](https://leetcode.com/problems/two-sum/description/) / [力扣](https://leetcode-cn.com/problems/two-sum/description/)
可以先对数组进行排序然后使用双指针方法或者二分查找方法这样做的时间复杂度为 O(NlogN)空间复杂度为 O(1)
@ -37,7 +41,9 @@ public int[] twoSum(int[] nums, int target) {
# 2. 判断数组是否含有重复元素
[217. Contains Duplicate (Easy)](https://leetcode.com/problems/contains-duplicate/description/)
217\. Contains Duplicate (Easy)
[Leetcode](https://leetcode.com/problems/contains-duplicate/description/) / [力扣](https://leetcode-cn.com/problems/contains-duplicate/description/)
```java
public boolean containsDuplicate(int[] nums) {
@ -51,7 +57,9 @@ public boolean containsDuplicate(int[] nums) {
# 3. 最长和谐序列
[594. Longest Harmonious Subsequence (Easy)](https://leetcode.com/problems/longest-harmonious-subsequence/description/)
594\. Longest Harmonious Subsequence (Easy)
[Leetcode](https://leetcode.com/problems/longest-harmonious-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-harmonious-subsequence/description/)
```html
Input: [1,3,2,2,5,2,3,7]
@ -79,7 +87,9 @@ public int findLHS(int[] nums) {
# 4. 最长连续序列
[128. Longest Consecutive Sequence (Hard)](https://leetcode.com/problems/longest-consecutive-sequence/description/)
128\. Longest Consecutive Sequence (Hard)
[Leetcode](https://leetcode.com/problems/longest-consecutive-sequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-consecutive-sequence/description/)
```html
Given [100, 4, 200, 1, 3, 2],

View File

@ -15,7 +15,9 @@
## 1. 判断是否为二分图
[785. Is Graph Bipartite? (Medium)](https://leetcode.com/problems/is-graph-bipartite/description/)
785\. Is Graph Bipartite? (Medium)
[Leetcode](https://leetcode.com/problems/is-graph-bipartite/description/) / [力扣](https://leetcode-cn.com/problems/is-graph-bipartite/description/)
```html
Input: [[1,3], [0,2], [1,3], [0,2]]
@ -74,7 +76,9 @@ private boolean isBipartite(int curNode, int curColor, int[] colors, int[][] gra
## 1. 课程安排的合法性
[207. Course Schedule (Medium)](https://leetcode.com/problems/course-schedule/description/)
207\. Course Schedule (Medium)
[Leetcode](https://leetcode.com/problems/course-schedule/description/) / [力扣](https://leetcode-cn.com/problems/course-schedule/description/)
```html
2, [[1,0]]
@ -132,7 +136,9 @@ private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked,
## 2. 课程安排的顺序
[210. Course Schedule II (Medium)](https://leetcode.com/problems/course-schedule-ii/description/)
210\. Course Schedule II (Medium)
[Leetcode](https://leetcode.com/problems/course-schedule-ii/description/) / [力扣](https://leetcode-cn.com/problems/course-schedule-ii/description/)
```html
4, [[1,0],[2,0],[3,1],[3,2]]
@ -195,7 +201,9 @@ private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked, List<Int
## 1. 冗余连接
[684. Redundant Connection (Medium)](https://leetcode.com/problems/redundant-connection/description/)
684\. Redundant Connection (Medium)
[Leetcode](https://leetcode.com/problems/redundant-connection/description/) / [力扣](https://leetcode-cn.com/problems/redundant-connection/description/)
```html
Input: [[1,2], [1,3], [2,3]]

View File

@ -50,7 +50,9 @@ Return "student a am I"
# 4. 两个字符串包含的字符是否完全相同
[242. Valid Anagram (Easy)](https://leetcode.com/problems/valid-anagram/description/)
242\. Valid Anagram (Easy)
[Leetcode](https://leetcode.com/problems/valid-anagram/description/) / [力扣](https://leetcode-cn.com/problems/valid-anagram/description/)
```html
s = "anagram", t = "nagaram", return true.
@ -81,7 +83,9 @@ public boolean isAnagram(String s, String t) {
# 5. 计算一组字符集合可以组成的回文字符串的最大长度
[409. Longest Palindrome (Easy)](https://leetcode.com/problems/longest-palindrome/description/)
409\. Longest Palindrome (Easy)
[Leetcode](https://leetcode.com/problems/longest-palindrome/description/) / [力扣](https://leetcode-cn.com/problems/longest-palindrome/description/)
```html
Input : "abccccdd"
@ -112,7 +116,9 @@ public int longestPalindrome(String s) {
# 6. 字符串同构
[205. Isomorphic Strings (Easy)](https://leetcode.com/problems/isomorphic-strings/description/)
205\. Isomorphic Strings (Easy)
[Leetcode](https://leetcode.com/problems/isomorphic-strings/description/) / [力扣](https://leetcode-cn.com/problems/isomorphic-strings/description/)
```html
Given "egg", "add", return true.
@ -140,7 +146,9 @@ public boolean isIsomorphic(String s, String t) {
# 7. 回文子字符串个数
[647. Palindromic Substrings (Medium)](https://leetcode.com/problems/palindromic-substrings/description/)
647\. Palindromic Substrings (Medium)
[Leetcode](https://leetcode.com/problems/palindromic-substrings/description/) / [力扣](https://leetcode-cn.com/problems/palindromic-substrings/description/)
```html
Input: "aaa"
@ -172,7 +180,9 @@ private void extendSubstrings(String s, int start, int end) {
# 8. 判断一个整数是否是回文数
[9. Palindrome Number (Easy)](https://leetcode.com/problems/palindrome-number/description/)
9\. Palindrome Number (Easy)
[Leetcode](https://leetcode.com/problems/palindrome-number/description/) / [力扣](https://leetcode-cn.com/problems/palindrome-number/description/)
要求不能使用额外空间也就不能将整数转换为字符串进行判断
@ -197,7 +207,9 @@ public boolean isPalindrome(int x) {
# 9. 统计二进制字符串中连续 1 和连续 0 数量相同的子字符串个数
[696. Count Binary Substrings (Easy)](https://leetcode.com/problems/count-binary-substrings/description/)
696\. Count Binary Substrings (Easy)
[Leetcode](https://leetcode.com/problems/count-binary-substrings/description/) / [力扣](https://leetcode-cn.com/problems/count-binary-substrings/description/)
```html
Input: "00110011"

View File

@ -28,7 +28,9 @@
## 1. Kth Element
[215. Kth Largest Element in an Array (Medium)](https://leetcode.com/problems/kth-largest-element-in-an-array/description/)
215\. Kth Largest Element in an Array (Medium)
[Leetcode](https://leetcode.com/problems/kth-largest-element-in-an-array/description/) / [力扣](https://leetcode-cn.com/problems/kth-largest-element-in-an-array/description/)
```text
Input: [3,2,1,5,6,4] and k = 2
@ -104,7 +106,9 @@ private void swap(int[] a, int i, int j) {
## 1. 出现频率最多的 k 个元素
[347. Top K Frequent Elements (Medium)](https://leetcode.com/problems/top-k-frequent-elements/description/)
347\. Top K Frequent Elements (Medium)
[Leetcode](https://leetcode.com/problems/top-k-frequent-elements/description/) / [力扣](https://leetcode-cn.com/problems/top-k-frequent-elements/description/)
```html
Given [1,1,1,2,2,3] and k = 2, return [1,2].
@ -145,7 +149,9 @@ public List<Integer> topKFrequent(int[] nums, int k) {
## 2. 按照字符出现次数对字符串排序
[451. Sort Characters By Frequency (Medium)](https://leetcode.com/problems/sort-characters-by-frequency/description/)
451\. Sort Characters By Frequency (Medium)
[Leetcode](https://leetcode.com/problems/sort-characters-by-frequency/description/) / [力扣](https://leetcode-cn.com/problems/sort-characters-by-frequency/description/)
```html
Input:
@ -199,7 +205,9 @@ public String frequencySort(String s) {
## 1. 按颜色进行排序
[75. Sort Colors (Medium)](https://leetcode.com/problems/sort-colors/description/)
75\. Sort Colors (Medium)
[Leetcode](https://leetcode.com/problems/sort-colors/description/) / [力扣](https://leetcode-cn.com/problems/sort-colors/description/)
```html
Input: [2,0,2,1,1,0]

View File

@ -61,7 +61,9 @@
## 1. 计算在网格中从原点到特定点的最短路径长度
[1091. Shortest Path in Binary Matrix(Medium)](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
1091\. Shortest Path in Binary Matrix(Medium)
[Leetcode](https://leetcode.com/problems/shortest-path-in-binary-matrix/) / [力扣](https://leetcode-cn.com/problems/shortest-path-in-binary-matrix/)
```html
[[1,1,0,1],
@ -104,7 +106,9 @@ public int shortestPathBinaryMatrix(int[][] grids) {
## 2. 组成整数的最小平方数数量
[279. Perfect Squares (Medium)](https://leetcode.com/problems/perfect-squares/description/)
279\. Perfect Squares (Medium)
[Leetcode](https://leetcode.com/problems/perfect-squares/description/) / [力扣](https://leetcode-cn.com/problems/perfect-squares/description/)
```html
For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
@ -167,7 +171,9 @@ private List<Integer> generateSquares(int n) {
## 3. 最短单词路径
[127. Word Ladder (Medium)](https://leetcode.com/problems/word-ladder/description/)
127\. Word Ladder (Medium)
[Leetcode](https://leetcode.com/problems/word-ladder/description/) / [力扣](https://leetcode-cn.com/problems/word-ladder/description/)
```html
Input:
@ -278,7 +284,9 @@ private int getShortestPath(List<Integer>[] graphic, int start, int end) {
## 1. 查找最大的连通面积
[695. Max Area of Island (Medium)](https://leetcode.com/problems/max-area-of-island/description/)
695\. Max Area of Island (Medium)
[Leetcode](https://leetcode.com/problems/max-area-of-island/description/) / [力扣](https://leetcode-cn.com/problems/max-area-of-island/description/)
```html
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
@ -325,7 +333,9 @@ private int dfs(int[][] grid, int r, int c) {
## 2. 矩阵中的连通分量数目
[200. Number of Islands (Medium)](https://leetcode.com/problems/number-of-islands/description/)
200\. Number of Islands (Medium)
[Leetcode](https://leetcode.com/problems/number-of-islands/description/) / [力扣](https://leetcode-cn.com/problems/number-of-islands/description/)
```html
Input:
@ -374,7 +384,9 @@ private void dfs(char[][] grid, int i, int j) {
## 3. 好友关系的连通分量数目
[547. Friend Circles (Medium)](https://leetcode.com/problems/friend-circles/description/)
547\. Friend Circles (Medium)
[Leetcode](https://leetcode.com/problems/friend-circles/description/) / [力扣](https://leetcode-cn.com/problems/friend-circles/description/)
```html
Input:
@ -418,7 +430,9 @@ private void dfs(int[][] M, int i, boolean[] hasVisited) {
## 4. 填充封闭区域
[130. Surrounded Regions (Medium)](https://leetcode.com/problems/surrounded-regions/description/)
130\. Surrounded Regions (Medium)
[Leetcode](https://leetcode.com/problems/surrounded-regions/description/) / [力扣](https://leetcode-cn.com/problems/surrounded-regions/description/)
```html
For example,
@ -483,7 +497,9 @@ private void dfs(char[][] board, int r, int c) {
## 5. 能到达的太平洋和大西洋的区域
[417. Pacific Atlantic Water Flow (Medium)](https://leetcode.com/problems/pacific-atlantic-water-flow/description/)
417\. Pacific Atlantic Water Flow (Medium)
[Leetcode](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) / [力扣](https://leetcode-cn.com/problems/pacific-atlantic-water-flow/description/)
```html
Given the following 5x5 matrix:
@ -571,7 +587,9 @@ Backtracking回溯属于 DFS。
## 1. 数字键盘组合
[17. Letter Combinations of a Phone Number (Medium)](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/)
17\. Letter Combinations of a Phone Number (Medium)
[Leetcode](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) / [力扣](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/description/)
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/9823768c-212b-4b1a-b69a-b3f59e07b977.jpg"/> </div><br>
@ -609,7 +627,9 @@ private void doCombination(StringBuilder prefix, List<String> combinations, fina
## 2. IP 地址划分
[93. Restore IP Addresses(Medium)](https://leetcode.com/problems/restore-ip-addresses/description/)
93\. Restore IP Addresses(Medium)
[Leetcode](https://leetcode.com/problems/restore-ip-addresses/description/) / [力扣](https://leetcode-cn.com/problems/restore-ip-addresses/description/)
```html
Given "25525511135",
@ -650,7 +670,9 @@ private void doRestore(int k, StringBuilder tempAddress, List<String> addresses,
## 3. 在矩阵中寻找字符串
[79. Word Search (Medium)](https://leetcode.com/problems/word-search/description/)
79\. Word Search (Medium)
[Leetcode](https://leetcode.com/problems/word-search/description/) / [力扣](https://leetcode-cn.com/problems/word-search/description/)
```html
For example,
@ -719,7 +741,9 @@ private boolean backtracking(int curLen, int r, int c, boolean[][] visited, fina
## 4. 输出二叉树中所有从根到叶子的路径
[257. Binary Tree Paths (Easy)](https://leetcode.com/problems/binary-tree-paths/description/)
257\. Binary Tree Paths (Easy)
[Leetcode](https://leetcode.com/problems/binary-tree-paths/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-paths/description/)
```html
1
@ -777,7 +801,9 @@ private String buildPath(List<Integer> values) {
## 5. 排列
[46. Permutations (Medium)](https://leetcode.com/problems/permutations/description/)
46\. Permutations (Medium)
[Leetcode](https://leetcode.com/problems/permutations/description/) / [力扣](https://leetcode-cn.com/problems/permutations/description/)
```html
[1,2,3] have the following permutations:
@ -820,7 +846,9 @@ private void backtracking(List<Integer> permuteList, List<List<Integer>> permute
## 6. 含有相同元素求排列
[47. Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/)
47\. Permutations II (Medium)
[Leetcode](https://leetcode.com/problems/permutations-ii/description/) / [力扣](https://leetcode-cn.com/problems/permutations-ii/description/)
```html
[1,1,2] have the following unique permutations:
@ -865,7 +893,9 @@ private void backtracking(List<Integer> permuteList, List<List<Integer>> permute
## 7. 组合
[77. Combinations (Medium)](https://leetcode.com/problems/combinations/description/)
77\. Combinations (Medium)
[Leetcode](https://leetcode.com/problems/combinations/description/) / [力扣](https://leetcode-cn.com/problems/combinations/description/)
```html
If n = 4 and k = 2, a solution is:
@ -902,7 +932,9 @@ private void backtracking(List<Integer> combineList, List<List<Integer>> combina
## 8. 组合求和
[39. Combination Sum (Medium)](https://leetcode.com/problems/combination-sum/description/)
39\. Combination Sum (Medium)
[Leetcode](https://leetcode.com/problems/combination-sum/description/) / [力扣](https://leetcode-cn.com/problems/combination-sum/description/)
```html
given candidate set [2, 3, 6, 7] and target 7,
@ -936,7 +968,9 @@ private void backtracking(List<Integer> tempCombination, List<List<Integer>> com
## 9. 含有相同元素的组合求和
[40. Combination Sum II (Medium)](https://leetcode.com/problems/combination-sum-ii/description/)
40\. Combination Sum II (Medium)
[Leetcode](https://leetcode.com/problems/combination-sum-ii/description/) / [力扣](https://leetcode-cn.com/problems/combination-sum-ii/description/)
```html
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
@ -981,7 +1015,9 @@ private void backtracking(List<Integer> tempCombination, List<List<Integer>> com
## 10. 1-9 数字的组合求和
[216. Combination Sum III (Medium)](https://leetcode.com/problems/combination-sum-iii/description/)
216\. Combination Sum III (Medium)
[Leetcode](https://leetcode.com/problems/combination-sum-iii/description/) / [力扣](https://leetcode-cn.com/problems/combination-sum-iii/description/)
```html
Input: k = 3, n = 9
@ -1021,7 +1057,9 @@ private void backtracking(int k, int n, int start,
## 11. 子集
[78. Subsets (Medium)](https://leetcode.com/problems/subsets/description/)
78\. Subsets (Medium)
[Leetcode](https://leetcode.com/problems/subsets/description/) / [力扣](https://leetcode-cn.com/problems/subsets/description/)
找出集合的所有子集子集不能重复[1, 2] [2, 1] 这种子集算重复
@ -1052,7 +1090,9 @@ private void backtracking(int start, List<Integer> tempSubset, List<List<Integer
## 12. 含有相同元素求子集
[90. Subsets II (Medium)](https://leetcode.com/problems/subsets-ii/description/)
90\. Subsets II (Medium)
[Leetcode](https://leetcode.com/problems/subsets-ii/description/) / [力扣](https://leetcode-cn.com/problems/subsets-ii/description/)
```html
For example,
@ -1102,7 +1142,9 @@ private void backtracking(int start, List<Integer> tempSubset, List<List<Integer
## 13. 分割字符串使得每个部分都是回文数
[131. Palindrome Partitioning (Medium)](https://leetcode.com/problems/palindrome-partitioning/description/)
131\. Palindrome Partitioning (Medium)
[Leetcode](https://leetcode.com/problems/palindrome-partitioning/description/) / [力扣](https://leetcode-cn.com/problems/palindrome-partitioning/description/)
```html
For example, given s = "aab",
@ -1148,7 +1190,9 @@ private boolean isPalindrome(String s, int begin, int end) {
## 14. 数独
[37. Sudoku Solver (Hard)](https://leetcode.com/problems/sudoku-solver/description/)
37\. Sudoku Solver (Hard)
[Leetcode](https://leetcode.com/problems/sudoku-solver/description/) / [力扣](https://leetcode-cn.com/problems/sudoku-solver/description/)
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/0e8fdc96-83c1-4798-9abe-45fc91d70b9d.png"/> </div><br>
@ -1205,7 +1249,9 @@ private int cubeNum(int i, int j) {
## 15. N 皇后
[51. N-Queens (Hard)](https://leetcode.com/problems/n-queens/description/)
51\. N-Queens (Hard)
[Leetcode](https://leetcode.com/problems/n-queens/description/) / [力扣](https://leetcode-cn.com/problems/n-queens/description/)
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/067b310c-6877-40fe-9dcf-10654e737485.jpg"/> </div><br>

View File

@ -46,7 +46,9 @@ x 和 y 的最小公倍数为lcm(x,y) = 2<sup>max(m0,n0)</sup> \* 3<sup>max(
## 1. 生成素数序列
[204. Count Primes (Easy)](https://leetcode.com/problems/count-primes/description/)
204\. Count Primes (Easy)
[Leetcode](https://leetcode.com/problems/count-primes/description/) / [力扣](https://leetcode-cn.com/problems/count-primes/description/)
埃拉托斯特尼筛法在每次找到一个素数时将能被素数整除的数排除掉
@ -122,7 +124,9 @@ public int gcd(int a, int b) {
## 1. 7 进制
[504. Base 7 (Easy)](https://leetcode.com/problems/base-7/description/)
504\. Base 7 (Easy)
[Leetcode](https://leetcode.com/problems/base-7/description/) / [力扣](https://leetcode-cn.com/problems/base-7/description/)
```java
public String convertToBase7(int num) {
@ -153,7 +157,9 @@ public String convertToBase7(int num) {
## 2. 16 进制
[405. Convert a Number to Hexadecimal (Easy)](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/)
405\. Convert a Number to Hexadecimal (Easy)
[Leetcode](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/) / [力扣](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/description/)
```html
Input:
@ -186,7 +192,9 @@ public String toHex(int num) {
## 3. 26 进制
[168. Excel Sheet Column Title (Easy)](https://leetcode.com/problems/excel-sheet-column-title/description/)
168\. Excel Sheet Column Title (Easy)
[Leetcode](https://leetcode.com/problems/excel-sheet-column-title/description/) / [力扣](https://leetcode-cn.com/problems/excel-sheet-column-title/description/)
```html
1 -> A
@ -214,7 +222,9 @@ public String convertToTitle(int n) {
## 1. 统计阶乘尾部有多少个 0
[172. Factorial Trailing Zeroes (Easy)](https://leetcode.com/problems/factorial-trailing-zeroes/description/)
172\. Factorial Trailing Zeroes (Easy)
[Leetcode](https://leetcode.com/problems/factorial-trailing-zeroes/description/) / [力扣](https://leetcode-cn.com/problems/factorial-trailing-zeroes/description/)
尾部的 0 2 * 5 得来2 的数量明显多于 5 的数量因此只要统计有多少个 5 即可
@ -232,7 +242,9 @@ public int trailingZeroes(int n) {
## 1. 二进制加法
[67. Add Binary (Easy)](https://leetcode.com/problems/add-binary/description/)
67\. Add Binary (Easy)
[Leetcode](https://leetcode.com/problems/add-binary/description/) / [力扣](https://leetcode-cn.com/problems/add-binary/description/)
```html
a = "11"
@ -260,7 +272,9 @@ public String addBinary(String a, String b) {
## 2. 字符串加法
[415. Add Strings (Easy)](https://leetcode.com/problems/add-strings/description/)
415\. Add Strings (Easy)
[Leetcode](https://leetcode.com/problems/add-strings/description/) / [力扣](https://leetcode-cn.com/problems/add-strings/description/)
字符串的值为非负整数
@ -282,7 +296,9 @@ public String addStrings(String num1, String num2) {
## 1. 改变数组元素使所有的数组元素都相等
[462. Minimum Moves to Equal Array Elements II (Medium)](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/)
462\. Minimum Moves to Equal Array Elements II (Medium)
[Leetcode](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/) / [力扣](https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements-ii/description/)
```html
Input:
@ -378,7 +394,9 @@ private void swap(int[] nums, int i, int j) {
## 1. 数组中出现次数多于 n / 2 的元素
[169. Majority Element (Easy)](https://leetcode.com/problems/majority-element/description/)
169\. Majority Element (Easy)
[Leetcode](https://leetcode.com/problems/majority-element/description/) / [力扣](https://leetcode-cn.com/problems/majority-element/description/)
先对数组排序最中间那个数出现次数一定多于 n / 2
@ -406,7 +424,9 @@ public int majorityElement(int[] nums) {
## 1. 平方数
[367. Valid Perfect Square (Easy)](https://leetcode.com/problems/valid-perfect-square/description/)
367\. Valid Perfect Square (Easy)
[Leetcode](https://leetcode.com/problems/valid-perfect-square/description/) / [力扣](https://leetcode-cn.com/problems/valid-perfect-square/description/)
```html
Input: 16
@ -432,7 +452,9 @@ public boolean isPerfectSquare(int num) {
## 2. 3 n 次方
[326. Power of Three (Easy)](https://leetcode.com/problems/power-of-three/description/)
326\. Power of Three (Easy)
[Leetcode](https://leetcode.com/problems/power-of-three/description/) / [力扣](https://leetcode-cn.com/problems/power-of-three/description/)
```java
public boolean isPowerOfThree(int n) {
@ -442,7 +464,9 @@ public boolean isPowerOfThree(int n) {
## 3. 乘积数组
[238. Product of Array Except Self (Medium)](https://leetcode.com/problems/product-of-array-except-self/description/)
238\. Product of Array Except Self (Medium)
[Leetcode](https://leetcode.com/problems/product-of-array-except-self/description/) / [力扣](https://leetcode-cn.com/problems/product-of-array-except-self/description/)
```html
For example, given [1,2,3,4], return [24,12,8,6].
@ -473,7 +497,9 @@ public int[] productExceptSelf(int[] nums) {
## 4. 找出数组中的乘积最大的三个数
[628. Maximum Product of Three Numbers (Easy)](https://leetcode.com/problems/maximum-product-of-three-numbers/description/)
628\. Maximum Product of Three Numbers (Easy)
[Leetcode](https://leetcode.com/problems/maximum-product-of-three-numbers/description/) / [力扣](https://leetcode-cn.com/problems/maximum-product-of-three-numbers/description/)
```html
Input: [1,2,3,4]

View File

@ -16,7 +16,9 @@
# 1. 把数组中的 0 移到末尾
[283. Move Zeroes (Easy)](https://leetcode.com/problems/move-zeroes/description/)
283\. Move Zeroes (Easy)
[Leetcode](https://leetcode.com/problems/move-zeroes/description/) / [力扣](https://leetcode-cn.com/problems/move-zeroes/description/)
```html
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
@ -38,7 +40,9 @@ public void moveZeroes(int[] nums) {
# 2. 改变矩阵维度
[566. Reshape the Matrix (Easy)](https://leetcode.com/problems/reshape-the-matrix/description/)
566\. Reshape the Matrix (Easy)
[Leetcode](https://leetcode.com/problems/reshape-the-matrix/description/) / [力扣](https://leetcode-cn.com/problems/reshape-the-matrix/description/)
```html
Input:
@ -74,7 +78,9 @@ public int[][] matrixReshape(int[][] nums, int r, int c) {
# 3. 找出数组中最长的连续 1
[485. Max Consecutive Ones (Easy)](https://leetcode.com/problems/max-consecutive-ones/description/)
485\. Max Consecutive Ones (Easy)
[Leetcode](https://leetcode.com/problems/max-consecutive-ones/description/) / [力扣](https://leetcode-cn.com/problems/max-consecutive-ones/description/)
```java
public int findMaxConsecutiveOnes(int[] nums) {
@ -89,7 +95,9 @@ public int findMaxConsecutiveOnes(int[] nums) {
# 4. 有序矩阵查找
[240. Search a 2D Matrix II (Medium)](https://leetcode.com/problems/search-a-2d-matrix-ii/description/)
240\. Search a 2D Matrix II (Medium)
[Leetcode](https://leetcode.com/problems/search-a-2d-matrix-ii/description/) / [力扣](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/description/)
```html
[
@ -115,7 +123,9 @@ public boolean searchMatrix(int[][] matrix, int target) {
# 5. 有序矩阵的 Kth Element
[378. Kth Smallest Element in a Sorted Matrix ((Medium))](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/)
378\. Kth Smallest Element in a Sorted Matrix ((Medium))
[Leetcode](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/) / [力扣](https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/description/)
```html
matrix = [
@ -128,7 +138,9 @@ k = 8,
return 13.
```
解题参考[Share my thoughts and Clean Java Code](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173)
<EFBFBD><EFBFBD>题参考[Share my thoughts and Clean Java Code
[Leetcode](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173) / [力扣](https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173)
二分查找解法
@ -181,7 +193,9 @@ class Tuple implements Comparable<Tuple> {
# 6. 一个数组元素在 [1, n] 之间其中一个数被替换为另一个数找出重复的数和丢失的数
[645. Set Mismatch (Easy)](https://leetcode.com/problems/set-mismatch/description/)
645\. Set Mismatch (Easy)
[Leetcode](https://leetcode.com/problems/set-mismatch/description/) / [力扣](https://leetcode-cn.com/problems/set-mismatch/description/)
```html
Input: nums = [1,2,2,4]
@ -221,12 +235,18 @@ private void swap(int[] nums, int i, int j) {
类似题目
- [448. Find All Numbers Disappeared in an Array (Easy)](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/)寻找所有丢失的元素
- [442. Find All Duplicates in an Array (Medium)](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/)寻找所有重复的元素
[448\. Find All Numbers Disappeared in an Array (Easy)
[Leetcode](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/)寻找所有丢失的元<EFBFBD>) / [力扣](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/description/)寻找所有丢失的元<EFBFBD>)
[442\. Find All Duplicates in an Array (Medium)
[Leetcode](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/)寻找所有重复的元素<EFBFBD>) / [力扣](https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/description/)寻找所有重复的元素<EFBFBD>)
# 7. 找出数组中重复的数数组值在 [1, n] 之间
[287. Find the Duplicate Number (Medium)](https://leetcode.com/problems/find-the-duplicate-number/description/)
287\. Find the Duplicate Number (Medium)
[Leetcode](https://leetcode.com/problems/find-the-duplicate-number/description/) / [力扣](https://leetcode-cn.com/problems/find-the-duplicate-number/description/)
要求不能修改数组也不能使用额外的空间
@ -268,7 +288,9 @@ public int findDuplicate(int[] nums) {
# 8. 数组相邻差值的个数
[667. Beautiful Arrangement II (Medium)](https://leetcode.com/problems/beautiful-arrangement-ii/description/)
667\. Beautiful Arrangement II (Medium)
[Leetcode](https://leetcode.com/problems/beautiful-arrangement-ii/description/) / [力扣](https://leetcode-cn.com/problems/beautiful-arrangement-ii/description/)
```html
Input: n = 3, k = 2
@ -296,7 +318,9 @@ public int[] constructArray(int n, int k) {
# 9. 数组的度
[697. Degree of an Array (Easy)](https://leetcode.com/problems/degree-of-an-array/description/)
697\. Degree of an Array (Easy)
[Leetcode](https://leetcode.com/problems/degree-of-an-array/description/) / [力扣](https://leetcode-cn.com/problems/degree-of-an-array/description/)
```html
Input: [1,2,2,3,1,4,2]
@ -335,7 +359,9 @@ public int findShortestSubArray(int[] nums) {
# 10. 对角元素相等的矩阵
[766. Toeplitz Matrix (Easy)](https://leetcode.com/problems/toeplitz-matrix/description/)
766\. Toeplitz Matrix (Easy)
[Leetcode](https://leetcode.com/problems/toeplitz-matrix/description/) / [力扣](https://leetcode-cn.com/problems/toeplitz-matrix/description/)
```html
1234
@ -373,7 +399,9 @@ private boolean check(int[][] matrix, int expectValue, int row, int col) {
# 11. 嵌套数组
[565. Array Nesting (Medium)](https://leetcode.com/problems/array-nesting/description/)
565\. Array Nesting (Medium)
[Leetcode](https://leetcode.com/problems/array-nesting/description/) / [力扣](https://leetcode-cn.com/problems/array-nesting/description/)
```html
Input: A = [5,4,0,3,1,6,2]
@ -407,7 +435,9 @@ public int arrayNesting(int[] nums) {
# 12. 分隔数组
[769. Max Chunks To Make Sorted (Medium)](https://leetcode.com/problems/max-chunks-to-make-sorted/description/)
769\. Max Chunks To Make Sorted (Medium)
[Leetcode](https://leetcode.com/problems/max-chunks-to-make-sorted/description/) / [力扣](https://leetcode-cn.com/problems/max-chunks-to-make-sorted/description/)
```html
Input: arr = [1,0,2,3,4]

View File

@ -10,7 +10,9 @@
# 1. 用栈实现队列
[232. Implement Queue using Stacks (Easy)](https://leetcode.com/problems/implement-queue-using-stacks/description/)
232\. Implement Queue using Stacks (Easy)
[Leetcode](https://leetcode.com/problems/implement-queue-using-stacks/description/) / [力扣](https://leetcode-cn.com/problems/implement-queue-using-stacks/description/)
栈的顺序为后进先出而队列的顺序为先进先出使用两个栈实现队列一个元素需要经过两个栈才能出队列在经过第一个栈时元素顺序被反转经过第二个栈时再次被反转此时就是先进先出顺序
@ -50,7 +52,9 @@ class MyQueue {
# 2. 用队列实现栈
[225. Implement Stack using Queues (Easy)](https://leetcode.com/problems/implement-stack-using-queues/description/)
225\. Implement Stack using Queues (Easy)
[Leetcode](https://leetcode.com/problems/implement-stack-using-queues/description/) / [力扣](https://leetcode-cn.com/problems/implement-stack-using-queues/description/)
在将一个元素 x 插入队列时为了维护原来的后进先出顺序需要让 x 插入队列首部而队列的默认插入顺序是队列尾部因此在将 x 插入队列尾部之后需要让除了 x 之外的所有元素出队列再入队列
@ -87,7 +91,9 @@ class MyStack {
# 3. 最小值栈
[155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/)
155\. Min Stack (Easy)
[Leetcode](https://leetcode.com/problems/min-stack/description/) / [力扣](https://leetcode-cn.com/problems/min-stack/description/)
```java
class MinStack {
@ -128,7 +134,9 @@ class MinStack {
# 4. 用栈实现括号匹配
[20. Valid Parentheses (Easy)](https://leetcode.com/problems/valid-parentheses/description/)
20\. Valid Parentheses (Easy)
[Leetcode](https://leetcode.com/problems/valid-parentheses/description/) / [力扣](https://leetcode-cn.com/problems/valid-parentheses/description/)
```html
"()[]{}"
@ -161,7 +169,9 @@ public boolean isValid(String s) {
# 5. 数组中元素与下一个比它大的元素之间的距离
[739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/)
739\. Daily Temperatures (Medium)
[Leetcode](https://leetcode.com/problems/daily-temperatures/description/) / [力扣](https://leetcode-cn.com/problems/daily-temperatures/description/)
```html
Input: [73, 74, 75, 71, 69, 72, 76, 73]
@ -188,7 +198,9 @@ public int[] dailyTemperatures(int[] temperatures) {
# 6. 循环数组中比当前元素大的下一个元素
[503. Next Greater Element II (Medium)](https://leetcode.com/problems/next-greater-element-ii/description/)
503\. Next Greater Element II (Medium)
[Leetcode](https://leetcode.com/problems/next-greater-element-ii/description/) / [力扣](https://leetcode-cn.com/problems/next-greater-element-ii/description/)
```text
Input: [1,2,1]

View File

@ -44,7 +44,9 @@
## 1. 树的高度
[104. Maximum Depth of Binary Tree (Easy)](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/)
104\. Maximum Depth of Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/)
```java
public int maxDepth(TreeNode root) {
@ -55,7 +57,9 @@ public int maxDepth(TreeNode root) {
## 2. 平衡树
[110. Balanced Binary Tree (Easy)](https://leetcode.com/problems/balanced-binary-tree/description/)
110\. Balanced Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/balanced-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/balanced-binary-tree/description/)
```html
3
@ -86,7 +90,9 @@ public int maxDepth(TreeNode root) {
## 3. 两节点的最长路径
[543. Diameter of Binary Tree (Easy)](https://leetcode.com/problems/diameter-of-binary-tree/description/)
543\. Diameter of Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/diameter-of-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/diameter-of-binary-tree/description/)
```html
Input:
@ -119,7 +125,9 @@ private int depth(TreeNode root) {
## 4. 翻转树
[226. Invert Binary Tree (Easy)](https://leetcode.com/problems/invert-binary-tree/description/)
226\. Invert Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/invert-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/invert-binary-tree/description/)
```java
public TreeNode invertTree(TreeNode root) {
@ -133,7 +141,9 @@ public TreeNode invertTree(TreeNode root) {
## 5. 归并两棵树
[617. Merge Two Binary Trees (Easy)](https://leetcode.com/problems/merge-two-binary-trees/description/)
617\. Merge Two Binary Trees (Easy)
[Leetcode](https://leetcode.com/problems/merge-two-binary-trees/description/) / [力扣](https://leetcode-cn.com/problems/merge-two-binary-trees/description/)
```html
Input:
@ -166,7 +176,9 @@ public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
## 6. 判断路径和是否等于一个数
[Leetcdoe : 112. Path Sum (Easy)](https://leetcode.com/problems/path-sum/description/)
Leetcdoe : 112. Path Sum (Easy)
[Leetcode](https://leetcode.com/problems/path-sum/description/) / [力扣](https://leetcode-cn.com/problems/path-sum/description/)
```html
Given the below binary tree and sum = 22,
@ -194,7 +206,9 @@ public boolean hasPathSum(TreeNode root, int sum) {
## 7. 统计路径和等于一个数的路径数量
[437. Path Sum III (Easy)](https://leetcode.com/problems/path-sum-iii/description/)
437\. Path Sum III (Easy)
[Leetcode](https://leetcode.com/problems/path-sum-iii/description/) / [力扣](https://leetcode-cn.com/problems/path-sum-iii/description/)
```html
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
@ -234,7 +248,9 @@ private int pathSumStartWithRoot(TreeNode root, int sum) {
## 8. 子树
[572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/)
572\. Subtree of Another Tree (Easy)
[Leetcode](https://leetcode.com/problems/subtree-of-another-tree/description/) / [力扣](https://leetcode-cn.com/problems/subtree-of-another-tree/description/)
```html
Given tree s:
@ -285,7 +301,9 @@ private boolean isSubtreeWithRoot(TreeNode s, TreeNode t) {
## 9. 树的对称
[101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/)
101\. Symmetric Tree (Easy)
[Leetcode](https://leetcode.com/problems/symmetric-tree/description/) / [力扣](https://leetcode-cn.com/problems/symmetric-tree/description/)
```html
1
@ -311,7 +329,9 @@ private boolean isSymmetric(TreeNode t1, TreeNode t2) {
## 10. 最小路径
[111. Minimum Depth of Binary Tree (Easy)](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/)
111\. Minimum Depth of Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/)
树的根节点到叶子节点的最小路径长度
@ -327,7 +347,9 @@ public int minDepth(TreeNode root) {
## 11. 统计左叶子节点的和
[404. Sum of Left Leaves (Easy)](https://leetcode.com/problems/sum-of-left-leaves/description/)
404\. Sum of Left Leaves (Easy)
[Leetcode](https://leetcode.com/problems/sum-of-left-leaves/description/) / [力扣](https://leetcode-cn.com/problems/sum-of-left-leaves/description/)
```html
3
@ -354,7 +376,9 @@ private boolean isLeaf(TreeNode node){
## 12. 相同节点值的最大路径长度
[687. Longest Univalue Path (Easy)](https://leetcode.com/problems/longest-univalue-path/)
687\. Longest Univalue Path (Easy)
[Leetcode](https://leetcode.com/problems/longest-univalue-path/) / [力扣](https://leetcode-cn.com/problems/longest-univalue-path/)
```html
1
@ -387,7 +411,9 @@ private int dfs(TreeNode root){
## 13. 间隔遍历
[337. House Robber III (Medium)](https://leetcode.com/problems/house-robber-iii/description/)
337\. House Robber III (Medium)
[Leetcode](https://leetcode.com/problems/house-robber-iii/description/) / [力扣](https://leetcode-cn.com/problems/house-robber-iii/description/)
```html
3
@ -411,7 +437,9 @@ public int rob(TreeNode root) {
## 14. 找出二叉树中第二小的节点
[671. Second Minimum Node In a Binary Tree (Easy)](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/)
671\. Second Minimum Node In a Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree/description/)
```html
Input:
@ -446,7 +474,9 @@ public int findSecondMinimumValue(TreeNode root) {
## 1. 一棵树每层节点的平均数
[637. Average of Levels in Binary Tree (Easy)](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/)
637\. Average of Levels in Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/description/)
```java
public List<Double> averageOfLevels(TreeNode root) {
@ -471,7 +501,9 @@ public List<Double> averageOfLevels(TreeNode root) {
## 2. 得到左下角的节点
[513. Find Bottom Left Tree Value (Easy)](https://leetcode.com/problems/find-bottom-left-tree-value/description/)
513\. Find Bottom Left Tree Value (Easy)
[Leetcode](https://leetcode.com/problems/find-bottom-left-tree-value/description/) / [力扣](https://leetcode-cn.com/problems/find-bottom-left-tree-value/description/)
```html
Input:
@ -552,7 +584,9 @@ void dfs(TreeNode root) {
## 1. 非递归实现二叉树的前序遍历
[144. Binary Tree Preorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-preorder-traversal/description/)
144\. Binary Tree Preorder Traversal (Medium)
[Leetcode](https://leetcode.com/problems/binary-tree-preorder-traversal/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/)
```java
public List<Integer> preorderTraversal(TreeNode root) {
@ -572,7 +606,9 @@ public List<Integer> preorderTraversal(TreeNode root) {
## 2. 非递归实现二叉树的后序遍历
[145. Binary Tree Postorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-postorder-traversal/description/)
145\. Binary Tree Postorder Traversal (Medium)
[Leetcode](https://leetcode.com/problems/binary-tree-postorder-traversal/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/)
前序遍历为 root -> left -> right后序遍历为 left -> right -> root可以修改前序遍历成为 root -> right -> left那么这个顺序就和后序遍历正好相反
@ -595,7 +631,9 @@ public List<Integer> postorderTraversal(TreeNode root) {
## 3. 非递归实现二叉树的中序遍历
[94. Binary Tree Inorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-inorder-traversal/description/)
94\. Binary Tree Inorder Traversal (Medium)
[Leetcode](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/)
```java
public List<Integer> inorderTraversal(TreeNode root) {
@ -624,7 +662,9 @@ public List<Integer> inorderTraversal(TreeNode root) {
## 1. 修剪二叉查找树
[669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/)
669\. Trim a Binary Search Tree (Easy)
[Leetcode](https://leetcode.com/problems/trim-a-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/trim-a-binary-search-tree/description/)
```html
Input:
@ -664,7 +704,9 @@ public TreeNode trimBST(TreeNode root, int L, int R) {
## 2. 寻找二叉查找树的第 k 个元素
[230. Kth Smallest Element in a BST (Medium)](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/)
230\. Kth Smallest Element in a BST (Medium)
[Leetcode](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/) / [力扣](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/description/)
中序遍历解法
@ -708,7 +750,9 @@ private int count(TreeNode node) {
## 3. 把二叉查找树每个节点的值都加上比它大的节点的值
[Convert BST to Greater Tree (Easy)](https://leetcode.com/problems/convert-bst-to-greater-tree/description/)
Convert BST to Greater Tree (Easy)
[Leetcode](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) / [力扣](https://leetcode-cn.com/problems/convert-bst-to-greater-tree/description/)
```html
Input: The root of a Binary Search Tree like this:
@ -745,7 +789,9 @@ private void traver(TreeNode node) {
## 4. 二叉查找树的最近公共祖先
[235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
235\. Lowest Common Ancestor of a Binary Search Tree (Easy)
[Leetcode](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
```html
_______6______
@ -769,7 +815,9 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
## 5. 二叉树的最近公共祖先
[236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
236\. Lowest Common Ancestor of a Binary Tree (Medium)
[Leetcode](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
```html
_______3______
@ -794,7 +842,9 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
## 6. 从有序数组中构造二叉查找树
[108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
108\. Convert Sorted Array to Binary Search Tree (Easy)
[Leetcode](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/description/)
```java
public TreeNode sortedArrayToBST(int[] nums) {
@ -813,7 +863,9 @@ private TreeNode toBST(int[] nums, int sIdx, int eIdx){
## 7. 根据有序链表构造平衡的二叉查找树
[109. Convert Sorted List to Binary Search Tree (Medium)](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/)
109\. Convert Sorted List to Binary Search Tree (Medium)
[Leetcode](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/description/)
```html
Given the sorted linked list: [-10,-3,0,5,9],
@ -854,7 +906,9 @@ private ListNode preMid(ListNode head) {
## 8. 在二叉查找树中寻找两个节点使它们的和为一个给定值
[653. Two Sum IV - Input is a BST (Easy)](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/)
653\. Two Sum IV - Input is a BST (Easy)
[Leetcode](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/) / [力扣](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/description/)
```html
Input:
@ -898,7 +952,9 @@ private void inOrder(TreeNode root, List<Integer> nums) {
## 9. 在二叉查找树中查找两个节点之差的最小绝对值
[530. Minimum Absolute Difference in BST (Easy)](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/)
530\. Minimum Absolute Difference in BST (Easy)
[Leetcode](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) / [力扣](https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/description/)
```html
Input:
@ -936,7 +992,9 @@ private void inOrder(TreeNode node) {
## 10. 寻找二叉查找树中出现次数最多的值
[501. Find Mode in Binary Search Tree (Easy)](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/)
501\. Find Mode in Binary Search Tree (Easy)
[Leetcode](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/description/)
```html
1
@ -993,7 +1051,9 @@ Trie又称前缀树或字典树用于判断字符串是否存在或者是
## 1. 实现一个 Trie
[208. Implement Trie (Prefix Tree) (Medium)](https://leetcode.com/problems/implement-trie-prefix-tree/description/)
208\. Implement Trie (Prefix Tree) (Medium)
[Leetcode](https://leetcode.com/problems/implement-trie-prefix-tree/description/) / [力扣](https://leetcode-cn.com/problems/implement-trie-prefix-tree/description/)
```java
class Trie {
@ -1055,7 +1115,9 @@ class Trie {
## 2. 实现一个 Trie用来求前缀和
[677. Map Sum Pairs (Medium)](https://leetcode.com/problems/map-sum-pairs/description/)
677\. Map Sum Pairs (Medium)
[Leetcode](https://leetcode.com/problems/map-sum-pairs/description/) / [力扣](https://leetcode-cn.com/problems/map-sum-pairs/description/)
```html
Input: insert("apple", 3), Output: Null

View File

@ -17,7 +17,9 @@
# 1. 分配饼干
[455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/)
455\. Assign Cookies (Easy)
[Leetcode](https://leetcode.com/problems/assign-cookies/description/) / [力扣](https://leetcode-cn.com/problems/assign-cookies/description/)
```html
Input: [1,2], [1,2,3]
@ -51,7 +53,9 @@ public int findContentChildren(int[] g, int[] s) {
# 2. 不重叠的区间个数
[435. Non-overlapping Intervals (Medium)](https://leetcode.com/problems/non-overlapping-intervals/description/)
435\. Non-overlapping Intervals (Medium)
[Leetcode](https://leetcode.com/problems/non-overlapping-intervals/description/) / [力扣](https://leetcode-cn.com/problems/non-overlapping-intervals/description/)
```html
Input: [ [1,2], [1,2], [1,2] ]
@ -109,7 +113,9 @@ Arrays.sort(intervals, new Comparator<int[]>() {
# 3. 投飞镖刺破气球
[452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
452\. Minimum Number of Arrows to Burst Balloons (Medium)
[Leetcode](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/) / [力扣](https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
```
Input:
@ -143,7 +149,9 @@ public int findMinArrowShots(int[][] points) {
# 4. 根据身高和序号重组队列
[406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/)
406\. Queue Reconstruction by Height(Medium)
[Leetcode](https://leetcode.com/problems/queue-reconstruction-by-height/description/) / [力扣](https://leetcode-cn.com/problems/queue-reconstruction-by-height/description/)
```html
Input:
@ -175,7 +183,9 @@ public int[][] reconstructQueue(int[][] people) {
# 5. 买卖股票最大的收益
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
121\. Best Time to Buy and Sell Stock (Easy)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/)
题目描述一次股票交易包含买入和卖出只进行一次交易求最大收益
@ -198,7 +208,9 @@ public int maxProfit(int[] prices) {
# 6. 买卖股票的最大收益 II
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
122\. Best Time to Buy and Sell Stock II (Easy)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
题目描述可以进行多次交易多次交易之间不能交叉进行可以进行多次交易
@ -219,7 +231,9 @@ public int maxProfit(int[] prices) {
# 7. 种植花朵
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
605\. Can Place Flowers (Easy)
[Leetcode](https://leetcode.com/problems/can-place-flowers/description/) / [力扣](https://leetcode-cn.com/problems/can-place-flowers/description/)
```html
Input: flowerbed = [1,0,0,0,1], n = 1
@ -249,7 +263,9 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) {
# 8. 判断是否为子序列
[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/)
392\. Is Subsequence (Medium)
[Leetcode](https://leetcode.com/problems/is-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/is-subsequence/description/)
```html
s = "abc", t = "ahbgdc"
@ -271,7 +287,9 @@ public boolean isSubsequence(String s, String t) {
# 9. 修改一个数成为非递减数组
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
665\. Non-decreasing Array (Easy)
[Leetcode](https://leetcode.com/problems/non-decreasing-array/description/) / [力扣](https://leetcode-cn.com/problems/non-decreasing-array/description/)
```html
Input: [4,2,3]
@ -305,7 +323,9 @@ public boolean checkPossibility(int[] nums) {
# 10. 子数组最大的和
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
53\. Maximum Subarray (Easy)
[Leetcode](https://leetcode.com/problems/maximum-subarray/description/) / [力扣](https://leetcode-cn.com/problems/maximum-subarray/description/)
```html
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
@ -329,7 +349,9 @@ public int maxSubArray(int[] nums) {
# 11. 分隔字符串使同种字符出现在一起
[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
763\. Partition Labels (Medium)
[Leetcode](https://leetcode.com/problems/partition-labels/description/) / [力扣](https://leetcode-cn.com/problems/partition-labels/description/)
```html
Input: S = "ababcbacadefegdehijhklij"

View File

@ -16,7 +16,9 @@
# 1. 找出两个链表的交点
[160. Intersection of Two Linked Lists (Easy)](https://leetcode.com/problems/intersection-of-two-linked-lists/description/)
160\. Intersection of Two Linked Lists (Easy)
[Leetcode](https://leetcode.com/problems/intersection-of-two-linked-lists/description/) / [力扣](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/)
例如以下示例中 A B 两个链表相交于 c1
@ -66,7 +68,9 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
# 2. 链表反转
[206. Reverse Linked List (Easy)](https://leetcode.com/problems/reverse-linked-list/description/)
206\. Reverse Linked List (Easy)
[Leetcode](https://leetcode.com/problems/reverse-linked-list/description/) / [力扣](https://leetcode-cn.com/problems/reverse-linked-list/description/)
递归
@ -100,7 +104,9 @@ public ListNode reverseList(ListNode head) {
# 3. 归并两个有序的链表
[21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists/description/)
21\. Merge Two Sorted Lists (Easy)
[Leetcode](https://leetcode.com/problems/merge-two-sorted-lists/description/) / [力扣](https://leetcode-cn.com/problems/merge-two-sorted-lists/description/)
```java
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
@ -118,7 +124,9 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
# 4. 从有序链表中删除重复节点
[83. Remove Duplicates from Sorted List (Easy)](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/)
83\. Remove Duplicates from Sorted List (Easy)
[Leetcode](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/) / [力扣](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/)
```html
Given 1->1->2, return 1->2.
@ -135,7 +143,9 @@ public ListNode deleteDuplicates(ListNode head) {
# 5. 删除链表的倒数第 n 个节点
[19. Remove Nth Node From End of List (Medium)](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)
19\. Remove Nth Node From End of List (Medium)
[Leetcode](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) / [力扣](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/)
```html
Given linked list: 1->2->3->4->5, and n = 2.
@ -161,7 +171,9 @@ public ListNode removeNthFromEnd(ListNode head, int n) {
# 6. 交换链表中的相邻结点
[24. Swap Nodes in Pairs (Medium)](https://leetcode.com/problems/swap-nodes-in-pairs/description/)
24\. Swap Nodes in Pairs (Medium)
[Leetcode](https://leetcode.com/problems/swap-nodes-in-pairs/description/) / [力扣](https://leetcode-cn.com/problems/swap-nodes-in-pairs/description/)
```html
Given 1->2->3->4, you should return the list as 2->1->4->3.
@ -189,7 +201,9 @@ public ListNode swapPairs(ListNode head) {
# 7. 链表求和
[445. Add Two Numbers II (Medium)](https://leetcode.com/problems/add-two-numbers-ii/description/)
445\. Add Two Numbers II (Medium)
[Leetcode](https://leetcode.com/problems/add-two-numbers-ii/description/) / [力扣](https://leetcode-cn.com/problems/add-two-numbers-ii/description/)
```html
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
@ -228,7 +242,9 @@ private Stack<Integer> buildStack(ListNode l) {
# 8. 回文链表
[234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/)
234\. Palindrome Linked List (Easy)
[Leetcode](https://leetcode.com/problems/palindrome-linked-list/description/) / [力扣](https://leetcode-cn.com/problems/palindrome-linked-list/description/)
题目要求 O(1) 的空间复杂度来求解
@ -277,7 +293,9 @@ private boolean isEqual(ListNode l1, ListNode l2) {
# 9. 分隔链表
[725. Split Linked List in Parts(Medium)](https://leetcode.com/problems/split-linked-list-in-parts/description/)
725\. Split Linked List in Parts(Medium)
[Leetcode](https://leetcode.com/problems/split-linked-list-in-parts/description/) / [力扣](https://leetcode-cn.com/problems/split-linked-list-in-parts/description/)
```html
Input:
@ -317,7 +335,9 @@ public ListNode[] splitListToParts(ListNode root, int k) {
# 10. 链表元素按奇偶聚集
[328. Odd Even Linked List (Medium)](https://leetcode.com/problems/odd-even-linked-list/description/)
328\. Odd Even Linked List (Medium)
[Leetcode](https://leetcode.com/problems/odd-even-linked-list/description/) / [力扣](https://leetcode-cn.com/problems/odd-even-linked-list/description/)
```html
Example:

View File

@ -96,7 +96,9 @@ l m h
# 1. 求开方
[69. Sqrt(x) (Easy)](https://leetcode.com/problems/sqrtx/description/)
69\. Sqrt(x) (Easy)
[Leetcode](https://leetcode.com/problems/sqrtx/description/) / [力扣](https://leetcode-cn.com/problems/sqrtx/description/)
```html
Input: 4
@ -134,7 +136,9 @@ public int mySqrt(int x) {
# 2. 大于给定元素的最小元素
[744. Find Smallest Letter Greater Than Target (Easy)](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/)
744\. Find Smallest Letter Greater Than Target (Easy)
[Leetcode](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/) / [力扣](https://leetcode-cn.com/problems/find-smallest-letter-greater-than-target/description/)
```html
Input:
@ -168,7 +172,9 @@ public char nextGreatestLetter(char[] letters, char target) {
# 3. 有序数组的 Single Element
[540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/)
540\. Single Element in a Sorted Array (Medium)
[Leetcode](https://leetcode.com/problems/single-element-in-a-sorted-array/description/) / [力扣](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/description/)
```html
Input: [1, 1, 2, 3, 3, 4, 4, 8, 8]
@ -205,7 +211,9 @@ public int singleNonDuplicate(int[] nums) {
# 4. 第一个错误的版本
[278. First Bad Version (Easy)](https://leetcode.com/problems/first-bad-version/description/)
278\. First Bad Version (Easy)
[Leetcode](https://leetcode.com/problems/first-bad-version/description/) / [力扣](https://leetcode-cn.com/problems/first-bad-version/description/)
题目描述给定一个元素 n 代表有 [1, 2, ..., n] 版本在第 x 位置开始出现错误版本导致后面的版本都错误可以调用 isBadVersion(int x) 知道某个版本是否错误要求找到第一个错误的版本
@ -230,7 +238,9 @@ public int firstBadVersion(int n) {
# 5. 旋转数组的最小数字
[153. Find Minimum in Rotated Sorted Array (Medium)](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/)
153\. Find Minimum in Rotated Sorted Array (Medium)
[Leetcode](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/) / [力扣](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description/)
```html
Input: [3,4,5,1,2],
@ -254,7 +264,9 @@ public int findMin(int[] nums) {
# 6. 查找区间
[34. Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
34\. Find First and Last Position of Element in Sorted Array
[Leetcode](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) / [力扣](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
```html
Input: nums = [5,7,7,8,8,10], target = 8

View File

@ -61,7 +61,9 @@ static String toBinaryString(int i); // 转换为二进制表示的字符串
# 1. 统计两个数的二进制表示有多少位不同
[461. Hamming Distance (Easy)](https://leetcode.com/problems/hamming-distance/)
461. Hamming Distance (Easy)
[Leetcode](https://leetcode.com/problems/hamming-distance/) / [力扣](https://leetcode-cn.com/problems/hamming-distance/)
```html
Input: x = 1, y = 4
@ -114,7 +116,9 @@ public int hammingDistance(int x, int y) {
# 2. 数组中唯一一个不重复的元素
[136. Single Number (Easy)](https://leetcode.com/problems/single-number/description/)
136\. Single Number (Easy)
[Leetcode](https://leetcode.com/problems/single-number/description/) / [力扣](https://leetcode-cn.com/problems/single-number/description/)
```html
Input: [4,1,2,1,2]
@ -133,7 +137,9 @@ public int singleNumber(int[] nums) {
# 3. 找出数组中缺失的那个数
[268. Missing Number (Easy)](https://leetcode.com/problems/missing-number/description/)
268\. Missing Number (Easy)
[Leetcode](https://leetcode.com/problems/missing-number/description/) / [力扣](https://leetcode-cn.com/problems/missing-number/description/)
```html
Input: [3,0,1]
@ -154,7 +160,9 @@ public int missingNumber(int[] nums) {
# 4. 数组中不重复的两个元素
[260. Single Number III (Medium)](https://leetcode.com/problems/single-number-iii/description/)
260\. Single Number III (Medium)
[Leetcode](https://leetcode.com/problems/single-number-iii/description/) / [力扣](https://leetcode-cn.com/problems/single-number-iii/description/)
两个不相等的元素在位级表示上必定会有一位存在不同
@ -178,7 +186,9 @@ public int[] singleNumber(int[] nums) {
# 5. 翻转一个数的比特位
[190. Reverse Bits (Easy)](https://leetcode.com/problems/reverse-bits/description/)
190\. Reverse Bits (Easy)
[Leetcode](https://leetcode.com/problems/reverse-bits/description/) / [力扣](https://leetcode-cn.com/problems/reverse-bits/description/)
```java
public int reverseBits(int n) {
@ -233,7 +243,9 @@ a = a ^ b;
# 7. 判断一个数是不是 2 n 次方
[231. Power of Two (Easy)](https://leetcode.com/problems/power-of-two/description/)
231\. Power of Two (Easy)
[Leetcode](https://leetcode.com/problems/power-of-two/description/) / [力扣](https://leetcode-cn.com/problems/power-of-two/description/)
二进制表示只有一个 1 存在
@ -253,7 +265,9 @@ public boolean isPowerOfTwo(int n) {
# 8. 判断一个数是不是 4 n 次方
[342. Power of Four (Easy)](https://leetcode.com/problems/power-of-four/)
342\. Power of Four (Easy)
[Leetcode](https://leetcode.com/problems/power-of-four/) / [力扣](https://leetcode-cn.com/problems/power-of-four/)
这种数在二进制表示中有且只有一个奇数位为 1例如 1610000
@ -273,7 +287,9 @@ public boolean isPowerOfFour(int num) {
# 9. 判断一个数的位级表示是否不会出现连续的 0 1
[693. Binary Number with Alternating Bits (Easy)](https://leetcode.com/problems/binary-number-with-alternating-bits/description/)
693\. Binary Number with Alternating Bits (Easy)
[Leetcode](https://leetcode.com/problems/binary-number-with-alternating-bits/description/) / [力扣](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/description/)
```html
Input: 10
@ -298,7 +314,9 @@ public boolean hasAlternatingBits(int n) {
# 10. 求一个数的补码
[476. Number Complement (Easy)](https://leetcode.com/problems/number-complement/description/)
476\. Number Complement (Easy)
[Leetcode](https://leetcode.com/problems/number-complement/description/) / [力扣](https://leetcode-cn.com/problems/number-complement/description/)
```html
Input: 5
@ -353,7 +371,9 @@ public int findComplement(int num) {
# 11. 实现整数的加法
[371. Sum of Two Integers (Easy)](https://leetcode.com/problems/sum-of-two-integers/description/)
371\. Sum of Two Integers (Easy)
[Leetcode](https://leetcode.com/problems/sum-of-two-integers/description/) / [力扣](https://leetcode-cn.com/problems/sum-of-two-integers/description/)
a ^ b 表示没有考虑进位的情况下两数的和(a & b) << 1 就是进位
@ -367,7 +387,9 @@ public int getSum(int a, int b) {
# 12. 字符串数组最大乘积
[318. Maximum Product of Word Lengths (Medium)](https://leetcode.com/problems/maximum-product-of-word-lengths/description/)
318\. Maximum Product of Word Lengths (Medium)
[Leetcode](https://leetcode.com/problems/maximum-product-of-word-lengths/description/) / [力扣](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/description/)
```html
Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
@ -402,7 +424,9 @@ public int maxProduct(String[] words) {
# 13. 统计从 0 \~ n 每个数的二进制表示中 1 的个数
[338. Counting Bits (Medium)](https://leetcode.com/problems/counting-bits/description/)
338\. Counting Bits (Medium)
[Leetcode](https://leetcode.com/problems/counting-bits/description/) / [力扣](https://leetcode-cn.com/problems/counting-bits/description/)
对于数字 6(110)它可以看成是 4(100) 再加一个 2(10)因此 dp[i] = dp[i&(i-1)] + 1;

View File

@ -6,7 +6,9 @@
# 1. 给表达式加括号
[241. Different Ways to Add Parentheses (Medium)](https://leetcode.com/problems/different-ways-to-add-parentheses/description/)
241\. Different Ways to Add Parentheses (Medium)
[Leetcode](https://leetcode.com/problems/different-ways-to-add-parentheses/description/) / [力扣](https://leetcode-cn.com/problems/different-ways-to-add-parentheses/description/)
```html
Input: "2-1-1".
@ -51,7 +53,9 @@ public List<Integer> diffWaysToCompute(String input) {
# 2. 不同的二叉搜索树
[95. Unique Binary Search Trees II (Medium)](https://leetcode.com/problems/unique-binary-search-trees-ii/description/)
95\. Unique Binary Search Trees II (Medium)
[Leetcode](https://leetcode.com/problems/unique-binary-search-trees-ii/description/) / [力扣](https://leetcode-cn.com/problems/unique-binary-search-trees-ii/description/)
给定一个数字 n要求生成所有值为 1...n 的二叉搜索树

View File

@ -46,7 +46,9 @@
## 1. 爬楼梯
[70. Climbing Stairs (Easy)](https://leetcode.com/problems/climbing-stairs/description/)
70\. Climbing Stairs (Easy)
[Leetcode](https://leetcode.com/problems/climbing-stairs/description/) / [力扣](https://leetcode-cn.com/problems/climbing-stairs/description/)
题目描述 N 阶楼梯每次可以上一阶或者两阶求有多少种上楼梯的方法
@ -77,7 +79,9 @@ public int climbStairs(int n) {
## 2. 强盗抢劫
[198. House Robber (Easy)](https://leetcode.com/problems/house-robber/description/)
198\. House Robber (Easy)
[Leetcode](https://leetcode.com/problems/house-robber/description/) / [力扣](https://leetcode-cn.com/problems/house-robber/description/)
题目描述抢劫一排住户但是不能抢邻近的住户求最大抢劫量
@ -103,7 +107,9 @@ public int rob(int[] nums) {
## 3. 强盗在环形街区抢劫
[213. House Robber II (Medium)](https://leetcode.com/problems/house-robber-ii/description/)
213\. House Robber II (Medium)
[Leetcode](https://leetcode.com/problems/house-robber-ii/description/) / [力扣](https://leetcode-cn.com/problems/house-robber-ii/description/)
```java
public int rob(int[] nums) {
@ -159,7 +165,9 @@ private int rob(int[] nums, int first, int last) {
## 1. 矩阵的最小路径和
[64. Minimum Path Sum (Medium)](https://leetcode.com/problems/minimum-path-sum/description/)
64\. Minimum Path Sum (Medium)
[Leetcode](https://leetcode.com/problems/minimum-path-sum/description/) / [力扣](https://leetcode-cn.com/problems/minimum-path-sum/description/)
```html
[[1,3,1],
@ -195,7 +203,9 @@ public int minPathSum(int[][] grid) {
## 2. 矩阵的总路径数
[62. Unique Paths (Medium)](https://leetcode.com/problems/unique-paths/description/)
62\. Unique Paths (Medium)
[Leetcode](https://leetcode.com/problems/unique-paths/description/) / [力扣](https://leetcode-cn.com/problems/unique-paths/description/)
题目描述统计从矩阵左上角到右下角的路径总数每次只能向右或者向下移动
@ -232,7 +242,9 @@ public int uniquePaths(int m, int n) {
## 1. 数组区间和
[303. Range Sum Query - Immutable (Easy)](https://leetcode.com/problems/range-sum-query-immutable/description/)
303\. Range Sum Query - Immutable (Easy)
[Leetcode](https://leetcode.com/problems/range-sum-query-immutable/description/) / [力扣](https://leetcode-cn.com/problems/range-sum-query-immutable/description/)
```html
Given nums = [-2, 0, 3, -5, 2, -1]
@ -264,7 +276,9 @@ class NumArray {
## 2. 数组中等差递增子区间的个数
[413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/)
413\. Arithmetic Slices (Medium)
[Leetcode](https://leetcode.com/problems/arithmetic-slices/description/) / [力扣](https://leetcode-cn.com/problems/arithmetic-slices/description/)
```html
A = [0, 1, 2, 3, 4]
@ -323,7 +337,9 @@ public int numberOfArithmeticSlices(int[] A) {
## 1. 分割整数的最大乘积
[343. Integer Break (Medim)](https://leetcode.com/problems/integer-break/description/)
343\. Integer Break (Medim)
[Leetcode](https://leetcode.com/problems/integer-break/description/) / [力扣](https://leetcode-cn.com/problems/integer-break/description/)
题目描述For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
@ -342,7 +358,9 @@ public int integerBreak(int n) {
## 2. 按平方数来分割整数
[279. Perfect Squares(Medium)](https://leetcode.com/problems/perfect-squares/description/)
279\. Perfect Squares(Medium)
[Leetcode](https://leetcode.com/problems/perfect-squares/description/) / [力扣](https://leetcode-cn.com/problems/perfect-squares/description/)
题目描述For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
@ -378,7 +396,9 @@ private List<Integer> generateSquareList(int n) {
## 3. 分割整数构成字母字符串
[91. Decode Ways (Medium)](https://leetcode.com/problems/decode-ways/description/)
91\. Decode Ways (Medium)
[Leetcode](https://leetcode.com/problems/decode-ways/description/) / [力扣](https://leetcode-cn.com/problems/decode-ways/description/)
题目描述Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
@ -426,7 +446,9 @@ public int numDecodings(String s) {
## 1. 最长递增子序列
[300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence/description/)
300\. Longest Increasing Subsequence (Medium)
[Leetcode](https://leetcode.com/problems/longest-increasing-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-increasing-subsequence/description/)
```java
public int lengthOfLIS(int[] nums) {
@ -508,7 +530,9 @@ private int binarySearch(int[] tails, int len, int key) {
## 2. 一组整数对能够构成的最长链
[646. Maximum Length of Pair Chain (Medium)](https://leetcode.com/problems/maximum-length-of-pair-chain/description/)
646\. Maximum Length of Pair Chain (Medium)
[Leetcode](https://leetcode.com/problems/maximum-length-of-pair-chain/description/) / [力扣](https://leetcode-cn.com/problems/maximum-length-of-pair-chain/description/)
```html
Input: [[1,2], [2,3], [3,4]]
@ -540,7 +564,9 @@ public int findLongestChain(int[][] pairs) {
## 3. 最长摆动子序列
[376. Wiggle Subsequence (Medium)](https://leetcode.com/problems/wiggle-subsequence/description/)
376\. Wiggle Subsequence (Medium)
[Leetcode](https://leetcode.com/problems/wiggle-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/wiggle-subsequence/description/)
```html
Input: [1,7,4,9,2,5]
@ -697,7 +723,9 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
## 1. 划分数组为和相等的两部分
[416. Partition Equal Subset Sum (Medium)](https://leetcode.com/problems/partition-equal-subset-sum/description/)
416\. Partition Equal Subset Sum (Medium)
[Leetcode](https://leetcode.com/problems/partition-equal-subset-sum/description/) / [力扣](https://leetcode-cn.com/problems/partition-equal-subset-sum/description/)
```html
Input: [1, 5, 11, 5]
@ -737,7 +765,9 @@ private int computeArraySum(int[] nums) {
## 2. 改变一组数的正负号使得它们的和为一给定数
[494. Target Sum (Medium)](https://leetcode.com/problems/target-sum/description/)
494\. Target Sum (Medium)
[Leetcode](https://leetcode.com/problems/target-sum/description/) / [力扣](https://leetcode-cn.com/problems/target-sum/description/)
```html
Input: nums is [1, 1, 1, 1, 1], S is 3.
@ -809,7 +839,9 @@ private int findTargetSumWays(int[] nums, int start, int S) {
## 3. 01 字符构成最多的字符串
[474. Ones and Zeroes (Medium)](https://leetcode.com/problems/ones-and-zeroes/description/)
474\. Ones and Zeroes (Medium)
[Leetcode](https://leetcode.com/problems/ones-and-zeroes/description/) / [力扣](https://leetcode-cn.com/problems/ones-and-zeroes/description/)
```html
Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
@ -847,7 +879,9 @@ public int findMaxForm(String[] strs, int m, int n) {
## 4. 找零钱的最少硬币数
[322. Coin Change (Medium)](https://leetcode.com/problems/coin-change/description/)
322\. Coin Change (Medium)
[Leetcode](https://leetcode.com/problems/coin-change/description/) / [力扣](https://leetcode-cn.com/problems/coin-change/description/)
```html
Example 1:
@ -888,7 +922,9 @@ public int coinChange(int[] coins, int amount) {
## 5. 找零钱的硬币数组合
[518\. Coin Change 2 (Medium)](https://leetcode.com/problems/coin-change-2/description/)
518\. Coin Change 2 (Medium)
[Leetcode](https://leetcode.com/problems/coin-change-2/description/) / [力扣](https://leetcode-cn.com/problems/coin-change-2/description/)
```text-html-basic
Input: amount = 5, coins = [1, 2, 5]
@ -920,7 +956,9 @@ public int change(int amount, int[] coins) {
## 6. 字符串按单词列表分割
[139. Word Break (Medium)](https://leetcode.com/problems/word-break/description/)
139\. Word Break (Medium)
[Leetcode](https://leetcode.com/problems/word-break/description/) / [力扣](https://leetcode-cn.com/problems/word-break/description/)
```html
s = "leetcode",
@ -957,7 +995,9 @@ public boolean wordBreak(String s, List<String> wordDict) {
## 7. 组合总和
[377. Combination Sum IV (Medium)](https://leetcode.com/problems/combination-sum-iv/description/)
377\. Combination Sum IV (Medium)
[Leetcode](https://leetcode.com/problems/combination-sum-iv/description/) / [力扣](https://leetcode-cn.com/problems/combination-sum-iv/description/)
```html
nums = [1, 2, 3]
@ -1000,7 +1040,9 @@ public int combinationSum4(int[] nums, int target) {
## 1. 需要冷却期的股票交易
[309. Best Time to Buy and Sell Stock with Cooldown(Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
309\. Best Time to Buy and Sell Stock with Cooldown(Medium)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
题目描述交易之后需要有一天的冷却时间
@ -1030,7 +1072,9 @@ public int maxProfit(int[] prices) {
## 2. 需要交易费用的股票交易
[714. Best Time to Buy and Sell Stock with Transaction Fee (Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
714\. Best Time to Buy and Sell Stock with Transaction Fee (Medium)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
```html
Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
@ -1069,7 +1113,9 @@ public int maxProfit(int[] prices, int fee) {
## 3. 只能进行两次的股票交易
[123. Best Time to Buy and Sell Stock III (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/)
123\. Best Time to Buy and Sell Stock III (Hard)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/description/)
```java
public int maxProfit(int[] prices) {
@ -1095,7 +1141,9 @@ public int maxProfit(int[] prices) {
## 4. 只能进行 k 次的股票交易
[188. Best Time to Buy and Sell Stock IV (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/)
188\. Best Time to Buy and Sell Stock IV (Hard)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/description/)
```java
public int maxProfit(int k, int[] prices) {
@ -1125,7 +1173,9 @@ public int maxProfit(int k, int[] prices) {
## 1. 删除两个字符串的字符使它们相等
[583. Delete Operation for Two Strings (Medium)](https://leetcode.com/problems/delete-operation-for-two-strings/description/)
583\. Delete Operation for Two Strings (Medium)
[Leetcode](https://leetcode.com/problems/delete-operation-for-two-strings/description/) / [力扣](https://leetcode-cn.com/problems/delete-operation-for-two-strings/description/)
```html
Input: "sea", "eat"
@ -1154,7 +1204,9 @@ public int minDistance(String word1, String word2) {
## 2. 编辑距离
[72. Edit Distance (Hard)](https://leetcode.com/problems/edit-distance/description/)
72\. Edit Distance (Hard)
[Leetcode](https://leetcode.com/problems/edit-distance/description/) / [力扣](https://leetcode-cn.com/problems/edit-distance/description/)
```html
Example 1:
@ -1207,7 +1259,9 @@ public int minDistance(String word1, String word2) {
## 3. 复制粘贴字符
[650. 2 Keys Keyboard (Medium)](https://leetcode.com/problems/2-keys-keyboard/description/)
650\. 2 Keys Keyboard (Medium)
[Leetcode](https://leetcode.com/problems/2-keys-keyboard/description/) / [力扣](https://leetcode-cn.com/problems/2-keys-keyboard/description/)
题目描述最开始只有一个字符 A问需要多少次操作能够得到 n 个字符 A每次操作可以复制当前所有的字符或者粘贴

View File

@ -10,12 +10,16 @@
- Java 中的 **HashSet** 用于存储一个集合可以查找元素是否在集合中如果元素有穷并且范围不大那么可以用一个布尔数组来存储一个元素是否存在例如对于只有小写字符的元素就可以用一个长度为 26 的布尔数组来存储一个字符集合使得空间复杂度降低为 O(1)
- Java 中的 **HashMap** 主要用于映射关系从而把两个元素联系起来HashMap 也可以用来对元素进行计数统计此时键为元素值为计数 HashSet 类似如果元素有穷并且范围不大可以用整型数组来进行统计在对一个内容进行压缩或者其它转换时利用 HashMap 可以把原始内容和转换后的内容联系起来例如在一个简化 url 的系统中 [Leetcdoe : 535. Encode and Decode TinyURL (Medium)](https://leetcode.com/problems/encode-and-decode-tinyurl/description/)利用 HashMap 就可以存储精简后的 url 到原始 url 的映射使得不仅可以显示简化的 url也可以根据简化的 url 得到原始 url 从而定位到正确的资源
Java 中的 **HashMap** 主要用于映射关系从而把两个元素联系起来HashMap 也可以用来对元素进行计数统计此时键为元素值为计数 HashSet 类似如果元素有穷并且范围不大可以用整型数组来进行统计在对一个内容进行压缩或者其它转换时利用 HashMap 可以把原始内容和转换后的内容联系起来例如在一个简化 url 的系统中 [Leetcdoe : 535. Encode and Decode TinyURL (Medium)
[Leetcode](https://leetcode.com/problems/encode-and-decode-tinyurl/description/)利用 HashMap 就可以存储精简后的 url 到原始 url 的映射使得不仅可以显示简化的 url也可以根据简化的 url 得到原始 url 从而定位到正确的资源<EFBFBD>) / [力扣](https://leetcode-cn.com/problems/encode-and-decode-tinyurl/description/)利用 HashMap 就可以存储精简后的 url 到原始 url 的映射使得不仅可以显示简化的 url也可以根据简化的 url 得到原始 url 从而定位到正确的资源<EFBFBD>)
# 1. 数组中两个数的和为给定值
[1. Two Sum (Easy)](https://leetcode.com/problems/two-sum/description/)
1\. Two Sum (Easy)
[Leetcode](https://leetcode.com/problems/two-sum/description/) / [力扣](https://leetcode-cn.com/problems/two-sum/description/)
可以先对数组进行排序然后使用双指针方法或者二分查找方法这样做的时间复杂度为 O(NlogN)空间复杂度为 O(1)
@ -37,7 +41,9 @@ public int[] twoSum(int[] nums, int target) {
# 2. 判断数组是否含有重复元素
[217. Contains Duplicate (Easy)](https://leetcode.com/problems/contains-duplicate/description/)
217\. Contains Duplicate (Easy)
[Leetcode](https://leetcode.com/problems/contains-duplicate/description/) / [力扣](https://leetcode-cn.com/problems/contains-duplicate/description/)
```java
public boolean containsDuplicate(int[] nums) {
@ -51,7 +57,9 @@ public boolean containsDuplicate(int[] nums) {
# 3. 最长和谐序列
[594. Longest Harmonious Subsequence (Easy)](https://leetcode.com/problems/longest-harmonious-subsequence/description/)
594\. Longest Harmonious Subsequence (Easy)
[Leetcode](https://leetcode.com/problems/longest-harmonious-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-harmonious-subsequence/description/)
```html
Input: [1,3,2,2,5,2,3,7]
@ -79,7 +87,9 @@ public int findLHS(int[] nums) {
# 4. 最长连续序列
[128. Longest Consecutive Sequence (Hard)](https://leetcode.com/problems/longest-consecutive-sequence/description/)
128\. Longest Consecutive Sequence (Hard)
[Leetcode](https://leetcode.com/problems/longest-consecutive-sequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-consecutive-sequence/description/)
```html
Given [100, 4, 200, 1, 3, 2],

View File

@ -15,7 +15,9 @@
## 1. 判断是否为二分图
[785. Is Graph Bipartite? (Medium)](https://leetcode.com/problems/is-graph-bipartite/description/)
785\. Is Graph Bipartite? (Medium)
[Leetcode](https://leetcode.com/problems/is-graph-bipartite/description/) / [力扣](https://leetcode-cn.com/problems/is-graph-bipartite/description/)
```html
Input: [[1,3], [0,2], [1,3], [0,2]]
@ -74,7 +76,9 @@ private boolean isBipartite(int curNode, int curColor, int[] colors, int[][] gra
## 1. 课程安排的合法性
[207. Course Schedule (Medium)](https://leetcode.com/problems/course-schedule/description/)
207\. Course Schedule (Medium)
[Leetcode](https://leetcode.com/problems/course-schedule/description/) / [力扣](https://leetcode-cn.com/problems/course-schedule/description/)
```html
2, [[1,0]]
@ -132,7 +136,9 @@ private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked,
## 2. 课程安排的顺序
[210. Course Schedule II (Medium)](https://leetcode.com/problems/course-schedule-ii/description/)
210\. Course Schedule II (Medium)
[Leetcode](https://leetcode.com/problems/course-schedule-ii/description/) / [力扣](https://leetcode-cn.com/problems/course-schedule-ii/description/)
```html
4, [[1,0],[2,0],[3,1],[3,2]]
@ -195,7 +201,9 @@ private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked, List<Int
## 1. 冗余连接
[684. Redundant Connection (Medium)](https://leetcode.com/problems/redundant-connection/description/)
684\. Redundant Connection (Medium)
[Leetcode](https://leetcode.com/problems/redundant-connection/description/) / [力扣](https://leetcode-cn.com/problems/redundant-connection/description/)
```html
Input: [[1,2], [1,3], [2,3]]

View File

@ -50,7 +50,9 @@ Return "student a am I"
# 4. 两个字符串包含的字符是否完全相同
[242. Valid Anagram (Easy)](https://leetcode.com/problems/valid-anagram/description/)
242\. Valid Anagram (Easy)
[Leetcode](https://leetcode.com/problems/valid-anagram/description/) / [力扣](https://leetcode-cn.com/problems/valid-anagram/description/)
```html
s = "anagram", t = "nagaram", return true.
@ -81,7 +83,9 @@ public boolean isAnagram(String s, String t) {
# 5. 计算一组字符集合可以组成的回文字符串的最大长度
[409. Longest Palindrome (Easy)](https://leetcode.com/problems/longest-palindrome/description/)
409\. Longest Palindrome (Easy)
[Leetcode](https://leetcode.com/problems/longest-palindrome/description/) / [力扣](https://leetcode-cn.com/problems/longest-palindrome/description/)
```html
Input : "abccccdd"
@ -112,7 +116,9 @@ public int longestPalindrome(String s) {
# 6. 字符串同构
[205. Isomorphic Strings (Easy)](https://leetcode.com/problems/isomorphic-strings/description/)
205\. Isomorphic Strings (Easy)
[Leetcode](https://leetcode.com/problems/isomorphic-strings/description/) / [力扣](https://leetcode-cn.com/problems/isomorphic-strings/description/)
```html
Given "egg", "add", return true.
@ -140,7 +146,9 @@ public boolean isIsomorphic(String s, String t) {
# 7. 回文子字符串个数
[647. Palindromic Substrings (Medium)](https://leetcode.com/problems/palindromic-substrings/description/)
647\. Palindromic Substrings (Medium)
[Leetcode](https://leetcode.com/problems/palindromic-substrings/description/) / [力扣](https://leetcode-cn.com/problems/palindromic-substrings/description/)
```html
Input: "aaa"
@ -172,7 +180,9 @@ private void extendSubstrings(String s, int start, int end) {
# 8. 判断一个整数是否是回文数
[9. Palindrome Number (Easy)](https://leetcode.com/problems/palindrome-number/description/)
9\. Palindrome Number (Easy)
[Leetcode](https://leetcode.com/problems/palindrome-number/description/) / [力扣](https://leetcode-cn.com/problems/palindrome-number/description/)
要求不能使用额外空间也就不能将整数转换为字符串进行判断
@ -197,7 +207,9 @@ public boolean isPalindrome(int x) {
# 9. 统计二进制字符串中连续 1 和连续 0 数量相同的子字符串个数
[696. Count Binary Substrings (Easy)](https://leetcode.com/problems/count-binary-substrings/description/)
696\. Count Binary Substrings (Easy)
[Leetcode](https://leetcode.com/problems/count-binary-substrings/description/) / [力扣](https://leetcode-cn.com/problems/count-binary-substrings/description/)
```html
Input: "00110011"

View File

@ -28,7 +28,9 @@
## 1. Kth Element
[215. Kth Largest Element in an Array (Medium)](https://leetcode.com/problems/kth-largest-element-in-an-array/description/)
215\. Kth Largest Element in an Array (Medium)
[Leetcode](https://leetcode.com/problems/kth-largest-element-in-an-array/description/) / [力扣](https://leetcode-cn.com/problems/kth-largest-element-in-an-array/description/)
```text
Input: [3,2,1,5,6,4] and k = 2
@ -104,7 +106,9 @@ private void swap(int[] a, int i, int j) {
## 1. 出现频率最多的 k 个元素
[347. Top K Frequent Elements (Medium)](https://leetcode.com/problems/top-k-frequent-elements/description/)
347\. Top K Frequent Elements (Medium)
[Leetcode](https://leetcode.com/problems/top-k-frequent-elements/description/) / [力扣](https://leetcode-cn.com/problems/top-k-frequent-elements/description/)
```html
Given [1,1,1,2,2,3] and k = 2, return [1,2].
@ -145,7 +149,9 @@ public List<Integer> topKFrequent(int[] nums, int k) {
## 2. 按照字符出现次数对字符串排序
[451. Sort Characters By Frequency (Medium)](https://leetcode.com/problems/sort-characters-by-frequency/description/)
451\. Sort Characters By Frequency (Medium)
[Leetcode](https://leetcode.com/problems/sort-characters-by-frequency/description/) / [力扣](https://leetcode-cn.com/problems/sort-characters-by-frequency/description/)
```html
Input:
@ -199,7 +205,9 @@ public String frequencySort(String s) {
## 1. 按颜色进行排序
[75. Sort Colors (Medium)](https://leetcode.com/problems/sort-colors/description/)
75\. Sort Colors (Medium)
[Leetcode](https://leetcode.com/problems/sort-colors/description/) / [力扣](https://leetcode-cn.com/problems/sort-colors/description/)
```html
Input: [2,0,2,1,1,0]

View File

@ -61,7 +61,9 @@
## 1. 计算在网格中从原点到特定点的最短路径长度
[1091. Shortest Path in Binary Matrix(Medium)](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
1091\. Shortest Path in Binary Matrix(Medium)
[Leetcode](https://leetcode.com/problems/shortest-path-in-binary-matrix/) / [力扣](https://leetcode-cn.com/problems/shortest-path-in-binary-matrix/)
```html
[[1,1,0,1],
@ -104,7 +106,9 @@ public int shortestPathBinaryMatrix(int[][] grids) {
## 2. 组成整数的最小平方数数量
[279. Perfect Squares (Medium)](https://leetcode.com/problems/perfect-squares/description/)
279\. Perfect Squares (Medium)
[Leetcode](https://leetcode.com/problems/perfect-squares/description/) / [力扣](https://leetcode-cn.com/problems/perfect-squares/description/)
```html
For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
@ -167,7 +171,9 @@ private List<Integer> generateSquares(int n) {
## 3. 最短单词路径
[127. Word Ladder (Medium)](https://leetcode.com/problems/word-ladder/description/)
127\. Word Ladder (Medium)
[Leetcode](https://leetcode.com/problems/word-ladder/description/) / [力扣](https://leetcode-cn.com/problems/word-ladder/description/)
```html
Input:
@ -278,7 +284,9 @@ private int getShortestPath(List<Integer>[] graphic, int start, int end) {
## 1. 查找最大的连通面积
[695. Max Area of Island (Medium)](https://leetcode.com/problems/max-area-of-island/description/)
695\. Max Area of Island (Medium)
[Leetcode](https://leetcode.com/problems/max-area-of-island/description/) / [力扣](https://leetcode-cn.com/problems/max-area-of-island/description/)
```html
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
@ -325,7 +333,9 @@ private int dfs(int[][] grid, int r, int c) {
## 2. 矩阵中的连通分量数目
[200. Number of Islands (Medium)](https://leetcode.com/problems/number-of-islands/description/)
200\. Number of Islands (Medium)
[Leetcode](https://leetcode.com/problems/number-of-islands/description/) / [力扣](https://leetcode-cn.com/problems/number-of-islands/description/)
```html
Input:
@ -374,7 +384,9 @@ private void dfs(char[][] grid, int i, int j) {
## 3. 好友关系的连通分量数目
[547. Friend Circles (Medium)](https://leetcode.com/problems/friend-circles/description/)
547\. Friend Circles (Medium)
[Leetcode](https://leetcode.com/problems/friend-circles/description/) / [力扣](https://leetcode-cn.com/problems/friend-circles/description/)
```html
Input:
@ -418,7 +430,9 @@ private void dfs(int[][] M, int i, boolean[] hasVisited) {
## 4. 填充封闭区域
[130. Surrounded Regions (Medium)](https://leetcode.com/problems/surrounded-regions/description/)
130\. Surrounded Regions (Medium)
[Leetcode](https://leetcode.com/problems/surrounded-regions/description/) / [力扣](https://leetcode-cn.com/problems/surrounded-regions/description/)
```html
For example,
@ -483,7 +497,9 @@ private void dfs(char[][] board, int r, int c) {
## 5. 能到达的太平洋和大西洋的区域
[417. Pacific Atlantic Water Flow (Medium)](https://leetcode.com/problems/pacific-atlantic-water-flow/description/)
417\. Pacific Atlantic Water Flow (Medium)
[Leetcode](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) / [力扣](https://leetcode-cn.com/problems/pacific-atlantic-water-flow/description/)
```html
Given the following 5x5 matrix:
@ -571,7 +587,9 @@ Backtracking回溯属于 DFS。
## 1. 数字键盘组合
[17. Letter Combinations of a Phone Number (Medium)](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/)
17\. Letter Combinations of a Phone Number (Medium)
[Leetcode](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) / [力扣](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/description/)
<div align="center"> <img src="pics/9823768c-212b-4b1a-b69a-b3f59e07b977.jpg"/> </div><br>
@ -609,7 +627,9 @@ private void doCombination(StringBuilder prefix, List<String> combinations, fina
## 2. IP 地址划分
[93. Restore IP Addresses(Medium)](https://leetcode.com/problems/restore-ip-addresses/description/)
93\. Restore IP Addresses(Medium)
[Leetcode](https://leetcode.com/problems/restore-ip-addresses/description/) / [力扣](https://leetcode-cn.com/problems/restore-ip-addresses/description/)
```html
Given "25525511135",
@ -650,7 +670,9 @@ private void doRestore(int k, StringBuilder tempAddress, List<String> addresses,
## 3. 在矩阵中寻找字符串
[79. Word Search (Medium)](https://leetcode.com/problems/word-search/description/)
79\. Word Search (Medium)
[Leetcode](https://leetcode.com/problems/word-search/description/) / [力扣](https://leetcode-cn.com/problems/word-search/description/)
```html
For example,
@ -719,7 +741,9 @@ private boolean backtracking(int curLen, int r, int c, boolean[][] visited, fina
## 4. 输出二叉树中所有从根到叶子的路径
[257. Binary Tree Paths (Easy)](https://leetcode.com/problems/binary-tree-paths/description/)
257\. Binary Tree Paths (Easy)
[Leetcode](https://leetcode.com/problems/binary-tree-paths/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-paths/description/)
```html
1
@ -777,7 +801,9 @@ private String buildPath(List<Integer> values) {
## 5. 排列
[46. Permutations (Medium)](https://leetcode.com/problems/permutations/description/)
46\. Permutations (Medium)
[Leetcode](https://leetcode.com/problems/permutations/description/) / [力扣](https://leetcode-cn.com/problems/permutations/description/)
```html
[1,2,3] have the following permutations:
@ -820,7 +846,9 @@ private void backtracking(List<Integer> permuteList, List<List<Integer>> permute
## 6. 含有相同元素求排列
[47. Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/)
47\. Permutations II (Medium)
[Leetcode](https://leetcode.com/problems/permutations-ii/description/) / [力扣](https://leetcode-cn.com/problems/permutations-ii/description/)
```html
[1,1,2] have the following unique permutations:
@ -865,7 +893,9 @@ private void backtracking(List<Integer> permuteList, List<List<Integer>> permute
## 7. 组合
[77. Combinations (Medium)](https://leetcode.com/problems/combinations/description/)
77\. Combinations (Medium)
[Leetcode](https://leetcode.com/problems/combinations/description/) / [力扣](https://leetcode-cn.com/problems/combinations/description/)
```html
If n = 4 and k = 2, a solution is:
@ -902,7 +932,9 @@ private void backtracking(List<Integer> combineList, List<List<Integer>> combina
## 8. 组合求和
[39. Combination Sum (Medium)](https://leetcode.com/problems/combination-sum/description/)
39\. Combination Sum (Medium)
[Leetcode](https://leetcode.com/problems/combination-sum/description/) / [力扣](https://leetcode-cn.com/problems/combination-sum/description/)
```html
given candidate set [2, 3, 6, 7] and target 7,
@ -936,7 +968,9 @@ private void backtracking(List<Integer> tempCombination, List<List<Integer>> com
## 9. 含有相同元素的组合求和
[40. Combination Sum II (Medium)](https://leetcode.com/problems/combination-sum-ii/description/)
40\. Combination Sum II (Medium)
[Leetcode](https://leetcode.com/problems/combination-sum-ii/description/) / [力扣](https://leetcode-cn.com/problems/combination-sum-ii/description/)
```html
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
@ -981,7 +1015,9 @@ private void backtracking(List<Integer> tempCombination, List<List<Integer>> com
## 10. 1-9 数字的组合求和
[216. Combination Sum III (Medium)](https://leetcode.com/problems/combination-sum-iii/description/)
216\. Combination Sum III (Medium)
[Leetcode](https://leetcode.com/problems/combination-sum-iii/description/) / [力扣](https://leetcode-cn.com/problems/combination-sum-iii/description/)
```html
Input: k = 3, n = 9
@ -1021,7 +1057,9 @@ private void backtracking(int k, int n, int start,
## 11. 子集
[78. Subsets (Medium)](https://leetcode.com/problems/subsets/description/)
78\. Subsets (Medium)
[Leetcode](https://leetcode.com/problems/subsets/description/) / [力扣](https://leetcode-cn.com/problems/subsets/description/)
找出集合的所有子集子集不能重复[1, 2] [2, 1] 这种子集算重复
@ -1052,7 +1090,9 @@ private void backtracking(int start, List<Integer> tempSubset, List<List<Integer
## 12. 含有相同元素求子集
[90. Subsets II (Medium)](https://leetcode.com/problems/subsets-ii/description/)
90\. Subsets II (Medium)
[Leetcode](https://leetcode.com/problems/subsets-ii/description/) / [力扣](https://leetcode-cn.com/problems/subsets-ii/description/)
```html
For example,
@ -1102,7 +1142,9 @@ private void backtracking(int start, List<Integer> tempSubset, List<List<Integer
## 13. 分割字符串使得每个部分都是回文数
[131. Palindrome Partitioning (Medium)](https://leetcode.com/problems/palindrome-partitioning/description/)
131\. Palindrome Partitioning (Medium)
[Leetcode](https://leetcode.com/problems/palindrome-partitioning/description/) / [力扣](https://leetcode-cn.com/problems/palindrome-partitioning/description/)
```html
For example, given s = "aab",
@ -1148,7 +1190,9 @@ private boolean isPalindrome(String s, int begin, int end) {
## 14. 数独
[37. Sudoku Solver (Hard)](https://leetcode.com/problems/sudoku-solver/description/)
37\. Sudoku Solver (Hard)
[Leetcode](https://leetcode.com/problems/sudoku-solver/description/) / [力扣](https://leetcode-cn.com/problems/sudoku-solver/description/)
<div align="center"> <img src="pics/0e8fdc96-83c1-4798-9abe-45fc91d70b9d.png"/> </div><br>
@ -1205,7 +1249,9 @@ private int cubeNum(int i, int j) {
## 15. N 皇后
[51. N-Queens (Hard)](https://leetcode.com/problems/n-queens/description/)
51\. N-Queens (Hard)
[Leetcode](https://leetcode.com/problems/n-queens/description/) / [力扣](https://leetcode-cn.com/problems/n-queens/description/)
<div align="center"> <img src="pics/067b310c-6877-40fe-9dcf-10654e737485.jpg"/> </div><br>

View File

@ -46,7 +46,9 @@ x 和 y 的最小公倍数为lcm(x,y) = 2<sup>max(m0,n0)</sup> \* 3<sup>max(
## 1. 生成素数序列
[204. Count Primes (Easy)](https://leetcode.com/problems/count-primes/description/)
204\. Count Primes (Easy)
[Leetcode](https://leetcode.com/problems/count-primes/description/) / [力扣](https://leetcode-cn.com/problems/count-primes/description/)
埃拉托斯特尼筛法在每次找到一个素数时将能被素数整除的数排除掉
@ -122,7 +124,9 @@ public int gcd(int a, int b) {
## 1. 7 进制
[504. Base 7 (Easy)](https://leetcode.com/problems/base-7/description/)
504\. Base 7 (Easy)
[Leetcode](https://leetcode.com/problems/base-7/description/) / [力扣](https://leetcode-cn.com/problems/base-7/description/)
```java
public String convertToBase7(int num) {
@ -153,7 +157,9 @@ public String convertToBase7(int num) {
## 2. 16 进制
[405. Convert a Number to Hexadecimal (Easy)](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/)
405\. Convert a Number to Hexadecimal (Easy)
[Leetcode](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/) / [力扣](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/description/)
```html
Input:
@ -186,7 +192,9 @@ public String toHex(int num) {
## 3. 26 进制
[168. Excel Sheet Column Title (Easy)](https://leetcode.com/problems/excel-sheet-column-title/description/)
168\. Excel Sheet Column Title (Easy)
[Leetcode](https://leetcode.com/problems/excel-sheet-column-title/description/) / [力扣](https://leetcode-cn.com/problems/excel-sheet-column-title/description/)
```html
1 -> A
@ -214,7 +222,9 @@ public String convertToTitle(int n) {
## 1. 统计阶乘尾部有多少个 0
[172. Factorial Trailing Zeroes (Easy)](https://leetcode.com/problems/factorial-trailing-zeroes/description/)
172\. Factorial Trailing Zeroes (Easy)
[Leetcode](https://leetcode.com/problems/factorial-trailing-zeroes/description/) / [力扣](https://leetcode-cn.com/problems/factorial-trailing-zeroes/description/)
尾部的 0 2 * 5 得来2 的数量明显多于 5 的数量因此只要统计有多少个 5 即可
@ -232,7 +242,9 @@ public int trailingZeroes(int n) {
## 1. 二进制加法
[67. Add Binary (Easy)](https://leetcode.com/problems/add-binary/description/)
67\. Add Binary (Easy)
[Leetcode](https://leetcode.com/problems/add-binary/description/) / [力扣](https://leetcode-cn.com/problems/add-binary/description/)
```html
a = "11"
@ -260,7 +272,9 @@ public String addBinary(String a, String b) {
## 2. 字符串加法
[415. Add Strings (Easy)](https://leetcode.com/problems/add-strings/description/)
415\. Add Strings (Easy)
[Leetcode](https://leetcode.com/problems/add-strings/description/) / [力扣](https://leetcode-cn.com/problems/add-strings/description/)
字符串的值为非负整数
@ -282,7 +296,9 @@ public String addStrings(String num1, String num2) {
## 1. 改变数组元素使所有的数组元素都相等
[462. Minimum Moves to Equal Array Elements II (Medium)](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/)
462\. Minimum Moves to Equal Array Elements II (Medium)
[Leetcode](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/) / [力扣](https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements-ii/description/)
```html
Input:
@ -378,7 +394,9 @@ private void swap(int[] nums, int i, int j) {
## 1. 数组中出现次数多于 n / 2 的元素
[169. Majority Element (Easy)](https://leetcode.com/problems/majority-element/description/)
169\. Majority Element (Easy)
[Leetcode](https://leetcode.com/problems/majority-element/description/) / [力扣](https://leetcode-cn.com/problems/majority-element/description/)
先对数组排序最中间那个数出现次数一定多于 n / 2
@ -406,7 +424,9 @@ public int majorityElement(int[] nums) {
## 1. 平方数
[367. Valid Perfect Square (Easy)](https://leetcode.com/problems/valid-perfect-square/description/)
367\. Valid Perfect Square (Easy)
[Leetcode](https://leetcode.com/problems/valid-perfect-square/description/) / [力扣](https://leetcode-cn.com/problems/valid-perfect-square/description/)
```html
Input: 16
@ -432,7 +452,9 @@ public boolean isPerfectSquare(int num) {
## 2. 3 n 次方
[326. Power of Three (Easy)](https://leetcode.com/problems/power-of-three/description/)
326\. Power of Three (Easy)
[Leetcode](https://leetcode.com/problems/power-of-three/description/) / [力扣](https://leetcode-cn.com/problems/power-of-three/description/)
```java
public boolean isPowerOfThree(int n) {
@ -442,7 +464,9 @@ public boolean isPowerOfThree(int n) {
## 3. 乘积数组
[238. Product of Array Except Self (Medium)](https://leetcode.com/problems/product-of-array-except-self/description/)
238\. Product of Array Except Self (Medium)
[Leetcode](https://leetcode.com/problems/product-of-array-except-self/description/) / [力扣](https://leetcode-cn.com/problems/product-of-array-except-self/description/)
```html
For example, given [1,2,3,4], return [24,12,8,6].
@ -473,7 +497,9 @@ public int[] productExceptSelf(int[] nums) {
## 4. 找出数组中的乘积最大的三个数
[628. Maximum Product of Three Numbers (Easy)](https://leetcode.com/problems/maximum-product-of-three-numbers/description/)
628\. Maximum Product of Three Numbers (Easy)
[Leetcode](https://leetcode.com/problems/maximum-product-of-three-numbers/description/) / [力扣](https://leetcode-cn.com/problems/maximum-product-of-three-numbers/description/)
```html
Input: [1,2,3,4]

View File

@ -16,7 +16,9 @@
# 1. 把数组中的 0 移到末尾
[283. Move Zeroes (Easy)](https://leetcode.com/problems/move-zeroes/description/)
283\. Move Zeroes (Easy)
[Leetcode](https://leetcode.com/problems/move-zeroes/description/) / [力扣](https://leetcode-cn.com/problems/move-zeroes/description/)
```html
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
@ -38,7 +40,9 @@ public void moveZeroes(int[] nums) {
# 2. 改变矩阵维度
[566. Reshape the Matrix (Easy)](https://leetcode.com/problems/reshape-the-matrix/description/)
566\. Reshape the Matrix (Easy)
[Leetcode](https://leetcode.com/problems/reshape-the-matrix/description/) / [力扣](https://leetcode-cn.com/problems/reshape-the-matrix/description/)
```html
Input:
@ -74,7 +78,9 @@ public int[][] matrixReshape(int[][] nums, int r, int c) {
# 3. 找出数组中最长的连续 1
[485. Max Consecutive Ones (Easy)](https://leetcode.com/problems/max-consecutive-ones/description/)
485\. Max Consecutive Ones (Easy)
[Leetcode](https://leetcode.com/problems/max-consecutive-ones/description/) / [力扣](https://leetcode-cn.com/problems/max-consecutive-ones/description/)
```java
public int findMaxConsecutiveOnes(int[] nums) {
@ -89,7 +95,9 @@ public int findMaxConsecutiveOnes(int[] nums) {
# 4. 有序矩阵查找
[240. Search a 2D Matrix II (Medium)](https://leetcode.com/problems/search-a-2d-matrix-ii/description/)
240\. Search a 2D Matrix II (Medium)
[Leetcode](https://leetcode.com/problems/search-a-2d-matrix-ii/description/) / [力扣](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/description/)
```html
[
@ -115,7 +123,9 @@ public boolean searchMatrix(int[][] matrix, int target) {
# 5. 有序矩阵的 Kth Element
[378. Kth Smallest Element in a Sorted Matrix ((Medium))](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/)
378\. Kth Smallest Element in a Sorted Matrix ((Medium))
[Leetcode](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/) / [力扣](https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/description/)
```html
matrix = [
@ -128,7 +138,9 @@ k = 8,
return 13.
```
解题参考[Share my thoughts and Clean Java Code](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173)
<EFBFBD><EFBFBD>题参考[Share my thoughts and Clean Java Code
[Leetcode](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173) / [力扣](https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173)
二分查找解法
@ -181,7 +193,9 @@ class Tuple implements Comparable<Tuple> {
# 6. 一个数组元素在 [1, n] 之间其中一个数被替换为另一个数找出重复的数和丢失的数
[645. Set Mismatch (Easy)](https://leetcode.com/problems/set-mismatch/description/)
645\. Set Mismatch (Easy)
[Leetcode](https://leetcode.com/problems/set-mismatch/description/) / [力扣](https://leetcode-cn.com/problems/set-mismatch/description/)
```html
Input: nums = [1,2,2,4]
@ -221,12 +235,18 @@ private void swap(int[] nums, int i, int j) {
类似题目
- [448. Find All Numbers Disappeared in an Array (Easy)](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/)寻找所有丢失的元素
- [442. Find All Duplicates in an Array (Medium)](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/)寻找所有重复的元素
[448\. Find All Numbers Disappeared in an Array (Easy)
[Leetcode](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/)寻找所有丢失的元<EFBFBD>) / [力扣](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/description/)寻找所有丢失的元<EFBFBD>)
[442\. Find All Duplicates in an Array (Medium)
[Leetcode](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/)寻找所有重复的元素<EFBFBD>) / [力扣](https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/description/)寻找所有重复的元素<EFBFBD>)
# 7. 找出数组中重复的数数组值在 [1, n] 之间
[287. Find the Duplicate Number (Medium)](https://leetcode.com/problems/find-the-duplicate-number/description/)
287\. Find the Duplicate Number (Medium)
[Leetcode](https://leetcode.com/problems/find-the-duplicate-number/description/) / [力扣](https://leetcode-cn.com/problems/find-the-duplicate-number/description/)
要求不能修改数组也不能使用额外的空间
@ -268,7 +288,9 @@ public int findDuplicate(int[] nums) {
# 8. 数组相邻差值的个数
[667. Beautiful Arrangement II (Medium)](https://leetcode.com/problems/beautiful-arrangement-ii/description/)
667\. Beautiful Arrangement II (Medium)
[Leetcode](https://leetcode.com/problems/beautiful-arrangement-ii/description/) / [力扣](https://leetcode-cn.com/problems/beautiful-arrangement-ii/description/)
```html
Input: n = 3, k = 2
@ -296,7 +318,9 @@ public int[] constructArray(int n, int k) {
# 9. 数组的度
[697. Degree of an Array (Easy)](https://leetcode.com/problems/degree-of-an-array/description/)
697\. Degree of an Array (Easy)
[Leetcode](https://leetcode.com/problems/degree-of-an-array/description/) / [力扣](https://leetcode-cn.com/problems/degree-of-an-array/description/)
```html
Input: [1,2,2,3,1,4,2]
@ -335,7 +359,9 @@ public int findShortestSubArray(int[] nums) {
# 10. 对角元素相等的矩阵
[766. Toeplitz Matrix (Easy)](https://leetcode.com/problems/toeplitz-matrix/description/)
766\. Toeplitz Matrix (Easy)
[Leetcode](https://leetcode.com/problems/toeplitz-matrix/description/) / [力扣](https://leetcode-cn.com/problems/toeplitz-matrix/description/)
```html
1234
@ -373,7 +399,9 @@ private boolean check(int[][] matrix, int expectValue, int row, int col) {
# 11. 嵌套数组
[565. Array Nesting (Medium)](https://leetcode.com/problems/array-nesting/description/)
565\. Array Nesting (Medium)
[Leetcode](https://leetcode.com/problems/array-nesting/description/) / [力扣](https://leetcode-cn.com/problems/array-nesting/description/)
```html
Input: A = [5,4,0,3,1,6,2]
@ -407,7 +435,9 @@ public int arrayNesting(int[] nums) {
# 12. 分隔数组
[769. Max Chunks To Make Sorted (Medium)](https://leetcode.com/problems/max-chunks-to-make-sorted/description/)
769\. Max Chunks To Make Sorted (Medium)
[Leetcode](https://leetcode.com/problems/max-chunks-to-make-sorted/description/) / [力扣](https://leetcode-cn.com/problems/max-chunks-to-make-sorted/description/)
```html
Input: arr = [1,0,2,3,4]

View File

@ -10,7 +10,9 @@
# 1. 用栈实现队列
[232. Implement Queue using Stacks (Easy)](https://leetcode.com/problems/implement-queue-using-stacks/description/)
232\. Implement Queue using Stacks (Easy)
[Leetcode](https://leetcode.com/problems/implement-queue-using-stacks/description/) / [力扣](https://leetcode-cn.com/problems/implement-queue-using-stacks/description/)
栈的顺序为后进先出而队列的顺序为先进先出使用两个栈实现队列一个元素需要经过两个栈才能出队列在经过第一个栈时元素顺序被反转经过第二个栈时再次被反转此时就是先进先出顺序
@ -50,7 +52,9 @@ class MyQueue {
# 2. 用队列实现栈
[225. Implement Stack using Queues (Easy)](https://leetcode.com/problems/implement-stack-using-queues/description/)
225\. Implement Stack using Queues (Easy)
[Leetcode](https://leetcode.com/problems/implement-stack-using-queues/description/) / [力扣](https://leetcode-cn.com/problems/implement-stack-using-queues/description/)
在将一个元素 x 插入队列时为了维护原来的后进先出顺序需要让 x 插入队列首部而队列的默认插入顺序是队列尾部因此在将 x 插入队列尾部之后需要让除了 x 之外的所有元素出队列再入队列
@ -87,7 +91,9 @@ class MyStack {
# 3. 最小值栈
[155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/)
155\. Min Stack (Easy)
[Leetcode](https://leetcode.com/problems/min-stack/description/) / [力扣](https://leetcode-cn.com/problems/min-stack/description/)
```java
class MinStack {
@ -128,7 +134,9 @@ class MinStack {
# 4. 用栈实现括号匹配
[20. Valid Parentheses (Easy)](https://leetcode.com/problems/valid-parentheses/description/)
20\. Valid Parentheses (Easy)
[Leetcode](https://leetcode.com/problems/valid-parentheses/description/) / [力扣](https://leetcode-cn.com/problems/valid-parentheses/description/)
```html
"()[]{}"
@ -161,7 +169,9 @@ public boolean isValid(String s) {
# 5. 数组中元素与下一个比它大的元素之间的距离
[739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/)
739\. Daily Temperatures (Medium)
[Leetcode](https://leetcode.com/problems/daily-temperatures/description/) / [力扣](https://leetcode-cn.com/problems/daily-temperatures/description/)
```html
Input: [73, 74, 75, 71, 69, 72, 76, 73]
@ -188,7 +198,9 @@ public int[] dailyTemperatures(int[] temperatures) {
# 6. 循环数组中比当前元素大的下一个元素
[503. Next Greater Element II (Medium)](https://leetcode.com/problems/next-greater-element-ii/description/)
503\. Next Greater Element II (Medium)
[Leetcode](https://leetcode.com/problems/next-greater-element-ii/description/) / [力扣](https://leetcode-cn.com/problems/next-greater-element-ii/description/)
```text
Input: [1,2,1]

View File

@ -44,7 +44,9 @@
## 1. 树的高度
[104. Maximum Depth of Binary Tree (Easy)](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/)
104\. Maximum Depth of Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/)
```java
public int maxDepth(TreeNode root) {
@ -55,7 +57,9 @@ public int maxDepth(TreeNode root) {
## 2. 平衡树
[110. Balanced Binary Tree (Easy)](https://leetcode.com/problems/balanced-binary-tree/description/)
110\. Balanced Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/balanced-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/balanced-binary-tree/description/)
```html
3
@ -86,7 +90,9 @@ public int maxDepth(TreeNode root) {
## 3. 两节点的最长路径
[543. Diameter of Binary Tree (Easy)](https://leetcode.com/problems/diameter-of-binary-tree/description/)
543\. Diameter of Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/diameter-of-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/diameter-of-binary-tree/description/)
```html
Input:
@ -119,7 +125,9 @@ private int depth(TreeNode root) {
## 4. 翻转树
[226. Invert Binary Tree (Easy)](https://leetcode.com/problems/invert-binary-tree/description/)
226\. Invert Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/invert-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/invert-binary-tree/description/)
```java
public TreeNode invertTree(TreeNode root) {
@ -133,7 +141,9 @@ public TreeNode invertTree(TreeNode root) {
## 5. 归并两棵树
[617. Merge Two Binary Trees (Easy)](https://leetcode.com/problems/merge-two-binary-trees/description/)
617\. Merge Two Binary Trees (Easy)
[Leetcode](https://leetcode.com/problems/merge-two-binary-trees/description/) / [力扣](https://leetcode-cn.com/problems/merge-two-binary-trees/description/)
```html
Input:
@ -166,7 +176,9 @@ public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
## 6. 判断路径和是否等于一个数
[Leetcdoe : 112. Path Sum (Easy)](https://leetcode.com/problems/path-sum/description/)
Leetcdoe : 112. Path Sum (Easy)
[Leetcode](https://leetcode.com/problems/path-sum/description/) / [力扣](https://leetcode-cn.com/problems/path-sum/description/)
```html
Given the below binary tree and sum = 22,
@ -194,7 +206,9 @@ public boolean hasPathSum(TreeNode root, int sum) {
## 7. 统计路径和等于一个数的路径数量
[437. Path Sum III (Easy)](https://leetcode.com/problems/path-sum-iii/description/)
437\. Path Sum III (Easy)
[Leetcode](https://leetcode.com/problems/path-sum-iii/description/) / [力扣](https://leetcode-cn.com/problems/path-sum-iii/description/)
```html
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
@ -234,7 +248,9 @@ private int pathSumStartWithRoot(TreeNode root, int sum) {
## 8. 子树
[572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/)
572\. Subtree of Another Tree (Easy)
[Leetcode](https://leetcode.com/problems/subtree-of-another-tree/description/) / [力扣](https://leetcode-cn.com/problems/subtree-of-another-tree/description/)
```html
Given tree s:
@ -285,7 +301,9 @@ private boolean isSubtreeWithRoot(TreeNode s, TreeNode t) {
## 9. 树的对称
[101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/)
101\. Symmetric Tree (Easy)
[Leetcode](https://leetcode.com/problems/symmetric-tree/description/) / [力扣](https://leetcode-cn.com/problems/symmetric-tree/description/)
```html
1
@ -311,7 +329,9 @@ private boolean isSymmetric(TreeNode t1, TreeNode t2) {
## 10. 最小路径
[111. Minimum Depth of Binary Tree (Easy)](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/)
111\. Minimum Depth of Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/)
树的根节点到叶子节点的最小路径长度
@ -327,7 +347,9 @@ public int minDepth(TreeNode root) {
## 11. 统计左叶子节点的和
[404. Sum of Left Leaves (Easy)](https://leetcode.com/problems/sum-of-left-leaves/description/)
404\. Sum of Left Leaves (Easy)
[Leetcode](https://leetcode.com/problems/sum-of-left-leaves/description/) / [力扣](https://leetcode-cn.com/problems/sum-of-left-leaves/description/)
```html
3
@ -354,7 +376,9 @@ private boolean isLeaf(TreeNode node){
## 12. 相同节点值的最大路径长度
[687. Longest Univalue Path (Easy)](https://leetcode.com/problems/longest-univalue-path/)
687\. Longest Univalue Path (Easy)
[Leetcode](https://leetcode.com/problems/longest-univalue-path/) / [力扣](https://leetcode-cn.com/problems/longest-univalue-path/)
```html
1
@ -387,7 +411,9 @@ private int dfs(TreeNode root){
## 13. 间隔遍历
[337. House Robber III (Medium)](https://leetcode.com/problems/house-robber-iii/description/)
337\. House Robber III (Medium)
[Leetcode](https://leetcode.com/problems/house-robber-iii/description/) / [力扣](https://leetcode-cn.com/problems/house-robber-iii/description/)
```html
3
@ -411,7 +437,9 @@ public int rob(TreeNode root) {
## 14. 找出二叉树中第二小的节点
[671. Second Minimum Node In a Binary Tree (Easy)](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/)
671\. Second Minimum Node In a Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree/description/)
```html
Input:
@ -446,7 +474,9 @@ public int findSecondMinimumValue(TreeNode root) {
## 1. 一棵树每层节点的平均数
[637. Average of Levels in Binary Tree (Easy)](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/)
637\. Average of Levels in Binary Tree (Easy)
[Leetcode](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/description/)
```java
public List<Double> averageOfLevels(TreeNode root) {
@ -471,7 +501,9 @@ public List<Double> averageOfLevels(TreeNode root) {
## 2. 得到左下角的节点
[513. Find Bottom Left Tree Value (Easy)](https://leetcode.com/problems/find-bottom-left-tree-value/description/)
513\. Find Bottom Left Tree Value (Easy)
[Leetcode](https://leetcode.com/problems/find-bottom-left-tree-value/description/) / [力扣](https://leetcode-cn.com/problems/find-bottom-left-tree-value/description/)
```html
Input:
@ -552,7 +584,9 @@ void dfs(TreeNode root) {
## 1. 非递归实现二叉树的前序遍历
[144. Binary Tree Preorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-preorder-traversal/description/)
144\. Binary Tree Preorder Traversal (Medium)
[Leetcode](https://leetcode.com/problems/binary-tree-preorder-traversal/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/)
```java
public List<Integer> preorderTraversal(TreeNode root) {
@ -572,7 +606,9 @@ public List<Integer> preorderTraversal(TreeNode root) {
## 2. 非递归实现二叉树的后序遍历
[145. Binary Tree Postorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-postorder-traversal/description/)
145\. Binary Tree Postorder Traversal (Medium)
[Leetcode](https://leetcode.com/problems/binary-tree-postorder-traversal/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/)
前序遍历为 root -> left -> right后序遍历为 left -> right -> root可以修改前序遍历成为 root -> right -> left那么这个顺序就和后序遍历正好相反
@ -595,7 +631,9 @@ public List<Integer> postorderTraversal(TreeNode root) {
## 3. 非递归实现二叉树的中序遍历
[94. Binary Tree Inorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-inorder-traversal/description/)
94\. Binary Tree Inorder Traversal (Medium)
[Leetcode](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/)
```java
public List<Integer> inorderTraversal(TreeNode root) {
@ -624,7 +662,9 @@ public List<Integer> inorderTraversal(TreeNode root) {
## 1. 修剪二叉查找树
[669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/)
669\. Trim a Binary Search Tree (Easy)
[Leetcode](https://leetcode.com/problems/trim-a-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/trim-a-binary-search-tree/description/)
```html
Input:
@ -664,7 +704,9 @@ public TreeNode trimBST(TreeNode root, int L, int R) {
## 2. 寻找二叉查找树的第 k 个元素
[230. Kth Smallest Element in a BST (Medium)](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/)
230\. Kth Smallest Element in a BST (Medium)
[Leetcode](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/) / [力扣](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/description/)
中序遍历解法
@ -708,7 +750,9 @@ private int count(TreeNode node) {
## 3. 把二叉查找树每个节点的值都加上比它大的节点的值
[Convert BST to Greater Tree (Easy)](https://leetcode.com/problems/convert-bst-to-greater-tree/description/)
Convert BST to Greater Tree (Easy)
[Leetcode](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) / [力扣](https://leetcode-cn.com/problems/convert-bst-to-greater-tree/description/)
```html
Input: The root of a Binary Search Tree like this:
@ -745,7 +789,9 @@ private void traver(TreeNode node) {
## 4. 二叉查找树的最近公共祖先
[235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
235\. Lowest Common Ancestor of a Binary Search Tree (Easy)
[Leetcode](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
```html
_______6______
@ -769,7 +815,9 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
## 5. 二叉树的最近公共祖先
[236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
236\. Lowest Common Ancestor of a Binary Tree (Medium)
[Leetcode](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
```html
_______3______
@ -794,7 +842,9 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
## 6. 从有序数组中构造二叉查找树
[108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
108\. Convert Sorted Array to Binary Search Tree (Easy)
[Leetcode](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/description/)
```java
public TreeNode sortedArrayToBST(int[] nums) {
@ -813,7 +863,9 @@ private TreeNode toBST(int[] nums, int sIdx, int eIdx){
## 7. 根据有序链表构造平衡的二叉查找树
[109. Convert Sorted List to Binary Search Tree (Medium)](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/)
109\. Convert Sorted List to Binary Search Tree (Medium)
[Leetcode](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/description/)
```html
Given the sorted linked list: [-10,-3,0,5,9],
@ -854,7 +906,9 @@ private ListNode preMid(ListNode head) {
## 8. 在二叉查找树中寻找两个节点使它们的和为一个给定值
[653. Two Sum IV - Input is a BST (Easy)](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/)
653\. Two Sum IV - Input is a BST (Easy)
[Leetcode](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/) / [力扣](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/description/)
```html
Input:
@ -898,7 +952,9 @@ private void inOrder(TreeNode root, List<Integer> nums) {
## 9. 在二叉查找树中查找两个节点之差的最小绝对值
[530. Minimum Absolute Difference in BST (Easy)](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/)
530\. Minimum Absolute Difference in BST (Easy)
[Leetcode](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) / [力扣](https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/description/)
```html
Input:
@ -936,7 +992,9 @@ private void inOrder(TreeNode node) {
## 10. 寻找二叉查找树中出现次数最多的值
[501. Find Mode in Binary Search Tree (Easy)](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/)
501\. Find Mode in Binary Search Tree (Easy)
[Leetcode](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/description/)
```html
1
@ -993,7 +1051,9 @@ Trie又称前缀树或字典树用于判断字符串是否存在或者是
## 1. 实现一个 Trie
[208. Implement Trie (Prefix Tree) (Medium)](https://leetcode.com/problems/implement-trie-prefix-tree/description/)
208\. Implement Trie (Prefix Tree) (Medium)
[Leetcode](https://leetcode.com/problems/implement-trie-prefix-tree/description/) / [力扣](https://leetcode-cn.com/problems/implement-trie-prefix-tree/description/)
```java
class Trie {
@ -1055,7 +1115,9 @@ class Trie {
## 2. 实现一个 Trie用来求前缀和
[677. Map Sum Pairs (Medium)](https://leetcode.com/problems/map-sum-pairs/description/)
677\. Map Sum Pairs (Medium)
[Leetcode](https://leetcode.com/problems/map-sum-pairs/description/) / [力扣](https://leetcode-cn.com/problems/map-sum-pairs/description/)
```html
Input: insert("apple", 3), Output: Null

View File

@ -17,7 +17,9 @@
# 1. 分配饼干
[455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/)
455\. Assign Cookies (Easy)
[Leetcode](https://leetcode.com/problems/assign-cookies/description/) / [力扣](https://leetcode-cn.com/problems/assign-cookies/description/)
```html
Input: [1,2], [1,2,3]
@ -51,7 +53,9 @@ public int findContentChildren(int[] g, int[] s) {
# 2. 不重叠的区间个数
[435. Non-overlapping Intervals (Medium)](https://leetcode.com/problems/non-overlapping-intervals/description/)
435\. Non-overlapping Intervals (Medium)
[Leetcode](https://leetcode.com/problems/non-overlapping-intervals/description/) / [力扣](https://leetcode-cn.com/problems/non-overlapping-intervals/description/)
```html
Input: [ [1,2], [1,2], [1,2] ]
@ -109,7 +113,9 @@ Arrays.sort(intervals, new Comparator<int[]>() {
# 3. 投飞镖刺破气球
[452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
452\. Minimum Number of Arrows to Burst Balloons (Medium)
[Leetcode](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/) / [力扣](https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
```
Input:
@ -143,7 +149,9 @@ public int findMinArrowShots(int[][] points) {
# 4. 根据身高和序号重组队列
[406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/)
406\. Queue Reconstruction by Height(Medium)
[Leetcode](https://leetcode.com/problems/queue-reconstruction-by-height/description/) / [力扣](https://leetcode-cn.com/problems/queue-reconstruction-by-height/description/)
```html
Input:
@ -175,7 +183,9 @@ public int[][] reconstructQueue(int[][] people) {
# 5. 买卖股票最大的收益
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
121\. Best Time to Buy and Sell Stock (Easy)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/)
题目描述一次股票交易包含买入和卖出只进行一次交易求最大收益
@ -198,7 +208,9 @@ public int maxProfit(int[] prices) {
# 6. 买卖股票的最大收益 II
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
122\. Best Time to Buy and Sell Stock II (Easy)
[Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) / [力扣](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
题目描述可以进行多次交易多次交易之间不能交叉进行可以进行多次交易
@ -219,7 +231,9 @@ public int maxProfit(int[] prices) {
# 7. 种植花朵
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
605\. Can Place Flowers (Easy)
[Leetcode](https://leetcode.com/problems/can-place-flowers/description/) / [力扣](https://leetcode-cn.com/problems/can-place-flowers/description/)
```html
Input: flowerbed = [1,0,0,0,1], n = 1
@ -249,7 +263,9 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) {
# 8. 判断是否为子序列
[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/)
392\. Is Subsequence (Medium)
[Leetcode](https://leetcode.com/problems/is-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/is-subsequence/description/)
```html
s = "abc", t = "ahbgdc"
@ -271,7 +287,9 @@ public boolean isSubsequence(String s, String t) {
# 9. 修改一个数成为非递减数组
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
665\. Non-decreasing Array (Easy)
[Leetcode](https://leetcode.com/problems/non-decreasing-array/description/) / [力扣](https://leetcode-cn.com/problems/non-decreasing-array/description/)
```html
Input: [4,2,3]
@ -305,7 +323,9 @@ public boolean checkPossibility(int[] nums) {
# 10. 子数组最大的和
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
53\. Maximum Subarray (Easy)
[Leetcode](https://leetcode.com/problems/maximum-subarray/description/) / [力扣](https://leetcode-cn.com/problems/maximum-subarray/description/)
```html
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
@ -329,7 +349,9 @@ public int maxSubArray(int[] nums) {
# 11. 分隔字符串使同种字符出现在一起
[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
763\. Partition Labels (Medium)
[Leetcode](https://leetcode.com/problems/partition-labels/description/) / [力扣](https://leetcode-cn.com/problems/partition-labels/description/)
```html
Input: S = "ababcbacadefegdehijhklij"

View File

@ -16,7 +16,9 @@
# 1. 找出两个链表的交点
[160. Intersection of Two Linked Lists (Easy)](https://leetcode.com/problems/intersection-of-two-linked-lists/description/)
160\. Intersection of Two Linked Lists (Easy)
[Leetcode](https://leetcode.com/problems/intersection-of-two-linked-lists/description/) / [力扣](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/)
例如以下示例中 A B 两个链表相交于 c1
@ -66,7 +68,9 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
# 2. 链表反转
[206. Reverse Linked List (Easy)](https://leetcode.com/problems/reverse-linked-list/description/)
206\. Reverse Linked List (Easy)
[Leetcode](https://leetcode.com/problems/reverse-linked-list/description/) / [力扣](https://leetcode-cn.com/problems/reverse-linked-list/description/)
递归
@ -100,7 +104,9 @@ public ListNode reverseList(ListNode head) {
# 3. 归并两个有序的链表
[21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists/description/)
21\. Merge Two Sorted Lists (Easy)
[Leetcode](https://leetcode.com/problems/merge-two-sorted-lists/description/) / [力扣](https://leetcode-cn.com/problems/merge-two-sorted-lists/description/)
```java
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
@ -118,7 +124,9 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
# 4. 从有序链表中删除重复节点
[83. Remove Duplicates from Sorted List (Easy)](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/)
83\. Remove Duplicates from Sorted List (Easy)
[Leetcode](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/) / [力扣](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/)
```html
Given 1->1->2, return 1->2.
@ -135,7 +143,9 @@ public ListNode deleteDuplicates(ListNode head) {
# 5. 删除链表的倒数第 n 个节点
[19. Remove Nth Node From End of List (Medium)](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)
19\. Remove Nth Node From End of List (Medium)
[Leetcode](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) / [力扣](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/)
```html
Given linked list: 1->2->3->4->5, and n = 2.
@ -161,7 +171,9 @@ public ListNode removeNthFromEnd(ListNode head, int n) {
# 6. 交换链表中的相邻结点
[24. Swap Nodes in Pairs (Medium)](https://leetcode.com/problems/swap-nodes-in-pairs/description/)
24\. Swap Nodes in Pairs (Medium)
[Leetcode](https://leetcode.com/problems/swap-nodes-in-pairs/description/) / [力扣](https://leetcode-cn.com/problems/swap-nodes-in-pairs/description/)
```html
Given 1->2->3->4, you should return the list as 2->1->4->3.
@ -189,7 +201,9 @@ public ListNode swapPairs(ListNode head) {
# 7. 链表求和
[445. Add Two Numbers II (Medium)](https://leetcode.com/problems/add-two-numbers-ii/description/)
445\. Add Two Numbers II (Medium)
[Leetcode](https://leetcode.com/problems/add-two-numbers-ii/description/) / [力扣](https://leetcode-cn.com/problems/add-two-numbers-ii/description/)
```html
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
@ -228,7 +242,9 @@ private Stack<Integer> buildStack(ListNode l) {
# 8. 回文链表
[234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/)
234\. Palindrome Linked List (Easy)
[Leetcode](https://leetcode.com/problems/palindrome-linked-list/description/) / [力扣](https://leetcode-cn.com/problems/palindrome-linked-list/description/)
题目要求 O(1) 的空间复杂度来求解
@ -277,7 +293,9 @@ private boolean isEqual(ListNode l1, ListNode l2) {
# 9. 分隔链表
[725. Split Linked List in Parts(Medium)](https://leetcode.com/problems/split-linked-list-in-parts/description/)
725\. Split Linked List in Parts(Medium)
[Leetcode](https://leetcode.com/problems/split-linked-list-in-parts/description/) / [力扣](https://leetcode-cn.com/problems/split-linked-list-in-parts/description/)
```html
Input:
@ -317,7 +335,9 @@ public ListNode[] splitListToParts(ListNode root, int k) {
# 10. 链表元素按奇偶聚集
[328. Odd Even Linked List (Medium)](https://leetcode.com/problems/odd-even-linked-list/description/)
328\. Odd Even Linked List (Medium)
[Leetcode](https://leetcode.com/problems/odd-even-linked-list/description/) / [力扣](https://leetcode-cn.com/problems/odd-even-linked-list/description/)
```html
Example: