|
| 1 | +/** |
| 2 | + * Time Complexity: O(n) - Single pass |
| 3 | + * Space Complexity: O(1) - Excluding output array |
| 4 | + */ |
| 5 | +class Solution { |
| 6 | + public int[] countBits(int n) { |
| 7 | + int[] ans = new int[n + 1]; |
| 8 | + |
| 9 | + for (int i = 1; i <= n; i++) { |
| 10 | + ans[i] = ans[i >> 1] + (i & 1); |
| 11 | + } |
| 12 | + |
| 13 | + return ans; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +// Alternative approach using bit manipulation |
| 18 | +class SolutionBitManipulation { |
| 19 | + public int[] countBits(int n) { |
| 20 | + int[] ans = new int[n + 1]; |
| 21 | + |
| 22 | + for (int i = 0; i <= n; i++) { |
| 23 | + int count = 0; |
| 24 | + int num = i; |
| 25 | + |
| 26 | + while (num != 0) { |
| 27 | + num = num & (num - 1); |
| 28 | + count++; |
| 29 | + } |
| 30 | + |
| 31 | + ans[i] = count; |
| 32 | + } |
| 33 | + |
| 34 | + return ans; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Alternative approach using built-in function |
| 39 | +class SolutionBuiltIn { |
| 40 | + public int[] countBits(int n) { |
| 41 | + int[] ans = new int[n + 1]; |
| 42 | + |
| 43 | + for (int i = 0; i <= n; i++) { |
| 44 | + ans[i] = Integer.bitCount(i); |
| 45 | + } |
| 46 | + |
| 47 | + return ans; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Alternative approach using iterative |
| 52 | +class SolutionIterative { |
| 53 | + public int[] countBits(int n) { |
| 54 | + int[] ans = new int[n + 1]; |
| 55 | + |
| 56 | + for (int i = 1; i <= n; i++) { |
| 57 | + ans[i] = ans[i >> 1] + (i & 1); |
| 58 | + } |
| 59 | + |
| 60 | + return ans; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Alternative approach using while loop |
| 65 | +class SolutionWhileLoop { |
| 66 | + public int[] countBits(int n) { |
| 67 | + int[] ans = new int[n + 1]; |
| 68 | + |
| 69 | + int i = 1; |
| 70 | + while (i <= n) { |
| 71 | + ans[i] = ans[i >> 1] + (i & 1); |
| 72 | + i++; |
| 73 | + } |
| 74 | + |
| 75 | + return ans; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Alternative approach using enhanced for loop |
| 80 | +class SolutionEnhancedForLoop { |
| 81 | + public int[] countBits(int n) { |
| 82 | + int[] ans = new int[n + 1]; |
| 83 | + |
| 84 | + for (int i = 1; i <= n; i++) { |
| 85 | + ans[i] = ans[i >> 1] + (i & 1); |
| 86 | + } |
| 87 | + |
| 88 | + return ans; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Alternative approach using recursive |
| 93 | +class SolutionRecursive { |
| 94 | + public int[] countBits(int n) { |
| 95 | + int[] ans = new int[n + 1]; |
| 96 | + |
| 97 | + countBitsHelper(n, ans); |
| 98 | + |
| 99 | + return ans; |
| 100 | + } |
| 101 | + |
| 102 | + private void countBitsHelper(int n, int[] ans) { |
| 103 | + if (n == 0) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + countBitsHelper(n - 1, ans); |
| 108 | + ans[n] = ans[n >> 1] + (n & 1); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Alternative approach using power of 2 |
| 113 | +class SolutionPowerOfTwo { |
| 114 | + public int[] countBits(int n) { |
| 115 | + int[] ans = new int[n + 1]; |
| 116 | + ans[0] = 0; |
| 117 | + |
| 118 | + for (int i = 1; i <= n; i++) { |
| 119 | + if ((i & (i - 1)) == 0) { |
| 120 | + ans[i] = 1; |
| 121 | + } else { |
| 122 | + ans[i] = ans[i - (1 << (int)(Math.log(i) / Math.log(2)))] + 1; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return ans; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// More concise version |
| 131 | +class SolutionConcise { |
| 132 | + public int[] countBits(int n) { |
| 133 | + int[] ans = new int[n + 1]; |
| 134 | + for (int i = 1; i <= n; i++) { |
| 135 | + ans[i] = ans[i >> 1] + (i & 1); |
| 136 | + } |
| 137 | + return ans; |
| 138 | + } |
| 139 | +} |
0 commit comments