diff --git a/docs/notes/Leetcode 题解.md b/docs/notes/Leetcode 题解.md index 7ee1a2b7..23dfbefc 100644 --- a/docs/notes/Leetcode 题解.md +++ b/docs/notes/Leetcode 题解.md @@ -4243,7 +4243,7 @@ public boolean isPalindrome(ListNode head) { slow = slow.next; fast = fast.next.next; } - if (fast != null) slow = slow.next; // 链表节点个数为奇数,让 slow 指向下一个节点 + if (fast != null) slow = slow.next; // 偶数节点,让 slow 指向下一个节点 cut(head, slow); // 切成两个链表 return isEqual(head, reverse(slow)); } diff --git a/docs/notes/pics/gh_a68199af85d6_258_20_282_29.jpg b/docs/notes/pics/gh_a68199af85d6_258_20_282_29.jpg new file mode 100644 index 00000000..3c09c1c4 Binary files /dev/null and b/docs/notes/pics/gh_a68199af85d6_258_20_282_29.jpg differ diff --git a/docs/notes/剑指 offer 题解.md b/docs/notes/剑指 offer 题解.md index a0b07362..5fc40508 100644 --- a/docs/notes/剑指 offer 题解.md +++ b/docs/notes/剑指 offer 题解.md @@ -88,6 +88,10 @@ - [ProcessOn](https://www.processon.com/view/5a3e4c7be4b0909c1aa18b49) - [DrawIO](https://drive.google.com/file/d/1nSSCpPUC05MFoeFuf_aeTtkm7dG5-bJ1/view?usp=sharing) +本文内容可在微信小程序中阅读: + +

+ # 2. 实现 Singleton [单例模式](设计模式.md) @@ -1853,7 +1857,7 @@ private void backtracking(char[] chars, boolean[] hasUsed, StringBuilder s) { 多数投票问题,可以利用 Boyer-Moore Majority Vote Algorithm 来解决这个问题,使得时间复杂度为 O(N)。 -使用 cnt 来统计一个元素出现的次数,cnt 初始为 0。算法依次遍历数组中的数字,遍历到一个元素时,若 cnt 为 0,则令统计元素为遍历到的该元素,并置 cnt 为 1。若 cnt 不为 0,且当遍历到的元素和统计元素相等时,令 cnt++,否则令 cnt--。如果前面查找了 i 个元素,且 cnt == 0,说明前 i 个元素没有 majority,或者有 majority,但是出现的次数少于 i / 2 ,因为如果多于 i / 2 的话 cnt 就一定不会为 0 。此时剩下的 n - i 个元素中,majority 的数目依然多于 (n - i) / 2,因此继续查找就能找出 majority。 +使用 cnt 来统计一个元素出现的次数,当遍历到的元素和统计元素相等时,令 cnt++,否则令 cnt--。如果前面查找了 i 个元素,且 cnt == 0,说明前 i 个元素没有 majority,或者有 majority,但是出现的次数少于 i / 2 ,因为如果多于 i / 2 的话 cnt 就一定不会为 0 。此时剩下的 n - i 个元素中,majority 的数目依然多于 (n - i) / 2,因此继续查找就能找出 majority。 ```java public int MoreThanHalfNum_Solution(int[] nums) {