mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 07:06:42 +08:00
Update 0738.单调递增的数字.md
Python代码里大写的N会报错,把N改为了小写n。
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user