Skip to content

Commit 08f4164

Browse files
committed
feat: add solutions to lc problems: No.0670,0671
* No.0670.Maximum Swap * No.0671.Second Minimum Node In a Binary Tree
1 parent 26c5566 commit 08f4164

File tree

13 files changed

+321
-214
lines changed

13 files changed

+321
-214
lines changed

solution/0600-0699/0670.Maximum Swap/README.md

+61-41
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434

3535
<!-- 这里可写通用的实现逻辑 -->
3636

37+
**方法一:贪心**
38+
39+
先将数字转为字符串 `s`,然后从右往左遍历字符串 `s`,用数组 `d` 记录每个字符右侧的最大位置(可以是字符本身的位置)。
40+
41+
接着从左到右遍历 `d`,如果 `s[i] < s[d[i]]`,则进行交换,并退出遍历的过程。
42+
43+
最后将字符串 `s` 转为数字,即为答案。
44+
45+
时间复杂度 $O(\log C)$,空间复杂度 $O(\log C)$。其中 $C$ 是数字 `num` 的值域。
46+
3747
<!-- tabs:start -->
3848

3949
### **Python3**
@@ -43,17 +53,17 @@
4353
```python
4454
class Solution:
4555
def maximumSwap(self, num: int) -> int:
46-
chars = list(str(num))
47-
n = len(chars)
48-
for i in range(n - 1):
49-
mx = i + 1
50-
for j in range(i + 1, n):
51-
if ord(chars[j]) >= ord(chars[mx]):
52-
mx = j
53-
if ord(chars[i]) < ord(chars[mx]):
54-
chars[i], chars[mx] = chars[mx], chars[i]
56+
s = list(str(num))
57+
n = len(s)
58+
d = list(range(n))
59+
for i in range(n - 2, -1, -1):
60+
if s[i] <= s[d[i + 1]]:
61+
d[i] = d[i + 1]
62+
for i, j in enumerate(d):
63+
if s[i] < s[j]:
64+
s[i], s[j] = s[j], s[i]
5565
break
56-
return int(''.join(chars))
66+
return int(''.join(s))
5767
```
5868

5969
### **Java**
@@ -63,23 +73,27 @@ class Solution:
6373
```java
6474
class Solution {
6575
public int maximumSwap(int num) {
66-
char[] chars = String.valueOf(num).toCharArray();
67-
int n = chars.length;
68-
for (int i = 0; i < n - 1; ++i) {
69-
int mx = i + 1;
70-
for (int j = i + 1; j < n; ++j) {
71-
if (chars[j] >= chars[mx]) {
72-
mx = j;
73-
}
76+
char[] s = String.valueOf(num).toCharArray();
77+
int n = s.length;
78+
int[] d = new int[n];
79+
for (int i = 0; i < n; ++i) {
80+
d[i] = i;
81+
}
82+
for (int i = n - 2; i >= 0; --i) {
83+
if (s[i] <= s[d[i + 1]]) {
84+
d[i] = d[i + 1];
7485
}
75-
if (chars[i] < chars[mx]) {
76-
char t = chars[i];
77-
chars[i] = chars[mx];
78-
chars[mx] = t;
86+
}
87+
for (int i = 0; i < n; ++i) {
88+
int j = d[i];
89+
if (s[i] < s[j]) {
90+
char t = s[i];
91+
s[i] = s[j];
92+
s[j] = t;
7993
break;
8094
}
8195
}
82-
return Integer.parseInt(String.valueOf(chars));
96+
return Integer.parseInt(String.valueOf(s));
8397
}
8498
}
8599
```
@@ -92,13 +106,17 @@ public:
92106
int maximumSwap(int num) {
93107
string s = to_string(num);
94108
int n = s.size();
95-
for (int i = 0; i < n - 1; ++i) {
96-
int mx = i + 1;
97-
for (int j = i + 1; j < n; ++j) {
98-
if (s[j] >= s[mx]) mx = j;
109+
vector<int> d(n);
110+
iota(d.begin(), d.end(), 0);
111+
for (int i = n - 2; ~i; --i) {
112+
if (s[i] <= s[d[i + 1]]) {
113+
d[i] = d[i + 1];
99114
}
100-
if (s[i] < s[mx]) {
101-
swap(s[i], s[mx]);
115+
}
116+
for (int i = 0; i < n; ++i) {
117+
int j = d[i];
118+
if (s[i] < s[j]) {
119+
swap(s[i], s[j]);
102120
break;
103121
}
104122
}
@@ -111,22 +129,24 @@ public:
111129
112130
```go
113131
func maximumSwap(num int) int {
114-
s := strconv.Itoa(num)
115-
chars := []byte(s)
116-
n := len(chars)
117-
for i := range chars[:n-1] {
118-
mx := i + 1
119-
for j := i + 1; j < n; j++ {
120-
if chars[j] >= chars[mx] {
121-
mx = j
122-
}
132+
s := []byte(strconv.Itoa(num))
133+
n := len(s)
134+
d := make([]int, n)
135+
for i := range d {
136+
d[i] = i
137+
}
138+
for i := n - 2; i >= 0; i-- {
139+
if s[i] <= s[d[i+1]] {
140+
d[i] = d[i+1]
123141
}
124-
if chars[i] < chars[mx] {
125-
chars[i], chars[mx] = chars[mx], chars[i]
142+
}
143+
for i, j := range d {
144+
if s[i] < s[j] {
145+
s[i], s[j] = s[j], s[i]
126146
break
127147
}
128148
}
129-
ans, _ := strconv.Atoi(string(chars))
149+
ans, _ := strconv.Atoi(string(s))
130150
return ans
131151
}
132152
```

solution/0600-0699/0670.Maximum Swap/README_EN.md

+51-41
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,45 @@
4141
```python
4242
class Solution:
4343
def maximumSwap(self, num: int) -> int:
44-
chars = list(str(num))
45-
n = len(chars)
46-
for i in range(n - 1):
47-
mx = i + 1
48-
for j in range(i + 1, n):
49-
if ord(chars[j]) >= ord(chars[mx]):
50-
mx = j
51-
if ord(chars[i]) < ord(chars[mx]):
52-
chars[i], chars[mx] = chars[mx], chars[i]
44+
s = list(str(num))
45+
n = len(s)
46+
d = list(range(n))
47+
for i in range(n - 2, -1, -1):
48+
if s[i] <= s[d[i + 1]]:
49+
d[i] = d[i + 1]
50+
for i, j in enumerate(d):
51+
if s[i] < s[j]:
52+
s[i], s[j] = s[j], s[i]
5353
break
54-
return int(''.join(chars))
54+
return int(''.join(s))
5555
```
5656

5757
### **Java**
5858

5959
```java
6060
class Solution {
6161
public int maximumSwap(int num) {
62-
char[] chars = String.valueOf(num).toCharArray();
63-
int n = chars.length;
64-
for (int i = 0; i < n - 1; ++i) {
65-
int mx = i + 1;
66-
for (int j = i + 1; j < n; ++j) {
67-
if (chars[j] >= chars[mx]) {
68-
mx = j;
69-
}
62+
char[] s = String.valueOf(num).toCharArray();
63+
int n = s.length;
64+
int[] d = new int[n];
65+
for (int i = 0; i < n; ++i) {
66+
d[i] = i;
67+
}
68+
for (int i = n - 2; i >= 0; --i) {
69+
if (s[i] <= s[d[i + 1]]) {
70+
d[i] = d[i + 1];
7071
}
71-
if (chars[i] < chars[mx]) {
72-
char t = chars[i];
73-
chars[i] = chars[mx];
74-
chars[mx] = t;
72+
}
73+
for (int i = 0; i < n; ++i) {
74+
int j = d[i];
75+
if (s[i] < s[j]) {
76+
char t = s[i];
77+
s[i] = s[j];
78+
s[j] = t;
7579
break;
7680
}
7781
}
78-
return Integer.parseInt(String.valueOf(chars));
82+
return Integer.parseInt(String.valueOf(s));
7983
}
8084
}
8185
```
@@ -88,13 +92,17 @@ public:
8892
int maximumSwap(int num) {
8993
string s = to_string(num);
9094
int n = s.size();
91-
for (int i = 0; i < n - 1; ++i) {
92-
int mx = i + 1;
93-
for (int j = i + 1; j < n; ++j) {
94-
if (s[j] >= s[mx]) mx = j;
95+
vector<int> d(n);
96+
iota(d.begin(), d.end(), 0);
97+
for (int i = n - 2; ~i; --i) {
98+
if (s[i] <= s[d[i + 1]]) {
99+
d[i] = d[i + 1];
95100
}
96-
if (s[i] < s[mx]) {
97-
swap(s[i], s[mx]);
101+
}
102+
for (int i = 0; i < n; ++i) {
103+
int j = d[i];
104+
if (s[i] < s[j]) {
105+
swap(s[i], s[j]);
98106
break;
99107
}
100108
}
@@ -107,22 +115,24 @@ public:
107115
108116
```go
109117
func maximumSwap(num int) int {
110-
s := strconv.Itoa(num)
111-
chars := []byte(s)
112-
n := len(chars)
113-
for i := range chars[:n-1] {
114-
mx := i + 1
115-
for j := i + 1; j < n; j++ {
116-
if chars[j] >= chars[mx] {
117-
mx = j
118-
}
118+
s := []byte(strconv.Itoa(num))
119+
n := len(s)
120+
d := make([]int, n)
121+
for i := range d {
122+
d[i] = i
123+
}
124+
for i := n - 2; i >= 0; i-- {
125+
if s[i] <= s[d[i+1]] {
126+
d[i] = d[i+1]
119127
}
120-
if chars[i] < chars[mx] {
121-
chars[i], chars[mx] = chars[mx], chars[i]
128+
}
129+
for i, j := range d {
130+
if s[i] < s[j] {
131+
s[i], s[j] = s[j], s[i]
122132
break
123133
}
124134
}
125-
ans, _ := strconv.Atoi(string(chars))
135+
ans, _ := strconv.Atoi(string(s))
126136
return ans
127137
}
128138
```

solution/0600-0699/0670.Maximum Swap/Solution.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ class Solution {
33
int maximumSwap(int num) {
44
string s = to_string(num);
55
int n = s.size();
6-
for (int i = 0; i < n - 1; ++i) {
7-
int mx = i + 1;
8-
for (int j = i + 1; j < n; ++j) {
9-
if (s[j] >= s[mx]) mx = j;
6+
vector<int> d(n);
7+
iota(d.begin(), d.end(), 0);
8+
for (int i = n - 2; ~i; --i) {
9+
if (s[i] <= s[d[i + 1]]) {
10+
d[i] = d[i + 1];
1011
}
11-
if (s[i] < s[mx]) {
12-
swap(s[i], s[mx]);
12+
}
13+
for (int i = 0; i < n; ++i) {
14+
int j = d[i];
15+
if (s[i] < s[j]) {
16+
swap(s[i], s[j]);
1317
break;
1418
}
1519
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
func maximumSwap(num int) int {
2-
s := strconv.Itoa(num)
3-
chars := []byte(s)
4-
n := len(chars)
5-
for i := range chars[:n-1] {
6-
mx := i + 1
7-
for j := i + 1; j < n; j++ {
8-
if chars[j] >= chars[mx] {
9-
mx = j
10-
}
2+
s := []byte(strconv.Itoa(num))
3+
n := len(s)
4+
d := make([]int, n)
5+
for i := range d {
6+
d[i] = i
7+
}
8+
for i := n - 2; i >= 0; i-- {
9+
if s[i] <= s[d[i+1]] {
10+
d[i] = d[i+1]
1111
}
12-
if chars[i] < chars[mx] {
13-
chars[i], chars[mx] = chars[mx], chars[i]
12+
}
13+
for i, j := range d {
14+
if s[i] < s[j] {
15+
s[i], s[j] = s[j], s[i]
1416
break
1517
}
1618
}
17-
ans, _ := strconv.Atoi(string(chars))
19+
ans, _ := strconv.Atoi(string(s))
1820
return ans
1921
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
class Solution {
22
public int maximumSwap(int num) {
3-
char[] chars = String.valueOf(num).toCharArray();
4-
int n = chars.length;
5-
for (int i = 0; i < n - 1; ++i) {
6-
int mx = i + 1;
7-
for (int j = i + 1; j < n; ++j) {
8-
if (chars[j] >= chars[mx]) {
9-
mx = j;
10-
}
3+
char[] s = String.valueOf(num).toCharArray();
4+
int n = s.length;
5+
int[] d = new int[n];
6+
for (int i = 0; i < n; ++i) {
7+
d[i] = i;
8+
}
9+
for (int i = n - 2; i >= 0; --i) {
10+
if (s[i] <= s[d[i + 1]]) {
11+
d[i] = d[i + 1];
1112
}
12-
if (chars[i] < chars[mx]) {
13-
char t = chars[i];
14-
chars[i] = chars[mx];
15-
chars[mx] = t;
13+
}
14+
for (int i = 0; i < n; ++i) {
15+
int j = d[i];
16+
if (s[i] < s[j]) {
17+
char t = s[i];
18+
s[i] = s[j];
19+
s[j] = t;
1620
break;
1721
}
1822
}
19-
return Integer.parseInt(String.valueOf(chars));
23+
return Integer.parseInt(String.valueOf(s));
2024
}
2125
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution:
22
def maximumSwap(self, num: int) -> int:
3-
chars = list(str(num))
4-
n = len(chars)
5-
for i in range(n - 1):
6-
mx = i + 1
7-
for j in range(i + 1, n):
8-
if ord(chars[j]) >= ord(chars[mx]):
9-
mx = j
10-
if ord(chars[i]) < ord(chars[mx]):
11-
chars[i], chars[mx] = chars[mx], chars[i]
3+
s = list(str(num))
4+
n = len(s)
5+
d = list(range(n))
6+
for i in range(n - 2, -1, -1):
7+
if s[i] <= s[d[i + 1]]:
8+
d[i] = d[i + 1]
9+
for i, j in enumerate(d):
10+
if s[i] < s[j]:
11+
s[i], s[j] = s[j], s[i]
1212
break
13-
return int(''.join(chars))
13+
return int(''.join(s))

0 commit comments

Comments
 (0)