From 54570ecfc76d12615ed88800f324184b1aa3efd0 Mon Sep 17 00:00:00 2001 From: kerms Date: Thu, 28 May 2020 20:39:49 +0200 Subject: [PATCH] [28 May, 2020] Add cpp solution2 and solution3 --- .../28-Counting-Bits/Counting-Bits.cpp | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/May-LeetCoding-Challenge/28-Counting-Bits/Counting-Bits.cpp b/May-LeetCoding-Challenge/28-Counting-Bits/Counting-Bits.cpp index 465010e..51a0e58 100644 --- a/May-LeetCoding-Challenge/28-Counting-Bits/Counting-Bits.cpp +++ b/May-LeetCoding-Challenge/28-Counting-Bits/Counting-Bits.cpp @@ -12,4 +12,47 @@ class Solution { } return ans; } -}; \ No newline at end of file +}; + +class Solution2 { +public: + inline int numberOfSetBits(int i) + { + i = i - ((i >> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >> 2) & 0x33333333); + return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; + } + + vector countBits(int num) { + vector v; + for (int i = 0; i <= num ; ++i) + v.push_back(numberOfSetBits(i)); + return v; + } +}; + +class Solution3 { +public: + vector countBits(int num) { + vector v(num+1); + int n = 1, src = 0; + + /* init */ + v[0] = 0; + + while ((n<<1)+n <= num) { + for (;src