Skip to content

Commit 03ba6b3

Browse files
committed
chore: update 3370. Smallest Number With All Set Bits
1 parent ec2c790 commit 03ba6b3

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: 3370. Smallest Number With All Set Bits
3+
description: You are given a positive number n. Return the smallest number x greater than or equal to n, such that the binary representation of x contains only set bits
4+
keywords:
5+
[
6+
LeetCode,
7+
3370. Smallest Number With All Set Bits,
8+
Easy,
9+
Math,
10+
Bit Manipulation,
11+
]
12+
tags: [LeetCode, Easy, Math, Bit Manipulation]
13+
---
14+
15+
# [{frontMatter.title}](https://leetcode.com/problems/smallest-number-with-all-set-bits/)
16+
17+
import Tabs from "@theme/Tabs";
18+
import TabItem from "@theme/TabItem";
19+
import CodeBlock from "@theme/CodeBlock";
20+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
21+
import Solution from "!!raw-loader!./solution.js";
22+
import Description from "./_Description.md";
23+
import Examples from "./_Examples.md";
24+
25+
<DifficultyBadge difficulty="Easy" />
26+
27+
<Tabs>
28+
<TabItem value="description" label="題目描述" default>
29+
<details open>
30+
<summary>Description</summary>
31+
<Description />
32+
<Examples />
33+
</details>
34+
</TabItem>
35+
36+
<TabItem value="solution" label="解答">
37+
## Solution
38+
<CodeBlock language="js">{Solution}</CodeBlock>
39+
</TabItem>
40+
</Tabs>
41+
42+
## 解題思路
43+
44+
要找的是大於等於 `n` 的且全由 `set bits` 組成的數字
45+
46+
`set bits` 指的是在二進位表示法中的 `1`
47+
而題目的範圍很小,為 `1 <= n <= 1000`
48+
所以其實可以簡單列出所有小於等於 `1000` 且轉成二進位的全都由 `1` 組成的數字:
49+
50+
- 十進位的 `1` 轉成二進位是 `1`
51+
- 十進位的 `3` 轉成二進位是 `11`
52+
- 十進位的 `7` 轉成二進位是 `111`
53+
- 十進位的 `15` 轉成二進位是 `1111`
54+
- 十進位的 `31` 轉成二進位是 `11111`
55+
- 十進位的 `63` 轉成二進位是 `111111`
56+
- 十進位的 `127` 轉成二進位是 `1111111`
57+
- 十進位的 `255` 轉成二進位是 `11111111`
58+
- 十進位的 `511` 轉成二進位是 `111111111`
59+
60+
再大則是十進位的 `1023` 轉成二進位是 `1111111111`
61+
62+
所以程式可以寫成這樣,不管傳入 $1 \leq n \leq 1000$ 的任何一個數字都能判斷
63+
64+
```js
65+
var smallestNumber = function (n) {
66+
if (n > 511) return 1023; // 512 ~ 1000
67+
if (n > 255) return 511; // 256 ~ 511
68+
if (n > 127) return 255; // 128 ~ 255
69+
if (n > 63) return 127; // 64 ~ 127
70+
if (n > 31) return 63; // 32 ~ 63
71+
if (n > 15) return 31; // 16 ~ 31
72+
if (n > 7) return 15; // 8 ~ 15
73+
if (n > 3) return 7; // 4 ~ 7
74+
if (n > 1) return 3; // 2 ~ 3
75+
76+
return 1; // 1
77+
};
78+
```
79+
80+
## 心得
81+
82+
酷酷的有點鑽漏洞
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
You are given a positive number n.
2+
3+
Return the smallest number x greater than or equal to n, such that the binary representation of x contains only set bits
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#### Example 1:
2+
3+
> **Input:** n = 5
4+
> **Output:** 7
5+
> **Explanation:** The binary representation of 7 is `"111"`.
6+
7+
#### Example 2:
8+
9+
> **Input:** n = 10
10+
> **Output:** 15
11+
> **Explanation:** The binary representation of 15 is `"1111"`.
12+
13+
#### Example 3:
14+
15+
> **Input:** n = 3
16+
> **Output:** 3
17+
> **Explanation:** The binary representation of 3 is `"11"`.
18+
19+
## Constraints:
20+
21+
- `1 <= n <= 1000`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var smallestNumber = function (n) {
6+
if (n > 511) return 1023;
7+
if (n > 255) return 511;
8+
if (n > 127) return 255;
9+
if (n > 63) return 127;
10+
if (n > 31) return 63;
11+
if (n > 15) return 31;
12+
if (n > 7) return 15;
13+
if (n > 3) return 7;
14+
if (n > 1) return 3;
15+
16+
return 1;
17+
};

0 commit comments

Comments
 (0)