Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 72b6c6c

Browse files
committed
feat: add solutions to lc problem: No.1404
No.1404.Number of Steps to Reduce a Number in Binary Representation to One
1 parent 5a569cf commit 72b6c6c

File tree

6 files changed

+311
-2
lines changed

6 files changed

+311
-2
lines changed

‎solution/1400-1499/1404.Number of Steps to Reduce a Number in Binary Representation to One/README.md‎

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,130 @@ Step 1) 2 是偶数,除 2 得到 1
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:模拟操作**
66+
67+
模拟操作 1/2,同时用 carry 记录进位。
68+
6569
<!-- tabs:start -->
6670

6771
### **Python3**
6872

6973
<!-- 这里可写当前语言的特殊实现逻辑 -->
7074

7175
```python
72-
76+
class Solution:
77+
def numSteps(self, s: str) -> int:
78+
carry = False
79+
ans = 0
80+
for c in s[:0:-1]:
81+
if carry:
82+
if c == '0':
83+
c = '1'
84+
carry = False
85+
else:
86+
c = '0'
87+
if c == '1':
88+
ans += 1
89+
carry = True
90+
ans += 1
91+
if carry:
92+
ans += 1
93+
return ans
7394
```
7495

7596
### **Java**
7697

7798
<!-- 这里可写当前语言的特殊实现逻辑 -->
7899

79100
```java
101+
class Solution {
102+
public int numSteps(String s) {
103+
boolean carry = false;
104+
int ans = 0;
105+
for (int i = s.length() - 1; i > 0; --i) {
106+
char c = s.charAt(i);
107+
if (carry) {
108+
if (c == '0') {
109+
c = '1';
110+
carry = false;
111+
} else {
112+
c = '0';
113+
}
114+
}
115+
if (c == '1') {
116+
++ans;
117+
carry = true;
118+
}
119+
++ans;
120+
}
121+
if (carry) {
122+
++ans;
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
int numSteps(string s) {
135+
int ans = 0;
136+
bool carry = false;
137+
for (int i = s.size() - 1; i; --i)
138+
{
139+
char c = s[i];
140+
if (carry)
141+
{
142+
if (c == '0')
143+
{
144+
c = '1';
145+
carry = false;
146+
}
147+
else c = '0';
148+
}
149+
if (c == '1')
150+
{
151+
++ans;
152+
carry = true;
153+
}
154+
++ans;
155+
}
156+
if (carry) ++ans;
157+
return ans;
158+
}
159+
};
160+
```
80161
162+
### **Go**
163+
164+
```go
165+
func numSteps(s string) int {
166+
ans := 0
167+
carry := false
168+
for i := len(s) - 1; i > 0; i-- {
169+
c := s[i]
170+
if carry {
171+
if c == '0' {
172+
c = '1'
173+
carry = false
174+
} else {
175+
c = '0'
176+
}
177+
}
178+
if c == '1' {
179+
ans++
180+
carry = true
181+
}
182+
ans++
183+
}
184+
if carry {
185+
ans++
186+
}
187+
return ans
188+
}
81189
```
82190

83191
### **...**

‎solution/1400-1499/1404.Number of Steps to Reduce a Number in Binary Representation to One/README_EN.md‎

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,120 @@ Step 1) 2 is even, divide by 2 and obtain 1.&nbsp;
6464
### **Python3**
6565

6666
```python
67-
67+
class Solution:
68+
def numSteps(self, s: str) -> int:
69+
carry = False
70+
ans = 0
71+
for c in s[:0:-1]:
72+
if carry:
73+
if c == '0':
74+
c = '1'
75+
carry = False
76+
else:
77+
c = '0'
78+
if c == '1':
79+
ans += 1
80+
carry = True
81+
ans += 1
82+
if carry:
83+
ans += 1
84+
return ans
6885
```
6986

7087
### **Java**
7188

7289
```java
90+
class Solution {
91+
public int numSteps(String s) {
92+
boolean carry = false;
93+
int ans = 0;
94+
for (int i = s.length() - 1; i > 0; --i) {
95+
char c = s.charAt(i);
96+
if (carry) {
97+
if (c == '0') {
98+
c = '1';
99+
carry = false;
100+
} else {
101+
c = '0';
102+
}
103+
}
104+
if (c == '1') {
105+
++ans;
106+
carry = true;
107+
}
108+
++ans;
109+
}
110+
if (carry) {
111+
++ans;
112+
}
113+
return ans;
114+
}
115+
}
116+
```
73117

118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
int numSteps(string s) {
124+
int ans = 0;
125+
bool carry = false;
126+
for (int i = s.size() - 1; i; --i)
127+
{
128+
char c = s[i];
129+
if (carry)
130+
{
131+
if (c == '0')
132+
{
133+
c = '1';
134+
carry = false;
135+
}
136+
else c = '0';
137+
}
138+
if (c == '1')
139+
{
140+
++ans;
141+
carry = true;
142+
}
143+
++ans;
144+
}
145+
if (carry) ++ans;
146+
return ans;
147+
}
148+
};
74149
```
75150
151+
### **Go**
152+
153+
```go
154+
func numSteps(s string) int {
155+
ans := 0
156+
carry := false
157+
for i := len(s) - 1; i > 0; i-- {
158+
c := s[i]
159+
if carry {
160+
if c == '0' {
161+
c = '1'
162+
carry = false
163+
} else {
164+
c = '0'
165+
}
166+
}
167+
if c == '1' {
168+
ans++
169+
carry = true
170+
}
171+
ans++
172+
}
173+
if carry {
174+
ans++
175+
}
176+
return ans
177+
}
178+
```
179+
180+
76181
### **...**
77182

78183
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int numSteps(string s) {
4+
int ans = 0;
5+
bool carry = false;
6+
for (int i = s.size() - 1; i; --i)
7+
{
8+
char c = s[i];
9+
if (carry)
10+
{
11+
if (c == '0')
12+
{
13+
c = '1';
14+
carry = false;
15+
}
16+
else c = '0';
17+
}
18+
if (c == '1')
19+
{
20+
++ans;
21+
carry = true;
22+
}
23+
++ans;
24+
}
25+
if (carry) ++ans;
26+
return ans;
27+
}
28+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func numSteps(s string) int {
2+
ans := 0
3+
carry := false
4+
for i := len(s) - 1; i > 0; i-- {
5+
c := s[i]
6+
if carry {
7+
if c == '0' {
8+
c = '1'
9+
carry = false
10+
} else {
11+
c = '0'
12+
}
13+
}
14+
if c == '1' {
15+
ans++
16+
carry = true
17+
}
18+
ans++
19+
}
20+
if carry {
21+
ans++
22+
}
23+
return ans
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int numSteps(String s) {
3+
boolean carry = false;
4+
int ans = 0;
5+
for (int i = s.length() - 1; i > 0; --i) {
6+
char c = s.charAt(i);
7+
if (carry) {
8+
if (c == '0') {
9+
c = '1';
10+
carry = false;
11+
} else {
12+
c = '0';
13+
}
14+
}
15+
if (c == '1') {
16+
++ans;
17+
carry = true;
18+
}
19+
++ans;
20+
}
21+
if (carry) {
22+
++ans;
23+
}
24+
return ans;
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def numSteps(self, s: str) -> int:
3+
carry = False
4+
ans = 0
5+
for c in s[:0:-1]:
6+
if carry:
7+
if c == '0':
8+
c = '1'
9+
carry = False
10+
else:
11+
c = '0'
12+
if c == '1':
13+
ans += 1
14+
carry = True
15+
ans += 1
16+
if carry:
17+
ans += 1
18+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /