Skip to content

Commit 3a128b3

Browse files
committed
feat: add solutions to lc problem: No.0991
No.0991.Broken Calculator
1 parent d61d5c5 commit 3a128b3

File tree

6 files changed

+186
-2
lines changed

6 files changed

+186
-2
lines changed

solution/0900-0999/0991.Broken Calculator/README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,90 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:逆向计算**
57+
58+
我们可以采用逆向计算的方式,从 `target` 开始,如果 `target` 是奇数,则 `target++`,否则 `target >>= 1`,累加操作次数,直到 `target` 小于等于 `startValue`,此时的操作次数加上 `startValue - target` 即为最终结果。
59+
60+
时间复杂度 $O(\log n)$,其中 $n$ 为 `target`。空间复杂度 $O(1)$。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

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

6268
```python
63-
69+
class Solution:
70+
def brokenCalc(self, startValue: int, target: int) -> int:
71+
ans = 0
72+
while startValue < target:
73+
if target & 1:
74+
target += 1
75+
else:
76+
target >>= 1
77+
ans += 1
78+
ans += startValue - target
79+
return ans
6480
```
6581

6682
### **Java**
6783

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

7086
```java
87+
class Solution {
88+
public int brokenCalc(int startValue, int target) {
89+
int ans = 0;
90+
while (startValue < target) {
91+
if ((target & 1) == 1) {
92+
target++;
93+
} else {
94+
target >>= 1;
95+
}
96+
ans += 1;
97+
}
98+
ans += startValue - target;
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int brokenCalc(int startValue, int target) {
110+
int ans = 0;
111+
while (startValue < target) {
112+
if (target & 1) {
113+
target++;
114+
} else {
115+
target >>= 1;
116+
}
117+
++ans;
118+
}
119+
ans += startValue - target;
120+
return ans;
121+
}
122+
};
123+
```
71124
125+
### **Go**
126+
127+
```go
128+
func brokenCalc(startValue int, target int) (ans int) {
129+
for startValue < target {
130+
if target&1 == 1 {
131+
target++
132+
} else {
133+
target >>= 1
134+
}
135+
ans++
136+
}
137+
ans += startValue - target
138+
return
139+
}
72140
```
73141

74142
### **...**

solution/0900-0999/0991.Broken Calculator/README_EN.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,75 @@
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def brokenCalc(self, startValue: int, target: int) -> int:
57+
ans = 0
58+
while startValue < target:
59+
if target & 1:
60+
target += 1
61+
else:
62+
target >>= 1
63+
ans += 1
64+
ans += startValue - target
65+
return ans
5666
```
5767

5868
### **Java**
5969

6070
```java
71+
class Solution {
72+
public int brokenCalc(int startValue, int target) {
73+
int ans = 0;
74+
while (startValue < target) {
75+
if ((target & 1) == 1) {
76+
target++;
77+
} else {
78+
target >>= 1;
79+
}
80+
ans += 1;
81+
}
82+
ans += startValue - target;
83+
return ans;
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int brokenCalc(int startValue, int target) {
94+
int ans = 0;
95+
while (startValue < target) {
96+
if (target & 1) {
97+
target++;
98+
} else {
99+
target >>= 1;
100+
}
101+
++ans;
102+
}
103+
ans += startValue - target;
104+
return ans;
105+
}
106+
};
107+
```
61108
109+
### **Go**
110+
111+
```go
112+
func brokenCalc(startValue int, target int) (ans int) {
113+
for startValue < target {
114+
if target&1 == 1 {
115+
target++
116+
} else {
117+
target >>= 1
118+
}
119+
ans++
120+
}
121+
ans += startValue - target
122+
return
123+
}
62124
```
63125

64126
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int brokenCalc(int startValue, int target) {
4+
int ans = 0;
5+
while (startValue < target) {
6+
if (target & 1) {
7+
target++;
8+
} else {
9+
target >>= 1;
10+
}
11+
++ans;
12+
}
13+
ans += startValue - target;
14+
return ans;
15+
}
16+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func brokenCalc(startValue int, target int) (ans int) {
2+
for startValue < target {
3+
if target&1 == 1 {
4+
target++
5+
} else {
6+
target >>= 1
7+
}
8+
ans++
9+
}
10+
ans += startValue - target
11+
return
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int brokenCalc(int startValue, int target) {
3+
int ans = 0;
4+
while (startValue < target) {
5+
if ((target & 1) == 1) {
6+
target++;
7+
} else {
8+
target >>= 1;
9+
}
10+
ans += 1;
11+
}
12+
ans += startValue - target;
13+
return ans;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def brokenCalc(self, startValue: int, target: int) -> int:
3+
ans = 0
4+
while startValue < target:
5+
if target & 1:
6+
target += 1
7+
else:
8+
target >>= 1
9+
ans += 1
10+
ans += startValue - target
11+
return ans

0 commit comments

Comments
 (0)