From 31ac198cd18ab1c1eb32665ddf52623c6e3b6c03 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Thu, 17 Jun 2021 18:11:09 +0200 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=200738.=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97=20python3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 0738.单调递增的数字 python3 --- ...\236\347\232\204\346\225\260\345\255\227.md" | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index 5bddb23440..665f0fd6b0 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -147,18 +147,15 @@ class Solution { Python: -```python +```python3 class Solution: def monotoneIncreasingDigits(self, n: int) -> int: - strNum = list(str(n)) - flag = len(strNum) - for i in range(len(strNum) - 1, 0, -1): - if int(strNum[i]) < int(strNum[i - 1]): - strNum[i - 1] = str(int(strNum[i - 1]) - 1) - flag = i - for i in range(flag, len(strNum)): - strNum[i] = '9' - return int("".join(strNum)) + a = list(str(n)) + for i in range(len(a)-1,0,-1): + if int(a[i]) < int(a[i-1]): + a[i-1] = str(int(a[i-1]) - 1) + a[i:] = '9' * (len(a) - i) #python不需要设置flag值,直接按长度给9就好了 + return int("".join(a)) ``` Go: