Nav: << previous: 190.颠倒二进制位 | next: 192.统计词频 >>
Description
tab: English
<p>Given a positive integer <code>n</code>, write a function that returns the number of <span data-keyword="set-bit">set bits</span> in its binary representation (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 11</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The input binary string <strong>1011</strong> has a total of three set bits.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 128</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The input binary string <strong>10000000</strong> has a total of one set bit.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2147483645</span></p>
<p><strong>Output:</strong> <span class="example-io">30</span></p>
<p><strong>Explanation:</strong></p>
<p>The input binary string <strong>1111111111111111111111111111101</strong> has a total of thirty set bits.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> If this function is called many times, how would you optimize it?
---
[submissions](https://leetcode.com/problems/number-of-1-bits/submissions/) | [solutions](https://leetcode.com/problems/number-of-1-bits/solutions/)
tab: 中文
<p>给定一个正整数 <code>n</code>,编写一个函数,获取一个正整数的二进制形式并返回其二进制表达式中 <span data-keyword="set-bit">设置位</span> 的个数(也被称为<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F" target="_blank">汉明重量</a>)。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>n = 11
<strong>输出:</strong>3
<strong>解释:</strong>输入的二进制串 <code><strong>1011</strong> 中,共有 3 个设置位。</code>
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>n = 128
<strong>输出:</strong>1
<strong>解释:</strong>输入的二进制串 <strong>10000000</strong> 中,共有 1 个设置位。
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>n = 2147483645
<strong>输出:</strong>30
<strong>解释:</strong>输入的二进制串 <strong>1111111111111111111111111111101</strong> 中,共有 30 个设置位。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<ul>
</ul>
<p> </p>
<p><strong>进阶</strong>:</p>
<ul>
<li>如果多次调用这个函数,你将如何优化你的算法?</li>
</ul>
---
[提交记录](https://leetcode.cn/problems/number-of-1-bits/submissions/) | [题解](https://leetcode.cn/problems/number-of-1-bits/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122