Divide-and-Conquer

tags: Algorithm WIKIEPEDIA: https://en.wikipedia.org/wiki/Divide-and-conquer%5Falgorithm The points should been noted: The middle position is not (right - left) / 2, it must be left + ((right - left) / 2).

March 30, 2022 · 1 min · Gray King

LeetCode101: 136. Single Number

tags: Bit Manipulation,Bitwise Operator: XOR According to bitwise operator XOR: x ^ x = 0 y ^ 0 = y We apply the XOR operator to all the nums, all the same numbers will apply x ^ x = 0, and then y ^ 0 = y will result the single number. class Solution { public: int singleNumber(vector<int>& nums) { int xorN = 0; for (auto iter = nums.begin(); iter != nums.end(); ++iter) { xorN ^= *iter; } return xorN; } };

March 30, 2022 · 1 min · Gray King

Bit Manipulation

tags: Algorithm

March 30, 2022 · 1 min · Gray King

Bitwise Operator: XOR

tags: Bitwise Operators 0101 (decimal 5) XOR 0011 (decimal 3) = 0110 (decimal 6) 0010 (decimal 2) XOR 1010 (decimal 10) = 1000 (decimal 8) Useful features: 1 ^ 1 = 0 2 ^ 0 = 2

March 30, 2022 · 1 min · Gray King

LeetCode101: 287. Find the Duplicate Number

tags: Cycle detection,Fast & Slow Pointers,LeetCode101 Treat as A Linked List with circle According to the length of nums is n + 1, and integer range is [1, n], so we can treat each element as a index that point to some next value. For example: [1,3,4,2,2] It can be treated as(format is element(index)): 1(0) -> 3(1) -> 2(3) -> 4(3) -> 2(4) -> 4(3) We can see there is a circle in it, so: ...

March 29, 2022 · 1 min · Gray King