Movatterモバイル変換


[0]ホーム

URL:


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

Commit89fa7ce

Browse files
add 670
1 parent9dfc62c commit89fa7ce

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2525
|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
2627
|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
2728
|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
2829
|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
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.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+
publicclass_670 {
24+
publicstaticclassSolution1 {
25+
publicintmaximumSwap(intnum) {
26+
StringnumStr =String.valueOf(num);
27+
intmax =num;
28+
for (inti =0;i <numStr.length()-1;i++) {
29+
for (intj =i+1;j <numStr.length();j++) {
30+
if (numStr.charAt(i) <numStr.charAt(j)) {
31+
StringBuildersb =newStringBuilder(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+
returnmax;
39+
}
40+
}
41+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._670;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_670Test {
10+
privatestatic_670.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_670.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(7236,solution1.maximumSwap(2736));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(9973,solution1.maximumSwap(9973));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(73236,solution1.maximumSwap(23736));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(98213,solution1.maximumSwap(91283));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp