-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmaximumSwap.go
More file actions
56 lines (49 loc) · 1.13 KB
/
Copy pathmaximumSwap.go
File metadata and controls
56 lines (49 loc) · 1.13 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
44
45
46
47
48
49
50
51
52
53
54
55
56
/* https://leetcode.com/problems/maximum-swap/description/
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, 10^8]
*/
package lmath
import (
"bytes"
"strconv"
)
func maximumSwap(num int) int {
nums := []int{}
for num > 0 {
nums = append(nums, num%10)
num /= 10
}
for i, j := 0, len(nums)-1; i <= j; i, j = i+1, j-1 {
nums[i], nums[j] = nums[j], nums[i]
}
buckets := make([]int, 10, 10)
for i, num := range nums {
buckets[num] = i
}
nums2Int := func(nums []int) int {
var r bytes.Buffer
for _, num := range nums {
r.WriteRune(rune(num) + '0')
}
num, _ := strconv.Atoi(r.String())
return num
}
for i := 0; i < len(nums); i++ {
for j := 9; j > nums[i]; j-- {
if tmp := buckets[j]; tmp > i {
nums[i], nums[tmp] = nums[tmp], nums[i]
return nums2Int(nums)
}
}
}
return nums2Int(nums)
}