impl Solution { fnnext_permutation(nums: Vec<i32>) -> Option<Vec<i32>> { letmut nums = nums; letmut i = nums.len() - 1; while i > 0 { i -= 1; if nums[i] < nums[i + 1] { letmut j = nums.len() - 1; while nums[i] >= nums[j] { j -= 1; } nums.swap(i, j);
letmut low = i + 1; letmut high = nums.len() - 1;
while low < high { nums.swap(low, high); low += 1; high -= 1; } returnSome(nums); } } None } pubfnnext_greater_element(n: i32) -> i32 { letmut n = n; letmut digits = vec![]; while n != 0 { digits.push(n % 10);
n = n / 10; } digits.reverse(); ifletSome(greater) = Self::next_permutation(digits) { letmut res = 0_i32; for n in greater { let (shift_res, is_overflow) = res.overflowing_mul(10); if is_overflow { return -1; } let (new_res, is_overflow) = shift_res.overflowing_add(n); if is_overflow { return -1; } res = new_res; } return res } -1 } }