题目:
题解:
func nextGreaterElement(n int) int {x, cnt := n, 1for ; x >= 10 && x/10%10 >= x%10; x /= 10 {cnt++}x /= 10if x == 0 {return -1}targetDigit := x % 10x2, cnt2 := n, 0for ; x2%10 <= targetDigit; x2 /= 10 {cnt2++}x += x2%10 - targetDigit // 把 x2%10 换到 targetDigit 上for i := 0; i < cnt; i++ { // 反转 n 末尾的 cnt 个数字拼到 x 后d := targetDigitif i != cnt2 {d = n % 10}if x > math.MaxInt32/10 || x == math.MaxInt32/10 && d > 7 {return -1}x = x*10 + dn /= 10}return x
}