-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
63 lines (58 loc) · 1.18 KB
/
main.go
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
57
58
59
60
61
62
63
package main
/*
Create a function that takes a positive integer and returns the next bigger number that can be formed by rearranging
its digits. For example:
12 ==> 21
513 ==> 531
2017 ==> 2071
nextBigger(num: 12) // returns 21
nextBigger(num: 513) // returns 531
nextBigger(num: 2017) // returns 2071
If the digits can't be rearranged to form a bigger number, return -1 (or nil in Swift):
9 ==> -1
111 ==> -1
531 ==> -1
nextBigger(num: 9) // returns nil
nextBigger(num: 111) // returns nil
nextBigger(num: 531) // returns nil
*/
import (
"sort"
"strconv"
"strings"
)
func NextBigger(number int) int {
str := strconv.Itoa(number)
if len(str) < 2 {
return -1
}
pos := len(str) - 2
flag := false
for pos >= 0 {
if str[pos] < str[pos+1] {
flag = true
break
}
pos--
}
if pos == -1 && !flag {
return -1
}
head := str[0:pos]
tail := str[pos:]
chars := strings.Split(tail, "")
sort.Strings(chars)
tail = strings.Join(chars, "")
var sign uint8
for idx, _ := range tail {
if tail[idx] > str[pos] {
sign = tail[idx]
head += string(sign)
break
}
}
tail = strings.Replace(tail, string(sign), "", 1)
head += tail
result, _ := strconv.Atoi(head)
return result
}