Merge pull request #1 from htzhu/htzhu-patch-1

Update 0738.单调递增的数字.md
This commit is contained in:
htzhu
2024-10-26 21:45:41 -07:00
committed by GitHub

View File

@ -190,9 +190,9 @@ class Solution:
贪心(版本一)
```python
class Solution:
def monotoneIncreasingDigits(self, N: int) -> int:
def monotoneIncreasingDigits(self, n: int) -> int:
# 将整数转换为字符串
strNum = str(N)
strNum = str(n)
# flag用来标记赋值9从哪里开始
# 设置为字符串长度为了防止第二个for循环在flag没有被赋值的情况下执行
flag = len(strNum)
@ -216,9 +216,9 @@ class Solution:
贪心(版本二)
```python
class Solution:
def monotoneIncreasingDigits(self, N: int) -> int:
def monotoneIncreasingDigits(self, n: int) -> int:
# 将整数转换为字符串
strNum = list(str(N))
strNum = list(str(n))
# 从右往左遍历字符串
for i in range(len(strNum) - 1, 0, -1):
@ -238,9 +238,9 @@ class Solution:
```python
class Solution:
def monotoneIncreasingDigits(self, N: int) -> int:
def monotoneIncreasingDigits(self, n: int) -> int:
# 将整数转换为字符串
strNum = list(str(N))
strNum = list(str(n))
# 从右往左遍历字符串
for i in range(len(strNum) - 1, 0, -1):
@ -258,8 +258,8 @@ class Solution:
```python
class Solution:
def monotoneIncreasingDigits(self, N: int) -> int:
strNum = str(N)
def monotoneIncreasingDigits(self, n: int) -> int:
strNum = str(n)
for i in range(len(strNum) - 1, 0, -1):
# 如果当前字符比前一个字符小,说明需要修改前一个字符
if strNum[i - 1] > strNum[i]:
@ -272,12 +272,12 @@ class Solution:
```
### Go
```go
func monotoneIncreasingDigits(N int) int {
func monotoneIncreasingDigits(n int) int {
s := strconv.Itoa(N)//将数字转为字符串,方便使用下标
ss := []byte(s)//将字符串转为byte数组方便更改。
n := len(ss)
if n <= 1 {
return N
return n
}
for i := n-1; i > 0; i-- {
if ss[i-1] > ss[i] { //前一个大于后一位,前一位减1后面的全部置为9