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 23c2613

Browse files
committed
添加 0738.单调递增的数字.md Scala版本
1 parent eddfde7 commit 23c2613

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎problems/0738.单调递增的数字.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,37 @@ function monotoneIncreasingDigits(n: number): number {
246246
```
247247

248248

249+
### Scala
250+
251+
直接转换为了整数数组:
252+
```scala
253+
object Solution {
254+
import scala.collection.mutable
255+
def monotoneIncreasingDigits(n: Int): Int = {
256+
var digits = mutable.ArrayBuffer[Int]()
257+
// 提取每位数字
258+
var temp = n // 因为 参数n 是不可变量所以需要赋值给一个可变量
259+
while (temp != 0) {
260+
digits.append(temp % 10)
261+
temp = temp / 10
262+
}
263+
// 贪心
264+
var flag = -1
265+
for (i <- 0 until (digits.length - 1) if digits(i) < digits(i + 1)) {
266+
flag = i
267+
digits(i + 1) -= 1
268+
}
269+
for (i <- 0 to flag) digits(i) = 9
249270

271+
// 拼接
272+
var res = 0
273+
for (i <- 0 until digits.length) {
274+
res += digits(i) * math.pow(10, i).toInt
275+
}
276+
res
277+
}
278+
}
279+
```
250280

251281
-----------------------
252282
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

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