Skip to content

Commit d5e8663

Browse files
committed
feat: add solutions to lc problem: No.0738
No.0738.Monotone Increasing Digits
1 parent d1d9e4c commit d5e8663

File tree

6 files changed

+229
-2
lines changed

6 files changed

+229
-2
lines changed

solution/0700-0799/0738.Monotone Increasing Digits/README.md

+86-1
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,107 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:贪心**
49+
50+
从数字 `n` 的高位开始,找到第一个不满足 $n_{i-1} \le n_i$ 的位置 $i$。
51+
52+
然后,从后往前,只要发现 $n_{i-1} \gt n_i$,就将 $n_{i-1}$ 减 1。
53+
54+
最后将位置 $i$ 之后的所有数字都置为 9 即可。
55+
56+
时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。
57+
4858
<!-- tabs:start -->
4959

5060
### **Python3**
5161

5262
<!-- 这里可写当前语言的特殊实现逻辑 -->
5363

5464
```python
55-
65+
class Solution:
66+
def monotoneIncreasingDigits(self, n: int) -> int:
67+
s = list(str(n))
68+
i = 1
69+
while i < len(s) and s[i - 1] <= s[i]:
70+
i += 1
71+
if i < len(s):
72+
while i and s[i - 1] > s[i]:
73+
s[i - 1] = str(int(s[i - 1]) - 1)
74+
i -= 1
75+
i += 1
76+
while i < len(s):
77+
s[i] = '9'
78+
i += 1
79+
return int(''.join(s))
5680
```
5781

5882
### **Java**
5983

6084
<!-- 这里可写当前语言的特殊实现逻辑 -->
6185

6286
```java
87+
class Solution {
88+
public int monotoneIncreasingDigits(int n) {
89+
char[] s = String.valueOf(n).toCharArray();
90+
int i = 1;
91+
for (; i < s.length && s[i - 1] <= s[i]; ++i);
92+
if (i < s.length) {
93+
for (; i > 0 && s[i - 1] > s[i]; --i) {
94+
--s[i - 1];
95+
}
96+
++i;
97+
for (; i < s.length; ++i) {
98+
s[i] = '9';
99+
}
100+
}
101+
return Integer.parseInt(String.valueOf(s));
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int monotoneIncreasingDigits(int n) {
112+
string s = to_string(n);
113+
int i = 1;
114+
for (; i < s.size() && s[i - 1] <= s[i]; ++i);
115+
if (i < s.size()) {
116+
for (; i > 0 && s[i - 1] > s[i]; --i) {
117+
--s[i - 1];
118+
}
119+
++i;
120+
for (; i < s.size(); ++i) {
121+
s[i] = '9';
122+
}
123+
}
124+
return stoi(s);
125+
}
126+
};
127+
```
63128
129+
### **Go**
130+
131+
```go
132+
func monotoneIncreasingDigits(n int) int {
133+
s := []byte(strconv.Itoa(n))
134+
i := 1
135+
for ; i < len(s) && s[i-1] <= s[i]; i++ {
136+
}
137+
if i < len(s) {
138+
for ; i > 0 && s[i-1] > s[i]; i-- {
139+
s[i-1]--
140+
}
141+
i++
142+
for ; i < len(s); i++ {
143+
s[i] = '9'
144+
}
145+
}
146+
ans, _ := strconv.Atoi(string(s))
147+
return ans
148+
}
64149
```
65150

66151
### **...**

solution/0700-0799/0738.Monotone Increasing Digits/README_EN.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,88 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def monotoneIncreasingDigits(self, n: int) -> int:
49+
s = list(str(n))
50+
i = 1
51+
while i < len(s) and s[i - 1] <= s[i]:
52+
i += 1
53+
if i < len(s):
54+
while i and s[i - 1] > s[i]:
55+
s[i - 1] = str(int(s[i - 1]) - 1)
56+
i -= 1
57+
i += 1
58+
while i < len(s):
59+
s[i] = '9'
60+
i += 1
61+
return int(''.join(s))
4862
```
4963

5064
### **Java**
5165

5266
```java
67+
class Solution {
68+
public int monotoneIncreasingDigits(int n) {
69+
char[] s = String.valueOf(n).toCharArray();
70+
int i = 1;
71+
for (; i < s.length && s[i - 1] <= s[i]; ++i);
72+
if (i < s.length) {
73+
for (; i > 0 && s[i - 1] > s[i]; --i) {
74+
--s[i - 1];
75+
}
76+
++i;
77+
for (; i < s.length; ++i) {
78+
s[i] = '9';
79+
}
80+
}
81+
return Integer.parseInt(String.valueOf(s));
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
int monotoneIncreasingDigits(int n) {
92+
string s = to_string(n);
93+
int i = 1;
94+
for (; i < s.size() && s[i - 1] <= s[i]; ++i);
95+
if (i < s.size()) {
96+
for (; i > 0 && s[i - 1] > s[i]; --i) {
97+
--s[i - 1];
98+
}
99+
++i;
100+
for (; i < s.size(); ++i) {
101+
s[i] = '9';
102+
}
103+
}
104+
return stoi(s);
105+
}
106+
};
107+
```
53108
109+
### **Go**
110+
111+
```go
112+
func monotoneIncreasingDigits(n int) int {
113+
s := []byte(strconv.Itoa(n))
114+
i := 1
115+
for ; i < len(s) && s[i-1] <= s[i]; i++ {
116+
}
117+
if i < len(s) {
118+
for ; i > 0 && s[i-1] > s[i]; i-- {
119+
s[i-1]--
120+
}
121+
i++
122+
for ; i < len(s); i++ {
123+
s[i] = '9'
124+
}
125+
}
126+
ans, _ := strconv.Atoi(string(s))
127+
return ans
128+
}
54129
```
55130

56131
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int monotoneIncreasingDigits(int n) {
4+
string s = to_string(n);
5+
int i = 1;
6+
for (; i < s.size() && s[i - 1] <= s[i]; ++i);
7+
if (i < s.size()) {
8+
for (; i > 0 && s[i - 1] > s[i]; --i) {
9+
--s[i - 1];
10+
}
11+
++i;
12+
for (; i < s.size(); ++i) {
13+
s[i] = '9';
14+
}
15+
}
16+
return stoi(s);
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func monotoneIncreasingDigits(n int) int {
2+
s := []byte(strconv.Itoa(n))
3+
i := 1
4+
for ; i < len(s) && s[i-1] <= s[i]; i++ {
5+
}
6+
if i < len(s) {
7+
for ; i > 0 && s[i-1] > s[i]; i-- {
8+
s[i-1]--
9+
}
10+
i++
11+
for ; i < len(s); i++ {
12+
s[i] = '9'
13+
}
14+
}
15+
ans, _ := strconv.Atoi(string(s))
16+
return ans
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int monotoneIncreasingDigits(int n) {
3+
char[] s = String.valueOf(n).toCharArray();
4+
int i = 1;
5+
for (; i < s.length && s[i - 1] <= s[i]; ++i);
6+
if (i < s.length) {
7+
for (; i > 0 && s[i - 1] > s[i]; --i) {
8+
--s[i - 1];
9+
}
10+
++i;
11+
for (; i < s.length; ++i) {
12+
s[i] = '9';
13+
}
14+
}
15+
return Integer.parseInt(String.valueOf(s));
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def monotoneIncreasingDigits(self, n: int) -> int:
3+
s = list(str(n))
4+
i = 1
5+
while i < len(s) and s[i - 1] <= s[i]:
6+
i += 1
7+
if i < len(s):
8+
while i and s[i - 1] > s[i]:
9+
s[i - 1] = str(int(s[i - 1]) - 1)
10+
i -= 1
11+
i += 1
12+
while i < len(s):
13+
s[i] = '9'
14+
i += 1
15+
return int(''.join(s))

0 commit comments

Comments
 (0)