File tree Expand file tree Collapse file tree 1 file changed +19
-5
lines changed Expand file tree Collapse file tree 1 file changed +19
-5
lines changed Original file line number Diff line number Diff line change 11# [ Leetcode 338: Counting Bits] ( https://leetcode.com/problems/counting-bits/ )
22
33## Approaches
4- - [ Approach 1: Naive Approach] ( #approach-1-naive-approach )
5- - [ Approach 2: DP with Last Significant Bit] ( #approach-2-dp-with-last-significant-bit )
6- - [ Approach 3: DP with Highest Power of Two] ( #approach-3-dp-with-highest-power-of-two )
4+ - [ Leetcode 338: Counting Bits] ( #leetcode-338-counting-bits )
5+ - [ Approaches] ( #approaches )
6+ - [ Approach 1: Naive Approach] ( #approach-1-naive-approach )
7+ - [ Intuition] ( #intuition )
8+ - [ Code] ( #code )
9+ - [ Time Complexity] ( #time-complexity )
10+ - [ Space Complexity] ( #space-complexity )
11+ - [ Approach 2: DP with Last Significant Bit] ( #approach-2-dp-with-last-significant-bit )
12+ - [ Intuition] ( #intuition-1 )
13+ - [ Code] ( #code-1 )
14+ - [ Time Complexity] ( #time-complexity-1 )
15+ - [ Space Complexity] ( #space-complexity-1 )
16+ - [ Approach 3: DP with Highest Power of Two] ( #approach-3-dp-with-highest-power-of-two )
17+ - [ Intuition] ( #intuition-2 )
18+ - [ Code] ( #code-2 )
19+ - [ Time Complexity] ( #time-complexity-2 )
20+ - [ Space Complexity] ( #space-complexity-2 )
721
822---
923
@@ -81,9 +95,9 @@ public int[] countBits(int n) {
8195 for (int i = 1 ; i <= n; i++ ) {
8296 if (i == pow) {
8397 pow *= 2 ; // Move to the next power of two
84- x = 1 ;
98+ x = i ;
8599 }
86- res[i] = res[x ++ ] + 1 ; // Use the result from a smaller index plus one
100+ res[i] = res[i - x ] + 1 ; // Use the result from a smaller index plus one
87101 }
88102 return res;
89103}
You can’t perform that action at this time.
0 commit comments