Skip to content

Commit ed1fa34

Browse files
TisfyLetMeFly666
andauthored
update: 添加问题“1404.将二进制表示减到1的步骤数”的代码和题解 (#1415)
* 1404: HALF.cpp + TLE.py * 1404: WA.py (#1413) * 1404: AC.py (#1413) - AC,100.00%,31.67% archive * docs: en + git(4 days ago) * 1404: WA.cpp (#1413) * 1404: AC.cpp (#1413) - AC,5.11%,5.96% - 高精度模拟 * update: 添加问题“1404.将二进制表示减到1的步骤数”的代码和题解 (#1415) Signed-off-by: Tisfy <Tisfy@foxmail.com> * fix --------- Signed-off-by: Tisfy <Tisfy@foxmail.com> Co-authored-by: LetMeFly666 <Tisfy@qq.com>
1 parent 1c48972 commit ed1fa34

9 files changed

Lines changed: 415 additions & 6 deletions

.commitmsg

Lines changed: 0 additions & 1 deletion
This file was deleted.

Codes/1356-sort-integers-by-the-number-of-1-bits_20260225.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2026-02-25 21:36:09
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-02-25 21:37:54
5+
* @LastEditTime: 2026-02-26 22:05:15
66
*/
77
#if defined(_WIN32) || defined(__APPLE__)
88
#include "_[1,2]toVector.h"
@@ -11,7 +11,7 @@
1111
class Solution {
1212
public:
1313
vector<int> sortByBits(vector<int>& arr) {
14-
sort(arr.begin(), arr.end(), [](int& a, int& b) {
14+
sort(arr.begin(), arr.end(), [](const int& a, const int& b) {
1515
int ca = __builtin_popcount(a);
1616
int cb = __builtin_popcount(b);
1717
return ca != cb ? ca < cb : a < b;
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-26 22:06:13
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-27 22:29:56
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/*
12+
1101
13+
1110
14+
111
15+
1000
16+
100
17+
10
18+
1
19+
*/
20+
21+
class LetsNum {
22+
private:
23+
// 低位->高位
24+
deque<char> num;
25+
public:
26+
// From binary string
27+
LetsNum(string& s) {
28+
for (char c : s) {
29+
*this <<= 1;
30+
*this += c;
31+
}
32+
}
33+
34+
LetsNum& operator++(int) {
35+
int carry = 1;
36+
for (deque<char>::iterator it = num.begin(); it != num.end(); it++) {
37+
carry += *it - '0';
38+
*it = carry % 10 + '0';
39+
carry /= 10;
40+
}
41+
if (carry) {
42+
num.push_back(carry + '0');
43+
}
44+
return *this;
45+
}
46+
47+
// only 0/1 supported
48+
LetsNum& operator+=(const int& n) {
49+
if (!n) {
50+
return *this;
51+
}
52+
(*this)++;
53+
return *this;
54+
}
55+
56+
// only 0/1 supported
57+
LetsNum& operator+=(const char& c) {
58+
*this += c - '0';
59+
return *this;
60+
}
61+
62+
// only <<1 supported
63+
LetsNum& operator<<=(const int&) {
64+
int carry = 0;
65+
for (deque<char>::iterator it = num.begin(); it != num.end(); it++) {
66+
carry += (*it - '0') * 2;
67+
*it = carry % 10 + '0';
68+
carry /= 10;
69+
}
70+
if (carry) {
71+
num.push_back(carry + '0');
72+
}
73+
return *this;
74+
}
75+
76+
// only >>1 supported
77+
LetsNum& operator>>=(const int&) {
78+
int mod = 0;
79+
for (deque<char>::reverse_iterator it = num.rbegin(); it != num.rend(); it++) {
80+
mod = mod * 10 + (*it - '0');
81+
*it = mod / 2 + '0';
82+
mod %= 2;
83+
}
84+
if (*num.rbegin() == '0') {
85+
num.pop_back();
86+
}
87+
return *this;
88+
}
89+
90+
// only 1 supported
91+
bool operator!=(const int&) {
92+
return num.size() != 1 || *num.begin() != '1';
93+
}
94+
95+
bool isOdd() {
96+
return (*num.begin() - '0') % 2;
97+
}
98+
99+
void print() {
100+
for (deque<char>::reverse_iterator it = num.rbegin(); it != num.rend(); it++) {
101+
putchar(*it);
102+
}
103+
puts("");
104+
}
105+
};
106+
107+
class Solution {
108+
public:
109+
int numSteps(string s) {
110+
LetsNum num(s);
111+
int ans = 0;
112+
while (num != 1) {
113+
if (num.isOdd()) {
114+
num += 1;
115+
} else {
116+
num >>= 1;
117+
}
118+
ans++;
119+
}
120+
return ans;
121+
}
122+
};
123+
124+
#if defined(_WIN32) || defined(__APPLE__)
125+
/*
126+
1101
127+
*/
128+
int main() {
129+
string s;
130+
while (cin >> s) {
131+
Solution sol;
132+
cout << sol.numSteps(s) << endl;
133+
}
134+
return 0;
135+
}
136+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2026-02-26 23:55:52
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2026-02-26 23:59:25
6+
'''
7+
"""
8+
1101
9+
1110
10+
111
11+
1000
12+
100
13+
10
14+
1
15+
"""
16+
class Solution:
17+
def numSteps(self, s: str) -> int:
18+
a = int(s, 2)
19+
ans = 0
20+
while a != 1:
21+
if a % 2:
22+
a += 1
23+
else:
24+
a //= 2
25+
ans += 1
26+
# print(a)
27+
return ans
28+
29+
# print(Solution.numSteps('', '1101'))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@
577577
|1399.统计最大组的数目|简单|<a href="https://leetcode.cn/problems/count-largest-group/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/23/LeetCode%201399.%E7%BB%9F%E8%AE%A1%E6%9C%80%E5%A4%A7%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147448113" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-largest-group/solutions/3659689/letmefly-1399tong-ji-zui-da-zu-de-shu-mu-09kx/" target="_blank">LeetCode题解</a>|
578578
|1402.做菜顺序|困难|<a href="https://leetcode.cn/problems/reducing-dishes/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/22/LeetCode%201402.%E5%81%9A%E8%8F%9C%E9%A1%BA%E5%BA%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133974648" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reducing-dishes/solutions/2493595/letmefly-1402zuo-cai-shun-xu-pai-xu-er-f-s5te/" target="_blank">LeetCode题解</a>|
579579
|1403.非递增顺序的最小子序列|简单|<a href="https://leetcode.cn/problems/minimum-subsequence-in-non-increasing-order/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/04/LeetCode%201403.%E9%9D%9E%E9%80%92%E5%A2%9E%E9%A1%BA%E5%BA%8F%E7%9A%84%E6%9C%80%E5%B0%8F%E5%AD%90%E5%BA%8F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126155397" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-subsequence-in-non-increasing-order/solution/letmefly-1403fei-di-zeng-shun-xu-de-zui-h56ht/" target="_blank">LeetCode题解</a>|
580+
|1404.将二进制表示减到1的步骤数|中等|<a href="https://leetcode.cn/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/27/LeetCode%201404.%E5%B0%86%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%A1%A8%E7%A4%BA%E5%87%8F%E5%88%B01%E7%9A%84%E6%AD%A5%E9%AA%A4%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158472038" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/solutions/3909907/letmefly-1404jiang-er-jin-zhi-biao-shi-j-ubn7/" target="_blank">LeetCode题解</a>|
580581
|1408.数组中的字符串匹配|简单|<a href="https://leetcode.cn/problems/string-matching-in-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/06/LeetCode%201408.%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8C%B9%E9%85%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126189255" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/string-matching-in-an-array/solution/letmefly-1408shu-zu-zhong-de-zi-fu-chuan-v90u/" target="_blank">LeetCode题解</a>|
581582
|1410.HTML实体解析器|中等|<a href="https://leetcode.cn/problems/html-entity-parser/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/23/LeetCode%201410.HTML%E5%AE%9E%E4%BD%93%E8%A7%A3%E6%9E%90%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134571778" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/html-entity-parser/solutions/2538393/letmefly-1410html-shi-ti-jie-xi-qi-zi-fu-psbk/" target="_blank">LeetCode题解</a>|
582583
|1411.给Nx3网格图涂色的方案数|困难|<a href="https://leetcode.cn/problems/number-of-ways-to-paint-n-3-grid" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/03/LeetCode%201411.%E7%BB%99Nx3%E7%BD%91%E6%A0%BC%E5%9B%BE%E6%B6%82%E8%89%B2%E7%9A%84%E6%96%B9%E6%A1%88%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156545186" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solutions/3872035/letmefly-1411gei-n-x-3-wang-ge-tu-tu-se-gbpc3/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 1356.根据数字二进制下1的数目排序.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ categories: [题解, LeetCode]
8282
class Solution {
8383
public:
8484
vector<int> sortByBits(vector<int>& arr) {
85-
sort(arr.begin(), arr.end(), [](int& a, int& b) {
85+
sort(arr.begin(), arr.end(), [](const int& a, const int& b) {
8686
int ca = __builtin_popcount(a);
8787
int cb = __builtin_popcount(b);
8888
return ca != cb ? ca < cb : a < b;

0 commit comments

Comments
 (0)