Movatterモバイル変換


[0]ホーム

URL:


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

Commitb33e7b3

Browse files
Merge pull requestyoungyangyang04#418 from jojoo15/patch-36
优化 0738.单调递增的数字 python3
2 parents4cec106 +31ac198 commitb33e7b3

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,15 @@ class Solution {
147147

148148

149149
Python:
150-
```python
150+
```python3
151151
classSolution:
152152
defmonotoneIncreasingDigits(self,n:int) ->int:
153-
strNum=list(str(n))
154-
flag=len(strNum)
155-
for iinrange(len(strNum)-1,0,-1):
156-
ifint(strNum[i])<int(strNum[i-1]):
157-
strNum[i-1]=str(int(strNum[i-1])-1)
158-
flag= i
159-
for iinrange(flag,len(strNum)):
160-
strNum[i]='9'
161-
returnint("".join(strNum))
153+
a=list(str(n))
154+
for iinrange(len(a)-1,0,-1):
155+
ifint(a[i])<int(a[i-1]):
156+
a[i-1]=str(int(a[i-1])-1)
157+
a[i:]='9'* (len(a)- i)#python不需要设置flag值,直接按长度给9就好了
158+
returnint("".join(a))
162159
```
163160

164161
Go:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp