intnextGreaterElement(int n){ char nums[32]; sprintf(nums, "%d", n); int i = (int) strlen(nums) - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; } if (i < 0) { return-1; }
int j = strlen(nums) - 1; while (j >= 0 && nums[i] >= nums[j]) { j--; } int num = nums[i]; nums[i] = nums[j]; nums[j] = num; int left = i + 1; int right = strlen(nums) - 1; while (left < right) { int num = nums[left]; nums[left] = nums[right]; nums[right] = num; left++; right--; } long ans = atol(nums); return ans > INT_MAX ? -1 : ans; }
classSolution: defnextGreaterElement(self, n: int) -> int: x, cnt = n, 1 while x >= 10and x // 10 % 10 >= x % 10: cnt += 1 x //= 10 x //= 10 if x == 0: return -1
targetDigit = x % 10 x2, cnt2 = n, 0 while x2 % 10 <= targetDigit: cnt2 += 1 x2 //= 10 x += x2 % 10 - targetDigit # 把 x2 % 10 换到 targetDigit 上
MAX_INT = 2 ** 31 - 1 for i inrange(cnt): # 反转 n 末尾的 cnt 个数字拼到 x 后 d = n % 10if i != cnt2 else targetDigit # 为了演示算法,请读者把 x 视作一个 32 位有符号整数 if x > MAX_INT // 10or x == MAX_INT // 10and d > 7: return -1 x = x * 10 + d n //= 10 return x
classSolution { public: intnextGreaterElement(int n){ int x = n, cnt = 1; for (; x >= 10 && x / 10 % 10 >= x % 10; x /= 10) { ++cnt; } x /= 10; if (x == 0) { return-1; }
int targetDigit = x % 10; int x2 = n, cnt2 = 0; for (; x2 % 10 <= targetDigit; x2 /= 10) { ++cnt2; } x += x2 % 10 - targetDigit; // 把 x2 % 10 换到 targetDigit 上
for (int i = 0; i < cnt; ++i, n /= 10) { // 反转 n 末尾的 cnt 个数字拼到 x 后 int d = i != cnt2 ? n % 10 : targetDigit; if (x > INT_MAX / 10 || x == INT_MAX / 10 && d > 7) { return-1; } x = x * 10 + d; } return x; } };
classSolution { publicintnextGreaterElement(int n) { intx= n, cnt = 1; for (; x >= 10 && x / 10 % 10 >= x % 10; x /= 10) { ++cnt; } x /= 10; if (x == 0) { return -1; }
inttargetDigit= x % 10; intx2= n, cnt2 = 0; for (; x2 % 10 <= targetDigit; x2 /= 10) { ++cnt2; } x += x2 % 10 - targetDigit; // 把 x2 % 10 换到 targetDigit 上
for (inti=0; i < cnt; ++i, n /= 10) { // 反转 n 末尾的 cnt 个数字拼到 x 后 intd= i != cnt2 ? n % 10 : targetDigit; if (x > Integer.MAX_VALUE / 10 || x == Integer.MAX_VALUE / 10 && d > 7) { return -1; } x = x * 10 + d; } return x; } }
publicclassSolution { publicintNextGreaterElement(int n) { int x = n, cnt = 1; for (; x >= 10 && x / 10 % 10 >= x % 10; x /= 10) { ++cnt; } x /= 10; if (x == 0) { return-1; }
int targetDigit = x % 10; int x2 = n, cnt2 = 0; for (; x2 % 10 <= targetDigit; x2 /= 10) { ++cnt2; } x += x2 % 10 - targetDigit; // 把 x2 % 10 换到 targetDigit 上
for (int i = 0; i < cnt; ++i, n /= 10) { // 反转 n 末尾的 cnt 个数字拼到 x 后 int d = i != cnt2 ? n % 10 : targetDigit; if (x > int.MaxValue / 10 || x == int.MaxValue / 10 && d > 7) { return-1; } x = x * 10 + d; } return x; } }
funcnextGreaterElement(n int)int { x, cnt := n, 1 for ; x >= 10 && x/10%10 >= x%10; x /= 10 { cnt++ } x /= 10 if x == 0 { return-1 }
targetDigit := x % 10 x2, cnt2 := n, 0 for ; x2%10 <= targetDigit; x2 /= 10 { cnt2++ } x += x2%10 - targetDigit // 把 x2%10 换到 targetDigit 上
for i := 0; i < cnt; i++ { // 反转 n 末尾的 cnt 个数字拼到 x 后 d := targetDigit if i != cnt2 { d = n % 10 } if x > math.MaxInt32/10 || x == math.MaxInt32/10 && d > 7 { return-1 } x = x*10 + d n /= 10 } return x }
intnextGreaterElement(int n){ int x = n, cnt = 1; for (; x >= 10 && x / 10 % 10 >= x % 10; x /= 10) { ++cnt; } x /= 10; if (x == 0) { return-1; }
int targetDigit = x % 10; int x2 = n, cnt2 = 0; for (; x2 % 10 <= targetDigit; x2 /= 10) { ++cnt2; } x += x2 % 10 - targetDigit; // 把 x2 % 10 换到 targetDigit 上
for (int i = 0; i < cnt; ++i, n /= 10) { // 反转 n 末尾的 cnt 个数字拼到 x 后 int d = i != cnt2 ? n % 10 : targetDigit; if (x > INT_MAX / 10 || x == INT_MAX / 10 && d > 7) { return-1; } x = x * 10 + d; } return x; }