File tree Expand file tree Collapse file tree 6 files changed +186
-2
lines changed
solution/0900-0999/0991.Broken Calculator Expand file tree Collapse file tree 6 files changed +186
-2
lines changed Original file line number Diff line number Diff line change 53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
+ ** 方法一:逆向计算**
57
+
58
+ 我们可以采用逆向计算的方式,从 ` target ` 开始,如果 ` target ` 是奇数,则 ` target++ ` ,否则 ` target >>= 1 ` ,累加操作次数,直到 ` target ` 小于等于 ` startValue ` ,此时的操作次数加上 ` startValue - target ` 即为最终结果。
59
+
60
+ 时间复杂度 $O(\log n)$,其中 $n$ 为 ` target ` 。空间复杂度 $O(1)$。
61
+
56
62
<!-- tabs:start -->
57
63
58
64
### ** Python3**
59
65
60
66
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
67
62
68
``` 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
64
80
```
65
81
66
82
### ** Java**
67
83
68
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
85
70
86
``` 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
+ ```
71
124
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
+ }
72
140
```
73
141
74
142
### ** ...**
Original file line number Diff line number Diff line change 52
52
### ** Python3**
53
53
54
54
``` 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
56
66
```
57
67
58
68
### ** Java**
59
69
60
70
``` 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
+ ```
61
108
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
+ }
62
124
```
63
125
64
126
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments