Skip to content

Commit

Permalink
Add csharp solution for 338 Counting bits
Browse files Browse the repository at this point in the history
  • Loading branch information
SPponmag committed Aug 20, 2022
1 parent f110883 commit 13d4855
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 13d4855

Please sign in to comment.