-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path670. Maximum Swap.java
35 lines (35 loc) · 1.06 KB
/
670. Maximum Swap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public int maximumSwap(int num) {
StringBuilder answer = new StringBuilder("" + num);
int i = 0;
int j = answer.length() - 1;
int max_ = -1;
int id_ = -1;
int id__ = 0;
while (j > i) {
int digit = answer.charAt(j) - '0';
int self = answer.charAt(i) - '0';
if (digit > self && digit > max_) {
max_ = digit;
id_ = j;
id__ = i;
}
j--;
if (j == i && id_ == -1) {
i++;
j = answer.length() - 1;
}
}
if (id_ != -1) {
System.out.println(id__ + " " + id_);
char t = answer.charAt(id__);
answer.setCharAt(id__, answer.charAt(id_));
answer.setCharAt(id_, t);
}
return Integer.parseInt(answer.toString());
}
}