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; } ``` -