-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSwap.java
More file actions
43 lines (41 loc) · 1.21 KB
/
MaximumSwap.java
File metadata and controls
43 lines (41 loc) · 1.21 KB
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
36
37
38
39
40
41
42
43
package medium;
/**
* ClassName: MaximumSwap.java
* Author: chenyiAlone
* Create Time: 2019/11/4 16:21
* Description: No.670 Maximum Swap
* 思路:
*
* Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.
*
* Example 1:
* Input: 2736
* Output: 7236
* Explanation: Swap the number 2 and the number 7.
* Example 2:
* Input: 9973
* Output: 9973
* Explanation: No swap.
* Note:
* The given number is in the range [0, 108]
*
*/
public class MaximumSwap {
public int maximumSwap(int num) {
StringBuilder s = new StringBuilder(Integer.toString(num));
if (s.length() > 0) {
int max = s.length() - 1, rmi = s.length() - 1, swi = s.length() - 1;
for (int i = s.length() - 2; i >= 0; i--) {
if (s.charAt(i) < s.charAt(max)) {
rmi = max;
swi = i;
} else if (s.charAt(i) > s.charAt(max))
max = i;
}
char t = s.charAt(rmi);
s.setCharAt(rmi, s.charAt(swi));
s.setCharAt(swi, t);
}
return Integer.parseInt(s.toString());
}
}