GeezerVison is showing github.com — style: update format options · rabbitqyh/leetcode@24b62a9
File treeExpand file treeCollapse file tree Open diff view settings 0003.Longest Substring Without Repeating Characters 0004.Median of Two Sorted Arrays 0005.Longest Palindromic Substring
0900-0999/0983.Minimum Cost For Tickets 2300-2399/2399.Check Distances Between Same Letters
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .clang-format +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,7 +18,7 @@ AllowShortLambdasOnASingleLine: All | 18 | 18 | AllowShortIfStatementsOnASingleLine: true | 19 | 19 | AllowShortLoopsOnASingleLine: true | 20 | 20 | AlwaysBreakAfterDefinitionReturnType: None | 21 | | -AlwaysBreakAfterReturnType: TopLevelDefinitions | | 21 | +AlwaysBreakAfterReturnType: None | 22 | 22 | AlwaysBreakBeforeMultilineStrings: false | 23 | 23 | AlwaysBreakTemplateDeclarations: MultiLine | 24 | 24 | BinPackArguments: true | |
Collapse file Copy file name to clipboardExpand all lines: basic/searching/BinarySearch/README.md +2-4Lines changed: 2 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -25,12 +25,10 @@ int search(int left, int right) { | 25 | 25 | ### 模板 2 | 26 | 26 |
| 27 | 27 | ```java | 28 | | -boolean | 29 | | -check(int x) { | | 28 | +boolean check(int x) { | 30 | 29 | } | 31 | 30 |
| 32 | | -int | 33 | | -search(int left, int right) { | | 31 | +int search(int left, int right) { | 34 | 32 | while (left < right) { | 35 | 33 | int mid = (left + right + 1) >> 1; | 36 | 34 | if (check(mid)) { | |
Collapse file Copy file name to clipboardExpand all lines: basic/searching/BinarySearch/README_EN.md +2-4Lines changed: 2 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -23,12 +23,10 @@ int search(int left, int right) { | 23 | 23 | ### Template 2 | 24 | 24 |
| 25 | 25 | ```java | 26 | | -boolean | 27 | | -check(int x) { | | 26 | +boolean check(int x) { | 28 | 27 | } | 29 | 28 |
| 30 | | -int | 31 | | -search(int left, int right) { | | 29 | +int search(int left, int right) { | 32 | 30 | while (left < right) { | 33 | 31 | int mid = (left + right + 1) >> 1; | 34 | 32 | if (check(mid)) { | |
Collapse file Copy file name to clipboardExpand all lines: basic/sorting/CountingSort/CountingSort.java +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -public static void | 2 | | -sort(int[] nums, int min, int max) { | | 1 | +public static void sort(int[] nums, int min, int max) { | 3 | 2 | int n = nums.length; | 4 | 3 | int k = max - min + 1; | 5 | 4 | int[] c = new int[k]; | |
Collapse file Copy file name to clipboardExpand all lines: basic/sorting/CountingSort/README.md +1-2Lines changed: 1 addition & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 | ### **Java** | 18 | 18 |
| 19 | 19 | ```java | 20 | | -public static void | 21 | | -sort(int[] nums, int min, int max) { | | 20 | +public static void sort(int[] nums, int min, int max) { | 22 | 21 | int[] c = new int[max - min + 1]; | 23 | 22 | for (int v : nums) { | 24 | 23 | c[v - min]++; | |
Collapse file Copy file name to clipboardExpand all lines: solution/0000-0099/0001.Two Sum/README.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 59 | 59 |
| 60 | 60 | 遍历数组,当发现 `target - nums[i]` 在哈希表中,说明找到了目标值。 | 61 | 61 |
| 62 | | -时间复杂度 $O(n)$,空间复杂度 $O(n)$。 | | 62 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。 | 63 | 63 |
| 64 | 64 | <!-- tabs:start --> | 65 | 65 |
| |
Collapse file Copy file name to clipboardExpand all lines: solution/0000-0099/0002.Add Two Numbers/README.md +2-2Lines changed: 2 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 52 | 52 |
| 53 | 53 | **方法一:模拟** | 54 | 54 |
| 55 | | -同时遍历两个链表 $l1$, $l2$,对应节点值相加,进位记为 $carry$。当 $l1$, $l2$ 同时遍历结束,并且 $carry$ 为 $0$ 时,结束遍历。 | | 55 | +同时遍历两个链表 `l1`, `l2`,对应节点值相加,进位记为 `carry`。当 `l1`, `l2` 同时遍历结束,并且 `carry` 为 `0` 时,结束遍历。 | 56 | 56 |
| 57 | | -时间复杂度 $O(max(m, n))$,其中 $m$, $n$ 分别表示两个链表的长度。忽略结果链表的空间消耗,空间复杂度 $O(1)$。 | | 57 | +时间复杂度 $O(\max(m, n))$,其中 $m$, $n$ 分别表示两个链表的长度。忽略结果链表的空间消耗,空间复杂度 $O(1)$。 | 58 | 58 |
| 59 | 59 | <!-- tabs:start --> | 60 | 60 |
| |
Collapse file Copy file name to clipboardExpand all lines: solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +4-4Lines changed: 4 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 50 | 50 |
| 51 | 51 | **方法一:双指针 + 哈希表** | 52 | 52 |
| 53 | | -定义一个哈希表记录当前窗口内出现的字符,记 $i$ 和 $j$ 分别表示不重复子串的开始位置和结束位置,无重复字符子串的最大长度记为 $ans$。 | | 53 | +定义一个哈希表记录当前窗口内出现的字符,记 $i$ 和 $j$ 分别表示不重复子串的开始位置和结束位置,无重复字符子串的最大长度记为 `ans`。 | 54 | 54 |
| 55 | | -遍历字符串 $s$ 的每个字符 $s[j]$,我们记为 $c$。若 $s[i..j-1]$ 窗口内存在 $c$,则 $i$ 循环向右移动,更新哈希表,直至 $s[i..j-1]$ 窗口不存在 $c$,循环结束。将 $c$ 加入哈希表中,此时 $s[i..j]$ 窗口内不含重复元素,更新 $ans$ 的最大值 $ans = max(ans, j - i + 1)$。 | | 55 | +遍历字符串 `s` 的每个字符 $s[j]$,我们记为 $c$。若 $s[i..j-1]$ 窗口内存在 $c$,则 $i$ 循环向右移动,更新哈希表,直至 $s[i..j-1]$ 窗口不存在 `c`,循环结束。将 `c` 加入哈希表中,此时 $s[i..j]$ 窗口内不含重复元素,更新 `ans` 的最大值。 | 56 | 56 |
| 57 | | -最后返回 $ans$ 即可。 | | 57 | +最后返回 `ans` 即可。 | 58 | 58 |
| 59 | | -时间复杂度 $O(n)$,其中 $n$ 表示字符串 $s$ 的长度。 | | 59 | +时间复杂度 $O(n)$,其中 $n$ 表示字符串 `s` 的长度。 | 60 | 60 |
| 61 | 61 | 双指针算法模板: | 62 | 62 |
| |
Collapse file Copy file name to clipboardExpand all lines: solution/0000-0099/0004.Median of Two Sorted Arrays/README.md +11-6Lines changed: 11 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 47 | 47 |
| 48 | 48 | <!-- 这里可写通用的实现逻辑 --> | 49 | 49 |
| 50 | | -本题限制了时间复杂度为 `O(log (m+n))`,看到这个时间复杂度,自然而然的想到了应该使用二分查找法来求解。那么回顾一下中位数的定义,如果某个有序数组长度是奇数,那么其中位数就是最中间那个,如果是偶数,那么就是最中间两个数字的平均值。这里对于两个有序数组也是一样的,假设两个有序数组的长度分别为 m 和 n,由于两个数组长度之和 m+n 的奇偶不确定,因此需要分情况来讨论,对于奇数的情况,直接找到最中间的数即可,偶数的话需要求最中间两个数的平均值。为了简化代码,不分情况讨论,我们使用一个小 trick,我们分别找第 `(m+n+1) / 2` 个,和 `(m+n+2) / 2` 个,然后求其平均值即可,这对奇偶数均适用。假如 m+n 为奇数的话,那么其实 `(m+n+1) / 2` 和 `(m+n+2) / 2` 的值相等,相当于两个相同的数字相加再除以 2,还是其本身。 | | 50 | +**方法一:分治** | 51 | 51 |
| 52 | | -这里我们需要定义一个函数来在两个有序数组中找到第 K 个元素,下面重点来看如何实现找到第 K 个元素。 | | 52 | +本题限制了时间复杂度为 $O(\log (m+n))$,看到这个时间复杂度,自然而然的想到了应该使用二分查找法来求解。那么回顾一下中位数的定义,如果某个有序数组长度是奇数,那么其中位数就是最中间那个,如果是偶数,那么就是最中间两个数字的平均值。这里对于两个有序数组也是一样的,假设两个有序数组的长度分别为 $m$ 和 $n$,由于两个数组长度之和 $m+n$ 的奇偶不确定,因此需要分情况来讨论,对于奇数的情况,直接找到最中间的数即可,偶数的话需要求最中间两个数的平均值。为了简化代码,不分情况讨论,我们使用一个小 trick,我们分别找第 $\frac{m+n+1}{2}$ 和 $\frac{m+n+2}{2}$ 个,然后求其平均值即可,这对奇偶数均适用。假如 $m+n$ 为奇数的话,那么其实 $\frac{m+n+1}{2}$ 和 $\frac{m+n+2}{2}$ 的值相等,相当于两个相同的数字相加再除以 2,还是其本身。 | 53 | 53 |
| 54 | | -首先,为了避免产生新的数组从而增加时间复杂度,我们使用两个变量 i 和 j 分别来标记数组 nums1 和 nums2 的起始位置。然后来处理一些边界问题,比如当某一个数组的起始位置大于等于其数组长度时,说明其所有数字均已经被淘汰了,相当于一个空数组了,那么实际上就变成了在另一个数组中找数字,直接就可以找出来了。还有就是如果 K=1 的话,那么我们只要比较 nums1 和 nums2 的起始位置 i 和 j 上的数字就可以了。 | 55 | 54 |
| 56 | | -难点就在于一般的情况怎么处理?因为我们需要在两个有序数组中找到第 K 个元素,为了加快搜索的速度,我们要使用二分法,对 K 二分,意思是我们需要分别在 nums1 和 nums2 中查找第 K/2 个元素,注意这里由于两个数组的长度不定,所以有可能某个数组没有第 K/2 个数字,所以我们需要先检查一下,数组中到底存不存在第 K/2 个数字,如果存在就取出来,否则就赋值上一个整型最大值。如果某个数组没有第 K/2 个数字,那么我们就淘汰另一个数字的前 K/2 个数字即可。有没有可能两个数组都不存在第 K/2 个数字呢,这道题里是不可能的,因为我们的 K 不是任意给的,而是给的 m+n 的中间值,所以必定至少会有一个数组是存在第 K/2 个数字的。 | | 55 | +这里我们需要定义一个函数来在两个有序数组中找到第 $k$ 个元素,下面重点来看如何实现找到第 $k$ 个元素。 | 57 | 56 |
| 58 | | -最后是二分法的核心,比较这两个数组的第 K/2 小的数字 midVal1 和 midVal2 的大小,如果第一个数组的第 K/2 个数字小的话,那么说明我们要找的数字肯定不在 nums1 中的前 K/2 个数字,所以我们可以将其淘汰,将 nums1 的起始位置向后移动 K/2 个,并且此时的 K 也自减去 K/2,调用递归。反之,我们淘汰 nums2 中的前 K/2 个数字,并将 nums2 的起始位置向后移动 K/2 个,并且此时的 K 也自减去 K/2,调用递归即可。 | | 57 | +首先,为了避免产生新的数组从而增加时间复杂度,我们使用两个变量 $i$ 和 $j$ 分别来标记数组 `nums1` 和 `nums2` 的起始位置。然后来处理一些边界问题,比如当某一个数组的起始位置大于等于其数组长度时,说明其所有数字均已经被淘汰了,相当于一个空数组了,那么实际上就变成了在另一个数组中找数字,直接就可以找出来了。还有就是如果 $k=1$ 的话,那么我们只要比较 `nums1` 和 `nums2` 的起始位置 $i$ 和 $j$ 上的数字就可以了。 | 59 | 58 |
| 60 | | -> 实际是比较两个数组中的第 K/2 个数字哪一个可能到达最后合并后排序数组中的第 K 个元素的位置,其中小的那个数字注定不可能到达,所以可以直接将小的元素对应的数组的前 K/2 个数字淘汰。 | | 59 | +难点就在于一般的情况怎么处理?因为我们需要在两个有序数组中找到第 $k$ 个元素,为了加快搜索的速度,我们要使用二分法,对 $k$ 二分,意思是我们需要分别在 `nums1` 和 `nums2` 中查找第 $\left \lfloor \frac{k}{2} \right \rfloor$ 个元素,注意这里由于两个数组的长度不定,所以有可能某个数组没有第 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字,所以我们需要先检查一下,数组中到底存不存在第 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字,如果存在就取出来,否则就赋值上一个整型最大值。如果某个数组没有第 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字,那么我们就淘汰另一个数字的前 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字即可。有没有可能两个数组都不存在第 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字呢,这道题里是不可能的,因为我们的 $k$ 不是任意给的,而是给的 $m+n$ 的中间值,所以必定至少会有一个数组是存在第 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字的。 | | 60 | + | | 61 | +最后是二分法的核心,比较这两个数组的第 $\left \lfloor \frac{k}{2} \right \rfloor$ 小的数字 `midVal1` 和 `midVal2` 的大小,如果第一个数组的第 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字小的话,那么说明我们要找的数字肯定不在 `nums1` 中的前 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字,所以我们可以将其淘汰,将 `nums1` 的起始位置向后移动 $\left \lfloor \frac{k}{2} \right \rfloor$ 个,并且此时的 $k$ 也自减去 $\left \lfloor \frac{k}{2} \right \rfloor$,调用递归。反之,我们淘汰 `nums2` 中的前 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字,并将 `nums2` 的起始位置向后移动 $\left \lfloor \frac{k}{2} \right \rfloor$ 个,并且此时的 $k$ 也自减去 $\left \lfloor \frac{k}{2} \right \rfloor$,调用递归即可。 | | 62 | + | | 63 | +> 实际是比较两个数组中的第 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字哪一个可能到达最后合并后排序数组中的第 $k$ 个元素的位置,其中小的那个数字注定不可能到达,所以可以直接将小的元素对应的数组的前 $\left \lfloor \frac{k}{2} \right \rfloor$ 个数字淘汰。 | | 64 | +
| | 65 | +时间复杂度 $O(\log (m+n))$,其中 $m$ 和 $n$ 是两个数组的长度。 | 61 | 66 |
| 62 | 67 | <!-- tabs:start --> | 63 | 68 |
| |
Collapse file Copy file name to clipboardExpand all lines: solution/0000-0099/0005.Longest Palindromic Substring/README.md +6-4Lines changed: 6 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 38 | 38 |
| 39 | 39 | <!-- 这里可写通用的实现逻辑 --> | 40 | 40 |
| 41 | | -动态规划法。 | | 41 | +**方法一:动态规划** | 42 | 42 |
| 43 | | -设 `dp[i][j]` 表示字符串 `s[i..j]` 是否为回文串。 | | 43 | +设 $dp[i][j]$ 表示字符串 $s[i..j]$ 是否为回文串。 | 44 | 44 |
| 45 | | -- 当 `j - i < 2`,即字符串长度为 2 时,只要 `s[i] == s[j]`,`dp[i][j]` 就为 true。 | 46 | | -- 当 `j - i >= 2`,`dp[i][j] = dp[i + 1][j - 1] && s[i] == s[j]`。 | | 45 | +- 当 $j - i \lt 2$,即字符串长度为 `2` 时,只要 $s[i] == s[j]$,那么 $dp[i][j]$ 就为 `true`。 | | 46 | +- 当 $j - i \ge 2$,有 $dp[i][j] = dp[i + 1][j - 1] \cap s[i] == s[j]$。 | | 47 | + | | 48 | +时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。 | 47 | 49 |
| 48 | 50 | <!-- tabs:start --> | 49 | 51 |
| |
|