Back to Leetcode Go

2.15 ✅ Bit Manipulation

website/content/ChapterTwo/Bit_Manipulation.md

1.7.19.3 KB
Original Source

Bit Manipulation

  • 异或的特性。第 136 题,第 268 题,第 389 题,第 421 题,
go
x ^ 0 = x
x ^ 11111……1111 = ~x
x ^ (~x) = 11111……1111
x ^ x = 0
a ^ b = c  => a ^ c = b  => b ^ c = a (交换律)
a ^ b ^ c = a ^ (b ^ c) = (a ^ b)^ c (结合律)
  • 构造特殊 Mask,将特殊位置放 0 或 1。
go
将 x 最右边的 n 位清零, x & ( ~0 << n )
获取 x 的第 n 位值(0 或者 1),(x >> n) & 1
获取 x 的第 n 位的幂值,x & (1 << (n - 1))
仅将第 n 位置为 1,x | (1 << n)
仅将第 n 位置为 0,x & (~(1 << n))
将 x 最高位至第 n 位(含)清零,x & ((1 << n) - 1)
将第 n 位至第 0 位(含)清零,x & (~((1 << (n + 1)) - 1))
  • 有特殊意义的 & 位操作运算。第 260 题,第 201 题,第 318 题,第 371 题,第 397 题,第 461 题,第 693 题,
go
X & 1 == 1 判断是否是奇数(偶数)
X & = (X - 1) 将最低位(LSB)的 1 清零
X & -X 得到最低位(LSB)的 1
X & ~X = 0
No.TitleSolutionDifficultyTimeComplexitySpaceComplexityFavoriteAcceptance
0029Divide Two Integers[Go]({{< relref "/ChapterFour/0001~0099/0029.Divide-Two-Integers.md" >}})Medium17.2%
0067Add Binary[Go]({{< relref "/ChapterFour/0001~0099/0067.Add-Binary.md" >}})Easy52.4%
0078Subsets[Go]({{< relref "/ChapterFour/0001~0099/0078.Subsets.md" >}})MediumO(n^2)O(n)❤️74.9%
0089Gray Code[Go]({{< relref "/ChapterFour/0001~0099/0089.Gray-Code.md" >}})Medium57.2%
0090Subsets II[Go]({{< relref "/ChapterFour/0001~0099/0090.Subsets-II.md" >}})Medium55.9%
0136Single Number[Go]({{< relref "/ChapterFour/0100~0199/0136.Single-Number.md" >}})EasyO(n)O(1)70.7%
0137Single Number II[Go]({{< relref "/ChapterFour/0100~0199/0137.Single-Number-II.md" >}})MediumO(n)O(1)❤️58.5%
0187Repeated DNA Sequences[Go]({{< relref "/ChapterFour/0100~0199/0187.Repeated-DNA-Sequences.md" >}})MediumO(n)O(1)47.0%
0190Reverse Bits[Go]({{< relref "/ChapterFour/0100~0199/0190.Reverse-Bits.md" >}})EasyO(n)O(1)❤️54.0%
0191Number of 1 Bits[Go]({{< relref "/ChapterFour/0100~0199/0191.Number-of-1-Bits.md" >}})EasyO(n)O(1)66.6%
0201Bitwise AND of Numbers Range[Go]({{< relref "/ChapterFour/0200~0299/0201.Bitwise-AND-of-Numbers-Range.md" >}})MediumO(n)O(1)❤️42.5%
0231Power of Two[Go]({{< relref "/ChapterFour/0200~0299/0231.Power-of-Two.md" >}})EasyO(1)O(1)46.0%
0260Single Number III[Go]({{< relref "/ChapterFour/0200~0299/0260.Single-Number-III.md" >}})MediumO(n)O(1)❤️67.7%
0268Missing Number[Go]({{< relref "/ChapterFour/0200~0299/0268.Missing-Number.md" >}})EasyO(n)O(1)62.6%
0287Find the Duplicate Number[Go]({{< relref "/ChapterFour/0200~0299/0287.Find-the-Duplicate-Number.md" >}})Medium59.1%
0318Maximum Product of Word Lengths[Go]({{< relref "/ChapterFour/0300~0399/0318.Maximum-Product-of-Word-Lengths.md" >}})MediumO(n)O(1)59.9%
0338Counting Bits[Go]({{< relref "/ChapterFour/0300~0399/0338.Counting-Bits.md" >}})EasyO(n)O(n)75.8%
0342Power of Four[Go]({{< relref "/ChapterFour/0300~0399/0342.Power-of-Four.md" >}})EasyO(n)O(1)46.2%
0371Sum of Two Integers[Go]({{< relref "/ChapterFour/0300~0399/0371.Sum-of-Two-Integers.md" >}})MediumO(n)O(1)50.7%
0389Find the Difference[Go]({{< relref "/ChapterFour/0300~0399/0389.Find-the-Difference.md" >}})EasyO(n)O(1)59.9%
0393UTF-8 Validation[Go]({{< relref "/ChapterFour/0300~0399/0393.UTF-8-Validation.md" >}})MediumO(n)O(1)45.1%
0397Integer Replacement[Go]({{< relref "/ChapterFour/0300~0399/0397.Integer-Replacement.md" >}})MediumO(n)O(1)35.2%
0401Binary Watch[Go]({{< relref "/ChapterFour/0400~0499/0401.Binary-Watch.md" >}})EasyO(1)O(1)52.3%
0405Convert a Number to Hexadecimal[Go]({{< relref "/ChapterFour/0400~0499/0405.Convert-a-Number-to-Hexadecimal.md" >}})EasyO(n)O(1)46.8%
0421Maximum XOR of Two Numbers in an Array[Go]({{< relref "/ChapterFour/0400~0499/0421.Maximum-XOR-of-Two-Numbers-in-an-Array.md" >}})MediumO(n)O(1)❤️54.0%
0461Hamming Distance[Go]({{< relref "/ChapterFour/0400~0499/0461.Hamming-Distance.md" >}})EasyO(n)O(1)75.0%
0473Matchsticks to Square[Go]({{< relref "/ChapterFour/0400~0499/0473.Matchsticks-to-Square.md" >}})Medium40.2%
0476Number Complement[Go]({{< relref "/ChapterFour/0400~0499/0476.Number-Complement.md" >}})EasyO(n)O(1)67.4%
0477Total Hamming Distance[Go]({{< relref "/ChapterFour/0400~0499/0477.Total-Hamming-Distance.md" >}})MediumO(n)O(1)52.2%
0491Non-decreasing Subsequences[Go]({{< relref "/ChapterFour/0400~0499/0491.Non-decreasing-Subsequences.md" >}})Medium60.2%
0526Beautiful Arrangement[Go]({{< relref "/ChapterFour/0500~0599/0526.Beautiful-Arrangement.md" >}})Medium64.4%
0638Shopping Offers[Go]({{< relref "/ChapterFour/0600~0699/0638.Shopping-Offers.md" >}})Medium53.3%
0645Set Mismatch[Go]({{< relref "/ChapterFour/0600~0699/0645.Set-Mismatch.md" >}})Easy42.7%
0693Binary Number with Alternating Bits[Go]({{< relref "/ChapterFour/0600~0699/0693.Binary-Number-with-Alternating-Bits.md" >}})EasyO(n)O(1)❤️61.6%
0756Pyramid Transition Matrix[Go]({{< relref "/ChapterFour/0700~0799/0756.Pyramid-Transition-Matrix.md" >}})MediumO(n log n)O(n)52.7%
0762Prime Number of Set Bits in Binary Representation[Go]({{< relref "/ChapterFour/0700~0799/0762.Prime-Number-of-Set-Bits-in-Binary-Representation.md" >}})EasyO(n)O(1)68.0%
0784Letter Case Permutation[Go]({{< relref "/ChapterFour/0700~0799/0784.Letter-Case-Permutation.md" >}})MediumO(n)O(1)73.8%
0810Chalkboard XOR Game[Go]({{< relref "/ChapterFour/0800~0899/0810.Chalkboard-XOR-Game.md" >}})Hard55.8%
0864Shortest Path to Get All Keys[Go]({{< relref "/ChapterFour/0800~0899/0864.Shortest-Path-to-Get-All-Keys.md" >}})Hard45.6%
0898Bitwise ORs of Subarrays[Go]({{< relref "/ChapterFour/0800~0899/0898.Bitwise-ORs-of-Subarrays.md" >}})MediumO(n)O(1)37.2%
0980Unique Paths III[Go]({{< relref "/ChapterFour/0900~0999/0980.Unique-Paths-III.md" >}})Hard81.7%
0995Minimum Number of K Consecutive Bit Flips[Go]({{< relref "/ChapterFour/0900~0999/0995.Minimum-Number-of-K-Consecutive-Bit-Flips.md" >}})Hard51.2%
0996Number of Squareful Arrays[Go]({{< relref "/ChapterFour/0900~0999/0996.Number-of-Squareful-Arrays.md" >}})Hard49.2%
1009Complement of Base 10 Integer[Go]({{< relref "/ChapterFour/1000~1099/1009.Complement-of-Base-10-Integer.md" >}})Easy61.5%
1178Number of Valid Words for Each Puzzle[Go]({{< relref "/ChapterFour/1100~1199/1178.Number-of-Valid-Words-for-Each-Puzzle.md" >}})Hard46.3%
1239Maximum Length of a Concatenated String with Unique Characters[Go]({{< relref "/ChapterFour/1200~1299/1239.Maximum-Length-of-a-Concatenated-String-with-Unique-Characters.md" >}})Medium52.2%
1310XOR Queries of a Subarray[Go]({{< relref "/ChapterFour/1300~1399/1310.XOR-Queries-of-a-Subarray.md" >}})Medium72.3%
1442Count Triplets That Can Form Two Arrays of Equal XOR[Go]({{< relref "/ChapterFour/1400~1499/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR.md" >}})Medium76.1%
1461Check If a String Contains All Binary Codes of Size K[Go]({{< relref "/ChapterFour/1400~1499/1461.Check-If-a-String-Contains-All-Binary-Codes-of-Size-K.md" >}})Medium56.6%
1486XOR Operation in an Array[Go]({{< relref "/ChapterFour/1400~1499/1486.XOR-Operation-in-an-Array.md" >}})Easy84.6%
1655Distribute Repeating Integers[Go]({{< relref "/ChapterFour/1600~1699/1655.Distribute-Repeating-Integers.md" >}})Hard39.3%
1659Maximize Grid Happiness[Go]({{< relref "/ChapterFour/1600~1699/1659.Maximize-Grid-Happiness.md" >}})Hard38.8%
1680Concatenation of Consecutive Binary Numbers[Go]({{< relref "/ChapterFour/1600~1699/1680.Concatenation-of-Consecutive-Binary-Numbers.md" >}})Medium57.0%
1681Minimum Incompatibility[Go]({{< relref "/ChapterFour/1600~1699/1681.Minimum-Incompatibility.md" >}})Hard37.8%
1684Count the Number of Consistent Strings[Go]({{< relref "/ChapterFour/1600~1699/1684.Count-the-Number-of-Consistent-Strings.md" >}})Easy82.3%
1720Decode XORed Array[Go]({{< relref "/ChapterFour/1700~1799/1720.Decode-XORed-Array.md" >}})Easy85.8%
1734Decode XORed Permutation[Go]({{< relref "/ChapterFour/1700~1799/1734.Decode-XORed-Permutation.md" >}})Medium63.0%
1738Find Kth Largest XOR Coordinate Value[Go]({{< relref "/ChapterFour/1700~1799/1738.Find-Kth-Largest-XOR-Coordinate-Value.md" >}})Medium61.0%
1763Longest Nice Substring[Go]({{< relref "/ChapterFour/1700~1799/1763.Longest-Nice-Substring.md" >}})Easy61.5%
------------------------------------------------------------------------------------------------------------------------------------------------

<div style="display: flex;justify-content: space-between;align-items: center;"> <p><a href="https://books.halfrost.com/leetcode/ChapterTwo/Sorting/">⬅️上一页</a></p> <p><a href="https://books.halfrost.com/leetcode/ChapterTwo/Union_Find/">下一页➡️</a></p> </div>