diff --git a/solution/0100-0199/0191.Number of 1 Bits/README.md b/solution/0100-0199/0191.Number of 1 Bits/README.md index c03082bedcaca..b9ecf8276e64f 100644 --- a/solution/0100-0199/0191.Number of 1 Bits/README.md +++ b/solution/0100-0199/0191.Number of 1 Bits/README.md @@ -261,6 +261,18 @@ int hammingWeight(uint32_t n) { } ``` +### **Typescript** +```ts +function hammingWeight(n: number): number { + let ans: number = 0; + while (n !== 0) { + ans++; + n &= (n - 1); + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0191.Number of 1 Bits/README_EN.md b/solution/0100-0199/0191.Number of 1 Bits/README_EN.md index ba6eaa3c8bad9..19bb1e37206c0 100644 --- a/solution/0100-0199/0191.Number of 1 Bits/README_EN.md +++ b/solution/0100-0199/0191.Number of 1 Bits/README_EN.md @@ -211,6 +211,19 @@ int hammingWeight(uint32_t n) { } ``` +### **Typescript** +```ts +function hammingWeight(n: number): number { + let ans: number = 0; + while (n !== 0) { + ans++; + n &= (n - 1); + } + return ans; +} +``` + + ### **...** ``` diff --git a/solution/0100-0199/0191.Number of 1 Bits/solution.ts b/solution/0100-0199/0191.Number of 1 Bits/solution.ts new file mode 100644 index 0000000000000..c7573b60e56bd --- /dev/null +++ b/solution/0100-0199/0191.Number of 1 Bits/solution.ts @@ -0,0 +1,8 @@ +function hammingWeight(n: number): number { + let ans: number = 0; + while (n !== 0) { + ans++; + n &= (n - 1); + } + return ans; +} \ No newline at end of file