50
50
51
51
** 方法一:贪心**
52
52
53
- 我们定义函数 $f(x)$ 表示一个整数 $x$ 的每一位数字之和,那么题目要求的最小非负整数 $x$ 就是 $ f(n + x) \leq target$ 的最小值 。
53
+ 我们定义函数 $f(x)$ 表示一个整数 $x$ 的每一位数字之和,那么题目求的是 $ f(n + x) \leq target$ 的最小非负整数 $x$ 。
54
54
55
- 初始化 $x = 0$,循环判断 $f(n+x)$ 是否大于 $target$,如果大于,此时 $n+x$ 的最低一位非 $0$ 的数要置为 $0$,而前一位要加 $1$,然后继续判断。
55
+ 如果 $y = n+x$ 的每一位数字之和大于 $target$,那么我们可以循环通过以下操作,将 $y$ 的每一位数字之和减小到小于等于 $target$:
56
+
57
+ - 找到 $y$ 的最低位的非零数字,将其减小到 $0$,并将其高一位的数字加 $1$;
58
+ - 更新 $x$,继续上述操作,直到 $n+x$ 的每一位数字之和小于等于 $target$。
56
59
57
60
循环结束,返回 $x$ 即可。
58
61
59
- 时间复杂度 $O(\log^2 n)$。
62
+ 我们可以举个例子,假设 $n=467$, $target=6$,那么 $n$ 的变化过程如下:
63
+
64
+ $$
65
+ \begin{aligned}
66
+ & 467 \rightarrow 470 \rightarrow 500 \\
67
+ \end{aligned}
68
+ $$
69
+
70
+ 时间复杂度 $O(\log^2 n)$,其中 $n$ 给题目给定的整数。空间复杂度 $O(1)$。
60
71
61
72
<!-- tabs:start -->
62
73
67
78
``` python
68
79
class Solution :
69
80
def makeIntegerBeautiful (self , n : int , target : int ) -> int :
70
- def f (x ) :
71
- v = 0
81
+ def f (x : int ) -> int :
82
+ y = 0
72
83
while x:
73
- v += x % 10
84
+ y += x % 10
74
85
x //= 10
75
- return v
86
+ return y
76
87
77
88
x = 0
78
89
while f(n + x) > target:
@@ -106,32 +117,32 @@ class Solution {
106
117
}
107
118
108
119
private int f (long x ) {
109
- int v = 0 ;
120
+ int y = 0 ;
110
121
while (x > 0 ) {
111
- v += x % 10 ;
122
+ y += x % 10 ;
112
123
x /= 10 ;
113
124
}
114
- return v ;
125
+ return y ;
115
126
}
116
127
}
117
128
```
118
129
119
130
### ** C++**
120
131
121
132
``` cpp
122
- using ll = long long ;
123
-
124
133
class Solution {
125
134
public:
126
135
long long makeIntegerBeautiful(long long n, int target) {
136
+ using ll = long long;
127
137
auto f = [ ] (ll x) {
128
- int v = 0;
138
+ int y = 0;
129
139
while (x) {
130
- v += x % 10;
140
+ y += x % 10;
131
141
x /= 10;
132
142
}
133
- return v ;
143
+ return y ;
134
144
};
145
+
135
146
ll x = 0;
136
147
while (f(n + x) > target) {
137
148
ll y = n + x;
@@ -150,16 +161,13 @@ public:
150
161
### ** Go**
151
162
152
163
``` go
153
- func makeIntegerBeautiful(n int64, target int) int64 {
154
- f := func(x int64) int {
155
- v := 0
156
- for x > 0 {
157
- v += int(x % 10)
158
- x /= 10
164
+ func makeIntegerBeautiful (n int64 , target int ) (x int64 ) {
165
+ f := func (x int64 ) (y int ) {
166
+ for ; x > 0 ; x /= 10 {
167
+ y += int (x % 10 )
159
168
}
160
- return v
169
+ return
161
170
}
162
- var x int64
163
171
for f (n+x) > target {
164
172
y := n + x
165
173
var p int64 = 10
@@ -169,14 +177,34 @@ func makeIntegerBeautiful(n int64, target int) int64 {
169
177
}
170
178
x = (y/10 +1 )*p - n
171
179
}
172
- return x
180
+ return
173
181
}
174
182
```
175
183
176
184
### ** TypeScript**
177
185
178
186
``` ts
179
-
187
+ function makeIntegerBeautiful(n : number , target : number ): number {
188
+ const f = (x : number ): number => {
189
+ let y = 0 ;
190
+ for (; x > 0 ; x = Math .floor (x / 10 )) {
191
+ y += x % 10 ;
192
+ }
193
+ return y ;
194
+ };
195
+
196
+ let x = 0 ;
197
+ while (f (n + x ) > target ) {
198
+ let y = n + x ;
199
+ let p = 10 ;
200
+ while (y % 10 === 0 ) {
201
+ y = Math .floor (y / 10 );
202
+ p *= 10 ;
203
+ }
204
+ x = (Math .floor (y / 10 ) + 1 ) * p - n ;
205
+ }
206
+ return x ;
207
+ }
180
208
```
181
209
182
210
### ** ...**
0 commit comments