Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0abd667

Browse files
committed
feat: add soluitons to lc problem: No.0670
No.0670.Maximum Swap
1 parent 0a673d7 commit 0abd667

File tree

6 files changed

+240
-22
lines changed

6 files changed

+240
-22
lines changed

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,97 @@
4141
<!-- 这里可写当前语言的特殊实现逻辑 -->
4242

4343
```python
44-
44+
class Solution:
45+
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]
55+
break
56+
return int(''.join(chars))
4557
```
4658

4759
### **Java**
4860

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

5163
```java
64+
class Solution {
65+
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+
}
74+
}
75+
if (chars[i] < chars[mx]) {
76+
char t = chars[i];
77+
chars[i] = chars[mx];
78+
chars[mx] = t;
79+
break;
80+
}
81+
}
82+
return Integer.parseInt(String.valueOf(chars));
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int maximumSwap(int num) {
93+
string s = to_string(num);
94+
int n = s.size();
95+
for (int i = 0; i < n - 1; ++i)
96+
{
97+
int mx = i + 1;
98+
for (int j = i + 1; j < n; ++j)
99+
{
100+
if (s[j] >= s[mx]) mx = j;
101+
}
102+
if (s[i] < s[mx])
103+
{
104+
swap(s[i], s[mx]);
105+
break;
106+
}
107+
}
108+
return stoi(s);
109+
}
110+
};
111+
```
52112
113+
### **Go**
114+
115+
```go
116+
func maximumSwap(num int) int {
117+
s := strconv.Itoa(num)
118+
chars := []byte(s)
119+
n := len(chars)
120+
for i := range chars[:n-1] {
121+
mx := i + 1
122+
for j := i + 1; j < n; j++ {
123+
if chars[j] >= chars[mx] {
124+
mx = j
125+
}
126+
}
127+
if chars[i] < chars[mx] {
128+
chars[i], chars[mx] = chars[mx], chars[i]
129+
break
130+
}
131+
}
132+
ans, _ := strconv.Atoi(string(chars))
133+
return ans
134+
}
53135
```
54136

55137
### **...**

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,95 @@
3939
### **Python3**
4040

4141
```python
42-
42+
class Solution:
43+
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]
53+
break
54+
return int(''.join(chars))
4355
```
4456

4557
### **Java**
4658

4759
```java
60+
class Solution {
61+
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+
}
70+
}
71+
if (chars[i] < chars[mx]) {
72+
char t = chars[i];
73+
chars[i] = chars[mx];
74+
chars[mx] = t;
75+
break;
76+
}
77+
}
78+
return Integer.parseInt(String.valueOf(chars));
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int maximumSwap(int num) {
89+
string s = to_string(num);
90+
int n = s.size();
91+
for (int i = 0; i < n - 1; ++i)
92+
{
93+
int mx = i + 1;
94+
for (int j = i + 1; j < n; ++j)
95+
{
96+
if (s[j] >= s[mx]) mx = j;
97+
}
98+
if (s[i] < s[mx])
99+
{
100+
swap(s[i], s[mx]);
101+
break;
102+
}
103+
}
104+
return stoi(s);
105+
}
106+
};
107+
```
48108
109+
### **Go**
110+
111+
```go
112+
func maximumSwap(num int) int {
113+
s := strconv.Itoa(num)
114+
chars := []byte(s)
115+
n := len(chars)
116+
for i := range chars[:n-1] {
117+
mx := i + 1
118+
for j := i + 1; j < n; j++ {
119+
if chars[j] >= chars[mx] {
120+
mx = j
121+
}
122+
}
123+
if chars[i] < chars[mx] {
124+
chars[i], chars[mx] = chars[mx], chars[i]
125+
break
126+
}
127+
}
128+
ans, _ := strconv.Atoi(string(chars))
129+
return ans
130+
}
49131
```
50132

51133
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maximumSwap(int num) {
4+
string s = to_string(num);
5+
int n = s.size();
6+
for (int i = 0; i < n - 1; ++i)
7+
{
8+
int mx = i + 1;
9+
for (int j = i + 1; j < n; ++j)
10+
{
11+
if (s[j] >= s[mx]) mx = j;
12+
}
13+
if (s[i] < s[mx])
14+
{
15+
swap(s[i], s[mx]);
16+
break;
17+
}
18+
}
19+
return stoi(s);
20+
}
21+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
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+
}
11+
}
12+
if chars[i] < chars[mx] {
13+
chars[i], chars[mx] = chars[mx], chars[i]
14+
break
15+
}
16+
}
17+
ans, _ := strconv.Atoi(string(chars))
18+
return ans
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
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+
}
11+
}
12+
if (chars[i] < chars[mx]) {
13+
char t = chars[i];
14+
chars[i] = chars[mx];
15+
chars[mx] = t;
16+
break;
17+
}
18+
}
19+
return Integer.parseInt(String.valueOf(chars));
20+
}
21+
}
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
class Solution:
2-
def maximumSwap(self, num):
3-
"""
4-
:type num: int
5-
:rtype: int
6-
"""
7-
# s为能得到的最大数
8-
s = ''.join(sorted(list(str(num)), reverse=True))
9-
nums = str(num)
10-
if s == nums:
11-
return num
12-
for i in range(len(s)):
13-
if s[i] != nums[i]:
14-
kai = i
15-
break
16-
for i in range(len(nums) - 1, -1, -1):
17-
if nums[i] == s[kai]:
18-
loc = i
19-
break
20-
return int(s[: kai + 1] + nums[kai + 1 : loc] + nums[kai] + nums[loc + 1 :])
1+
class Solution:
2+
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]
12+
break
13+
return int(''.join(chars))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /