From 3fe6dc0c0e9e59cdbd2e1e335319ff6b9616f178 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Wed, 28 Oct 2026 22:51:10 +0800 Subject: [PATCH] =?UTF-8?q?Update=20Leetcode=20=E9=A2=98=E8=A7=A3=20-=20?= =?UTF-8?q?=E4=BD=8D=E8=BF=90=E7=AE=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 代码经力扣判题与本地对拍验证;难度标注按力扣更新,粗体小标题改为四级标题,Java 位操作的代码块标注为 java,其余示例改为 text。 --- notes/Leetcode 题解 - 位运算.md | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/notes/Leetcode 题解 - 位运算.md b/notes/Leetcode 题解 - 位运算.md index d84af383..239e4453 100644 --- a/notes/Leetcode 题解 - 位运算.md +++ b/notes/Leetcode 题解 - 位运算.md @@ -20,7 +20,7 @@ ## 0. 原理 -**基本原理** +#### 基本原理 0s 表示一串 0,1s 表示一串 1。 @@ -54,7 +54,7 @@ x ^ x = 0 x & x = x x | x = x 01111111 ``` -**位与运算技巧** +#### 位与运算技巧 n&(n-1) 去除 n 的位级表示中最低的那一位 1。例如对于二进制表示 01011011,减去 1 得到 01011010,这两个数相与得到 01011010。 @@ -76,7 +76,7 @@ n&(-n) 得到 n 的位级表示中最低的那一位 1。-n 得到 n 的反码 n-(n&(-n)) 则可以去除 n 的位级表示中最低的那一位 1,和 n&(n-1) 效果一样。 -**移位运算** +#### 移位运算 \>\> n 为算术右移,相当于除以 2n 并向下取整,例如 -7 \>\> 2 = -2。 @@ -102,7 +102,7 @@ n-(n&(-n)) 则可以去除 n 的位级表示中最低的那一位 1,和 n&(n-1 11111111111111111111111111100100 ``` -**mask 计算** +#### mask 计算 要获取 111111111,将 0 取反即可,\~0。 @@ -112,9 +112,9 @@ n-(n&(-n)) 则可以去除 n 的位级表示中最低的那一位 1,和 n&(n-1 要得到 1 到 i 位为 0 的 mask,只需将 1 到 i 位为 1 的 mask 取反,即 \~((1\<\> 1 11000000 mask |= mask >> 2 11110000 mask |= mask >> 4 11111111 @@ -432,7 +432,7 @@ public int findComplement(int num) { ## 11. 实现整数的加法 -371\. Sum of Two Integers (Easy) +371\. Sum of Two Integers (Medium) [Leetcode](https://leetcode.com/problems/sum-of-two-integers/description/) / [力扣](https://leetcode-cn.com/problems/sum-of-two-integers/description/) @@ -452,7 +452,7 @@ public int getSum(int a, int b) { [Leetcode](https://leetcode.com/problems/maximum-product-of-word-lengths/description/) / [力扣](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/description/) -```html +```text Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"] Return 16 The two words can be "abcw", "xtfn". @@ -485,7 +485,7 @@ public int maxProduct(String[] words) { ## 13. 统计从 0 \~ n 每个数的二进制表示中 1 的个数 -338\. Counting Bits (Medium) +338\. Counting Bits (Easy) [Leetcode](https://leetcode.com/problems/counting-bits/description/) / [力扣](https://leetcode-cn.com/problems/counting-bits/description/) @@ -500,4 +500,3 @@ public int[] countBits(int num) { return ret; } ``` -