diff --git a/OJ notes/pages/Bit-Manipulation count true trick.md b/OJ notes/pages/Bit-Manipulation count true trick.md new file mode 100644 index 0000000..5ab188a --- /dev/null +++ b/OJ notes/pages/Bit-Manipulation count true trick.md @@ -0,0 +1,34 @@ +# Bit-Manipulation count true trick + +#### 2022-07-22 15:11 + +> ##### Algorithms: +> #algorithm #bit_manipulation +> ##### Data Structure: +> #DS #bitset +> ##### Difficulty: +> #CS_analysis #difficulty-easy +> ##### Additional tags: +> + +##### Related problems: +```expander +tag:#coding_problem tag:#bit_manipulation -tag:#template_remove_me +``` + + +___ + +### What is Bit-Manipulation count true trick? + +(n & (n - 1)) + +### How does Bit-Manipulation count true trick work? + +#### validate if the number is power of 2 + +See [[Leetcode Power-of-Two#Method 1 using masking]] + +#### Counting 1 + +See [[Leetcode Number-of-1-Bits#Method 2 n n - 1 method]] \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Number-of-1-Bits.md b/OJ notes/pages/Leetcode Number-of-1-Bits.md new file mode 100644 index 0000000..b6d347c --- /dev/null +++ b/OJ notes/pages/Leetcode Number-of-1-Bits.md @@ -0,0 +1,105 @@ +# Leetcode Number-of-1-Bits + +#### 2022-07-22 14:45 + +> ##### Algorithms: +> #algorithm #bit_manipulation +> ##### Data structures: +> #DS #bitset +> ##### Difficulty: +> #coding_problem #difficulty-easy +> ##### Additional tags: +> #leetcode +> ##### Revisions: +> N/A + +##### Related topics: +```expander +tag:#bit_manipulation +``` + + +##### Links: +- [Link to problem](https://leetcode.com/problems/number-of-1-bits/) +___ +### Problem + +Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)). + +#### Examples + +**Example 1:** + +**Input:** n = 00000000000000000000000000001011 +**Output:** 3 +**Explanation:** The input binary string **00000000000000000000000000001011** has a total of three '1' bits. + +**Example 2:** + +**Input:** n = 00000000000000000000000010000000 +**Output:** 1 +**Explanation:** The input binary string **00000000000000000000000010000000** has a total of one '1' bit. + +**Example 3:** + +**Input:** n = 11111111111111111111111111111101 +**Output:** 31 +**Explanation:** The input binary string **11111111111111111111111111111101** has a total of thirty one '1' bits. + +#### Constraints + +- The input must be a **binary string** of length `32`. + +### Thoughts + +> [!summary] +> This is a #bit_manipulation problem. + +Two methods for this problem. + +#### Method 1: cpp's STL implementation + +simply use [bitset::count](https://en.cppreference.com/w/cpp/utility/bitset/count) + +#### Method 2: (n & (n - 1)) method + +By using n = (n & (n - 1)), we can remove the last `true` in the original bitset: + +``` +5 : 101 +4 : 100 +5 & 4 : 100 + +10 : 1010 +9 : 1001 +10 & 9 : 1000 +``` + +n - 1 changes the trailing `false`s to `true`, and change the last `true` to false, and by AND operation, we can remove the last `true` bit. + +### Solution + +#### CPP STL: + +```cpp +class Solution { +public: + int hammingWeight(uint32_t n) { return bitset<32>(n).count(); } +}; +```` + +#### Method 2: + +```cpp +class Solution { +public: + int hammingWeight(uint32_t n) { + int count = 0; + while (n != 0) { + n = (n & (n - 1)); + count++; + } + return count; + } +}; +``` \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Power-of-Two.md b/OJ notes/pages/Leetcode Power-of-Two.md new file mode 100644 index 0000000..aaa259d --- /dev/null +++ b/OJ notes/pages/Leetcode Power-of-Two.md @@ -0,0 +1,131 @@ +# Leetcode Power-of-Two + +#### 2022-07-22 14:30 + +> ##### Algorithms: +> #algorithm #bit_manipulation +> ##### Data structures: +> #DS #bitset +> ##### Difficulty: +> #coding_problem #difficulty-medium +> ##### Additional tags: +> #leetcode +> ##### Revisions: +> N/A + +##### Related topics: +```expander +tag:#bitset OR tag:#bit_manipulation +``` + + + +##### Links: +- [Link to problem](https://leetcode.com/problems/power-of-two/) +___ +### Problem + +Given an integer `n`, return _`true` if it is a power of two. Otherwise, return `false`_. + +An integer `n` is a power of two, if there exists an integer `x` such that `n == 2x`. + +#### Examples + +**Example 1:** + +**Input:** n = 1 +**Output:** true +**Explanation:** 20 = 1 + +**Example 2:** + +**Input:** n = 16 +**Output:** true +**Explanation:** 24 = 16 + +**Example 3:** + +**Input:** n = 3 +**Output:** false + +#### Constraints + +- `-231 <= n <= 231 - 1` + +### Thoughts + +> [!summary] +> This is a #bit_manipulation problem. +> The solution can come from investigating multiple +> examples and find the common rules + +Simple solution using bit masking: + +#### Method 1, using masking + +- if n <= 0, return false, because it's impossible to be power of 2. +- else, return !(n & (n - 1)) + +power of 2 and --1 looks like this + +``` +2 : 10 +1 : 01 + +4 : 100 +3 : 011 + +8 : 1000 +7 : 0111 +``` + +so, if it is power of 2, `!(n & (n - 1))` will produce true. + +otherwise, `(n & (n - 1))` will produce something other than 0. + +#### Method 2, count bits + +power of 2 must have one and only one `true` in the bitset: + +``` +2 : 10 +4 : 100 +8 : 1000 +... +``` + +So, by counting if the bitset has only one `true`. + +### Solution + +#### Method 1: + +```cpp +class Solution { +public: + bool isPowerOfTwo(int n) { + // n == 0: return 0 + // n != 0: return !(n & (n - 1)) + if (n <= 0) { + return false; + } else { + return !(n & (n - 1)); + } + } +}; +``` + +#### Method 2: + +```cpp +class Solution { +public: + bool isPowerOfTwo(int n) { + if (n <= 0) { + return false; + } else { + return (bitset<32>(n).count() == 1); + } + } +}; +``` \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Reverse-Bits.md b/OJ notes/pages/Leetcode Reverse-Bits.md new file mode 100644 index 0000000..28a3b9e --- /dev/null +++ b/OJ notes/pages/Leetcode Reverse-Bits.md @@ -0,0 +1,98 @@ +# Leetcode Reverse-Bits + +#### 2022-07-22 15:15 + +> ##### Algorithms: +> #algorithm #bit_manipulation +> ##### Data structures: +> #DS #bitset +> ##### Difficulty: +> #coding_problem #difficulty-easy +> ##### Additional tags: +> #leetcode +> ##### Revisions: +> N/A + +##### Related topics: +```expander +tag:#bitset +``` + + + +##### Links: +- [Link to problem](https://leetcode.com/problems/reverse-bits/) +- [Solution and detailed explanation](https://leetcode.com/problems/reverse-bits/discuss/1232842/JAVA-C%2B%2B-%3A-0ms-or-O(1)-Time-Complexity-or-in-place-or-Detailed-Explanation) +- [Shifting solution an explanation](https://leetcode.com/problems/reverse-bits/discuss/54741/O(1)-bit-operation-C++-solution-(8ms)/301342) +___ +### Problem + +Reverse bits of a given 32 bits unsigned integer. + +#### Examples + +**Example 1:** + +**Input:** n = 00000010100101000001111010011100 +**Output:** 964176192 (00111001011110000010100101000000) +**Explanation:** The input binary string **00000010100101000001111010011100** represents the unsigned integer 43261596, so return 964176192 which its binary representation is **00111001011110000010100101000000**. + +**Example 2:** + +**Input:** n = 11111111111111111111111111111101 +**Output:** 3221225471 (10111111111111111111111111111111) +**Explanation:** The input binary string **11111111111111111111111111111101** represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is **10111111111111111111111111111111**. + +#### Constraints + +- The input must be a **binary string** of length `32` + +### Thoughts + +> [!summary] +> This is a #bit_manipulation problem. + +There are two methods: +- swapping bits inside the bitset +- shifting + +#### Method 1: Swapping + +This is the most simple and intuitive one, all you have to do is swap the first and last numbers, and continue like that. + +#### Method 2: Shifting + +The bitset has operations like this `<<` `>>`, which can be used to shift, and reverse the array. + +The operation is already documented in the link above: +[[Leetcode Reverse-Bits#Links]] + +### Solution + +#### Method 1: + +```cpp +class Solution { +public: + uint32_t reverseBits(uint32_t n) { + // swap bits inside the function + bitset<32> nb(n); + bool tmp; + int l = 0, r = 31; + + while (l < r) { + tmp = nb[l]; + nb[l] = nb[r]; + nb[r] = tmp; + l++; + r--; + } + + return nb.to_ulong(); + } +}; +``` + +#### Shifting: + +==#TODO solve it using shifting== \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Single-Number.md b/OJ notes/pages/Leetcode Single-Number.md new file mode 100644 index 0000000..e61b1ba --- /dev/null +++ b/OJ notes/pages/Leetcode Single-Number.md @@ -0,0 +1,41 @@ +# Leetcode Single-Number + +#### 2022-07-22 15:31 + +> ##### Algorithms: +> #algorithm #bit_manipulation +> ##### Data structures: +> #DS #bitset +> ##### Difficulty: +> #coding_problem #difficulty-easy +> ##### Additional tags: +> #leetcode +> ##### Revisions: +> N/A + +##### Related topics: +```expander +tag:#bit_manipulation +``` + + + +##### Links: +- [Link to problem](https://leetcode.com/problems/single-number/) +___ +### Problem + +Given a **non-empty** array of integers `nums`, every element appears _twice_ except for one. Find that single one. + +You must implement a solution with a linear runtime complexity and use only constant extra space. + +#### Examples + +#### Constraints + +### Thoughts + +> [!summary] +> This is a #template_remove_me + +### Solution diff --git a/OJ notes/pages/Leetcode Triangle.md b/OJ notes/pages/Leetcode Triangle.md index ef91671..47dbfb2 100644 --- a/OJ notes/pages/Leetcode Triangle.md +++ b/OJ notes/pages/Leetcode Triangle.md @@ -67,3 +67,41 @@ Same as in [[Leetcode House-Robber]], there are four stages to optimization: #### Stage 2: recursion with cachinqg ### Solution + +#### Stage 2: + +```cpp +class Solution { + vector> cache; + int minimum(vector> &triangle, int level, int l, int r) { + if (level == 0) { + return triangle[0][0]; + } else { + int minLen = INT_MAX; + for (int i = l; i <= r; i++) { + if (i < 0 || i > level) { + continue; + } + if (cache[level][i] != -1) { + minLen = min(cache[level][i], minLen); + // cout<<"Using cache: "<> &triangle) { + // Stage one: recursive + cache = + vector>(triangle.size(), vector(triangle.size(), -1)); + return minimum(triangle, triangle.size() - 1, 0, triangle.size() - 1); + } +}; +``` \ No newline at end of file