diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5226356 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.py +*.json +*.csv \ No newline at end of file diff --git a/c++/single-number-iii.md b/c++/single-number-iii.md index d209f38..a4b3e85 100644 --- a/c++/single-number-iii.md +++ b/c++/single-number-iii.md @@ -37,16 +37,6 @@ std::vector singleNumber(std::vector& nums) { return result; } - -int main() { - std::vector nums = {1, 2, 1, 3, 2, 5}; - std::vector result = singleNumber(nums); - std::cout << "The numbers that appear once are: "; - for (int num : result) { - std::cout << num << " "; - } - return 0; -} ``` ### Time Complexity: @@ -82,7 +72,7 @@ std::vector singleNumber(std::vector& nums) { } // Find rightmost set bit different between two unique numbers - int rightmost_set_bit = xor_all & -xor_all; + unsigned int rightmost_set_bit = xor_all & -static_cast(xor_all); int num1 = 0, num2 = 0; @@ -97,21 +87,11 @@ std::vector singleNumber(std::vector& nums) { return {num1, num2}; } - -int main() { - std::vector nums = {1, 2, 1, 3, 2, 5}; - std::vector result = singleNumber(nums); - std::cout << "The numbers that appear once are: "; - for (int num : result) { - std::cout << num << " "; - } - return 0; -} ``` - ### Time Complexity: - O(n), where n is the number of elements in the vector. Each element is processed a constant number of times. ### Space Complexity: - O(1), as we are using fixed extra space regardless of input size. + diff --git a/go/intersection-of-two-linked-lists.md b/go/intersection-of-two-linked-lists.md index 7d6b4d6..15886b9 100644 --- a/go/intersection-of-two-linked-lists.md +++ b/go/intersection-of-two-linked-lists.md @@ -92,8 +92,17 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { // Both pointers move through both lists for a != b { // When reaching the end of a list, switch to the head of the other list - a = if a == nil { headB } else { a.Next } - b = if b == nil { headA } else { b.Next } + if a == nil { + a = headB + } else { + a = a.Next + } + + if b == nil { + b = headA + } else { + b = b.Next + } } return a // or b, both are the intersection node or nil if no intersection diff --git a/java/counting-bits.md b/java/counting-bits.md index e81deee..afe1842 100644 --- a/java/counting-bits.md +++ b/java/counting-bits.md @@ -1,9 +1,23 @@ # [Leetcode 338: Counting Bits](https://leetcode.com/problems/counting-bits/) ## Approaches -- [Approach 1: Naive Approach](#approach-1-naive-approach) -- [Approach 2: DP with Last Significant Bit](#approach-2-dp-with-last-significant-bit) -- [Approach 3: DP with Highest Power of Two](#approach-3-dp-with-highest-power-of-two) +- [Leetcode 338: Counting Bits](#leetcode-338-counting-bits) + - [Approaches](#approaches) + - [Approach 1: Naive Approach](#approach-1-naive-approach) + - [Intuition](#intuition) + - [Code](#code) + - [Time Complexity](#time-complexity) + - [Space Complexity](#space-complexity) + - [Approach 2: DP with Last Significant Bit](#approach-2-dp-with-last-significant-bit) + - [Intuition](#intuition-1) + - [Code](#code-1) + - [Time Complexity](#time-complexity-1) + - [Space Complexity](#space-complexity-1) + - [Approach 3: DP with Highest Power of Two](#approach-3-dp-with-highest-power-of-two) + - [Intuition](#intuition-2) + - [Code](#code-2) + - [Time Complexity](#time-complexity-2) + - [Space Complexity](#space-complexity-2) --- @@ -81,9 +95,9 @@ public int[] countBits(int n) { for (int i = 1; i <= n; i++) { if (i == pow) { pow *= 2; // Move to the next power of two - x = 1; + x = i; } - res[i] = res[x++] + 1; // Use the result from a smaller index plus one + res[i] = res[i - x] + 1; // Use the result from a smaller index plus one } return res; } diff --git a/java/house-robber.md b/java/house-robber.md index 1a0248c..416b614 100644 --- a/java/house-robber.md +++ b/java/house-robber.md @@ -52,7 +52,7 @@ To optimize the recursive approach, we can use memoization to store the results ```java class Solution { public int rob(int[] nums) { - int[] memo = new int[nums.length + 1]; + int[] memo = new int[nums.length]; Arrays.fill(memo, -1); return robFrom(0, nums, memo); } diff --git a/python/min-cost-climbing-stairs.md b/python/min-cost-climbing-stairs.md index 48563d8..40380ad 100644 --- a/python/min-cost-climbing-stairs.md +++ b/python/min-cost-climbing-stairs.md @@ -73,7 +73,7 @@ Using dynamic programming, we fill an array with the minimum costs from the base ```python def minCostClimbingStairs(cost): n = len(cost) - dp = [0] * (n + 1) + dp = [0] * (n + 2) # Start calculating the minimum cost from the top to the base. for i in range(n - 1, -1, -1):