34
34
35
35
<!-- 这里可写通用的实现逻辑 -->
36
36
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
+
37
47
<!-- tabs:start -->
38
48
39
49
### ** Python3**
43
53
``` python
44
54
class Solution :
45
55
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]
55
65
break
56
- return int (' ' .join(chars ))
66
+ return int (' ' .join(s ))
57
67
```
58
68
59
69
### ** Java**
@@ -63,23 +73,27 @@ class Solution:
63
73
``` java
64
74
class Solution {
65
75
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 ];
74
85
}
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;
79
93
break ;
80
94
}
81
95
}
82
- return Integer . parseInt(String . valueOf(chars ));
96
+ return Integer . parseInt(String . valueOf(s ));
83
97
}
84
98
}
85
99
```
@@ -92,13 +106,17 @@ public:
92
106
int maximumSwap(int num) {
93
107
string s = to_string(num);
94
108
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] ;
99
114
}
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] );
102
120
break;
103
121
}
104
122
}
@@ -111,22 +129,24 @@ public:
111
129
112
130
```go
113
131
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]
123
141
}
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]
126
146
break
127
147
}
128
148
}
129
- ans, _ := strconv.Atoi(string(chars ))
149
+ ans, _ := strconv.Atoi(string(s ))
130
150
return ans
131
151
}
132
152
```
0 commit comments