Skip to content

Commit

Permalink
Merge pull request neetcode-gh#868 from pontusdacke/338-counting-bits
Browse files Browse the repository at this point in the history
Add csharp solution for 338 Counting bits
  • Loading branch information
Ahmad-A0 authored Aug 21, 2022
2 parents 6fffdba + 13d4855 commit a076cfe
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions csharp/338-Counting-Bits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Solution {
public int[] CountBits(int n) {
var hammingWeights = new int[n+1];
for (int i = 0; i <= n; i++)
{
var binary = Convert.ToString(i, 2);
var hammingWeight = 0;
for (int j = 0; j < binary.Length; j++)
{
hammingWeight += binary[j] - '0';
}
hammingWeights[i] = hammingWeight;
}
return hammingWeights;
}
}

0 comments on commit a076cfe

Please sign in to comment.