mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
Update 0738.单调递增的数字.md
Python代码里大写的N会报错,把N改为了小写n。
This commit is contained in:
@ -190,9 +190,9 @@ class Solution:
|
|||||||
贪心(版本一)
|
贪心(版本一)
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def monotoneIncreasingDigits(self, N: int) -> int:
|
def monotoneIncreasingDigits(self, n: int) -> int:
|
||||||
# 将整数转换为字符串
|
# 将整数转换为字符串
|
||||||
strNum = str(N)
|
strNum = str(n)
|
||||||
# flag用来标记赋值9从哪里开始
|
# flag用来标记赋值9从哪里开始
|
||||||
# 设置为字符串长度,为了防止第二个for循环在flag没有被赋值的情况下执行
|
# 设置为字符串长度,为了防止第二个for循环在flag没有被赋值的情况下执行
|
||||||
flag = len(strNum)
|
flag = len(strNum)
|
||||||
@ -216,9 +216,9 @@ class Solution:
|
|||||||
贪心(版本二)
|
贪心(版本二)
|
||||||
```python
|
```python
|
||||||
class Solution:
|
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):
|
for i in range(len(strNum) - 1, 0, -1):
|
||||||
@ -238,9 +238,9 @@ class Solution:
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
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):
|
for i in range(len(strNum) - 1, 0, -1):
|
||||||
@ -258,8 +258,8 @@ class Solution:
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def monotoneIncreasingDigits(self, N: int) -> int:
|
def monotoneIncreasingDigits(self, n: int) -> int:
|
||||||
strNum = str(N)
|
strNum = str(n)
|
||||||
for i in range(len(strNum) - 1, 0, -1):
|
for i in range(len(strNum) - 1, 0, -1):
|
||||||
# 如果当前字符比前一个字符小,说明需要修改前一个字符
|
# 如果当前字符比前一个字符小,说明需要修改前一个字符
|
||||||
if strNum[i - 1] > strNum[i]:
|
if strNum[i - 1] > strNum[i]:
|
||||||
@ -272,12 +272,12 @@ class Solution:
|
|||||||
```
|
```
|
||||||
### Go
|
### Go
|
||||||
```go
|
```go
|
||||||
func monotoneIncreasingDigits(N int) int {
|
func monotoneIncreasingDigits(n int) int {
|
||||||
s := strconv.Itoa(N)//将数字转为字符串,方便使用下标
|
s := strconv.Itoa(N)//将数字转为字符串,方便使用下标
|
||||||
ss := []byte(s)//将字符串转为byte数组,方便更改。
|
ss := []byte(s)//将字符串转为byte数组,方便更改。
|
||||||
n := len(ss)
|
n := len(ss)
|
||||||
if n <= 1 {
|
if n <= 1 {
|
||||||
return N
|
return n
|
||||||
}
|
}
|
||||||
for i := n-1; i > 0; i-- {
|
for i := n-1; i > 0; i-- {
|
||||||
if ss[i-1] > ss[i] { //前一个大于后一位,前一位减1,后面的全部置为9
|
if ss[i-1] > ss[i] { //前一个大于后一位,前一位减1,后面的全部置为9
|
||||||
|
Reference in New Issue
Block a user