leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 3d31af3aefc3b3e5a8cea7482becf05cdbc6acf5
parent abef2395a9b110ee1cdd4a11b537c89f5aa7fe31
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Fri, 14 Apr 2023 14:35:14 -0500

Completed counting bits

Diffstat:
Acounting-bits/a.out | 0
Acounting-bits/counting-bits.cpp | 36++++++++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/counting-bits/a.out b/counting-bits/a.out Binary files differ. diff --git a/counting-bits/counting-bits.cpp b/counting-bits/counting-bits.cpp @@ -0,0 +1,36 @@ +#include <iostream> +#include <vector> +using namespace std; + +//Runtime: 4ms Beats: 82.49% +//Memory: 7.6MB Beats: 98.89% +int num_zeroes(int num){ + int ones = 0; + while (num > 0){ + ones += num%2; + num = num/2; + } + return ones; +} + +vector<int> countBits(int n) { + vector<int> out(n+1 , 0); + for(int i = 0 ; i <= n; ++i){ + out[i] = num_zeroes(i); + } + return out; +} + + +int main(){ + + cout << "Input number: "; + int val = 0; + cin >> val; + vector<int> out = countBits(val); + for(int i = 0; i < out.size() ; ++i){ + cout << out[i] << " "; + } + cout << endl; + return 0; +}