觀念
a^a = 0, a^0 = a 滿足交換性
n & (n-1)
題目
- 191 Number of 1 Bits (Easy)
- 231 Power of Two (Easy)
- 172 Factorial Trailing Zeroes (Easy)
- 793 Preimage Size of Factorial Zeroes Function (Hard)
- 204 Count Primes (Easy)
- 372 Super Pow (Medium)
- 448 Find All Numbers Disappeared in an Array (Easy)
- 654 Maximum Binary Tree (Medium)
- 382 Linked List Random Node (Medium)
- 398 Random Pick Index (Medium)
- 26 Remove Duplicates from Sorted Array (Easy)
- 83 Remove Duplicates from Sorted List (Easy)
- 27 Remove Element (Easy)
- 283 Move Zeroes (Easy)
- 292 Nim Game (Easy)
- 877 Stone Game (Medium)
- 319 Bulb Switcher (Medium)
[補充]
- 263 Ugly Number
- 264 Ugly Number II
- 1201 Ugly Number III
- 313 Super Ugly Number
- 279 Perfect Squares
Bit manipulation
191 Number of 1 Bits (Easy)
1 | int hammingWeight(uint32_t n) { |
190 Reverse Bits (Easy)
1 | uint32_t reverseBits(uint32_t n) { |
231 Power of Two (Easy)
1 | bool isPowerOfTwo(int n){ |
338 Counting Bits (Easy)
1 | class Solution { |
Duplicates and Unique
136 Single Number (Easy)
1 | int singleNumber(vector<int>& nums) { |
137 Single Number II (Medium)
1 | int singleNumber(vector<int>& nums) { |
260 Single Number III (Medium)
1 | vector<int> singleNumber(vector<int>& nums) { |
268 Missing Number (Easy)
1 | class Solution { |
287 Find the Duplicate Number (Medium) Two pointer and cycle
1 | int findDuplicate(vector<int>& nums) { |
389 Find the Difference
1 | char findTheDifference(string s, string t) { |
448 Find All Numbers Disappeared in an Array (Easy)
1 | vector<int> findDisappearedNumbers(vector<int>& nums) { |
442 Find All Duplicates in an Array (Medium)
1 | vector<int> findDuplicates(vector<int>& nums) { |
Two pointer
26 Remove Duplicates from Sorted Array (Easy)
1 | int removeDuplicates(vector<int>& nums) { |
80 Remove Duplicates from Sorted Array II (Medium)
1 | int removeDuplicates(vector<int>& nums) { |
27 Remove Element (Easy)
1 | int removeElement(vector<int>& nums, int val) { |
283 Move Zeroes (Easy)
1 | int removeElement(vector<int>& nums, int val) { |
203 Remove Linked List Elements (Easy)
1 | ListNode* removeElements(ListNode* head, int val) { |
83 Remove Duplicates from Sorted List (Easy)
1 | ListNode* deleteDuplicates(ListNode* head) { |