File tree Expand file tree Collapse file tree 3 files changed +79
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
23
23
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
24
24
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25
25
|671|[ Second Minimum Node In a Binary Tree] ( https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_671.java ) | O(n) | O(n) | Easy | Tree, DFS
26
+ |670|[ Maximum Swap] ( https://leetcode.com/problems/maximum-swap/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_670.java ) | O(n^2) | O(1) | Medium | String
26
27
|669|[ Trim a Binary Search Tree] ( https://leetcode.com/problems/trim-a-binary-search-tree/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_669.java ) | O(n) | O(1) | Easy | Tree, DFS
27
28
|668|[ Kth Smallest Number in Multiplication Table] ( https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_668.java ) | O(logm* n) | O(1) | Hard | Binary Search
28
29
|667|[ Beautiful Arrangement II] ( https://leetcode.com/problems/beautiful-arrangement-ii/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_667.java ) | O(n) | O(1) | Medium | Array
Original file line number Diff line number Diff line change
1
+ package com .fishercoder .solutions ;
2
+
3
+ /**
4
+ * 670. Maximum Swap
5
+ *
6
+ * Given a non-negative integer, you could swap two digits at most once to get the maximum valued number.
7
+ * Return the maximum valued number you could get.
8
+
9
+ Example 1:
10
+ Input: 2736
11
+ Output: 7236
12
+ Explanation: Swap the number 2 and the number 7.
13
+
14
+ Example 2:
15
+ Input: 9973
16
+ Output: 9973
17
+ Explanation: No swap.
18
+
19
+ Note:
20
+ The given number is in the range [0, 108]
21
+
22
+ */
23
+ public class _670 {
24
+ public static class Solution1 {
25
+ public int maximumSwap (int num ) {
26
+ String numStr = String .valueOf (num );
27
+ int max = num ;
28
+ for (int i = 0 ; i < numStr .length ()-1 ; i ++) {
29
+ for (int j = i +1 ; j < numStr .length (); j ++) {
30
+ if (numStr .charAt (i ) < numStr .charAt (j )) {
31
+ StringBuilder sb = new StringBuilder (numStr );
32
+ sb .replace (i , i +1 , String .valueOf (numStr .charAt (j )));
33
+ sb .replace (j , j +1 , String .valueOf (numStr .charAt (i )));
34
+ max = Math .max (max , Integer .parseInt (sb .toString ()));
35
+ }
36
+ }
37
+ }
38
+ return max ;
39
+ }
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ package com .fishercoder ;
2
+
3
+ import com .fishercoder .solutions ._670 ;
4
+ import org .junit .BeforeClass ;
5
+ import org .junit .Test ;
6
+
7
+ import static org .junit .Assert .assertEquals ;
8
+
9
+ public class _670Test {
10
+ private static _670 .Solution1 solution1 ;
11
+
12
+ @ BeforeClass
13
+ public static void setup () {
14
+ solution1 = new _670 .Solution1 ();
15
+ }
16
+
17
+ @ Test
18
+ public void test1 () {
19
+ assertEquals (7236 , solution1 .maximumSwap (2736 ));
20
+ }
21
+
22
+ @ Test
23
+ public void test2 () {
24
+ assertEquals (9973 , solution1 .maximumSwap (9973 ));
25
+ }
26
+
27
+ @ Test
28
+ public void test3 () {
29
+ assertEquals (73236 , solution1 .maximumSwap (23736 ));
30
+ }
31
+
32
+ @ Test
33
+ public void test4 () {
34
+ assertEquals (98213 , solution1 .maximumSwap (91283 ));
35
+ }
36
+
37
+ }
You can’t perform that action at this time.
0 commit comments