当前位置: 首页> 文旅> 艺术 > 毕业设计ppt答辩模板_电商设计工资多少钱一个月_网络推广seo公司_seo北京优化

毕业设计ppt答辩模板_电商设计工资多少钱一个月_网络推广seo公司_seo北京优化

时间:2025/7/14 17:12:00来源:https://blog.csdn.net/sinat_41679123/article/details/144288112 浏览次数:0次
毕业设计ppt答辩模板_电商设计工资多少钱一个月_网络推广seo公司_seo北京优化

Description

Given a positive integer n, find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive integer exists, return -1.

Note that the returned integer should fit in 32-bit integer, if there is a valid answer but it does not fit in 32-bit integer, return -1.

Example 1:

Input: n = 12
Output: 21

Example 2:

Input: n = 21
Output: -1

Constraints:

1 <= n <= 2^31 - 1

Solution

Solved after help…

Same as 31. 下一个排列

The next greater number should be: start from the rightmost, and until we find a digit that is smaller than at least one of the digits we have visited, we should swap the digit with the minimum but larger digit of all the digits we have visited. Then sort all the digits at the right. Then we would get our answer.

Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn)
Space complexity: o ( n ) o(n) o(n)

Code

class Solution:def nextGreaterElement(self, n: int) -> int:num_list = []while n > 0:num_list.append(n % 10)n //= 10stack = []for i in range(len(num_list)):if not stack or num_list[stack[-1]] <= num_list[i]:stack.append(i)else:swap_index = stack[-1]# swap current number with previous minimum larger onewhile stack and num_list[stack[-1]] > num_list[i]:swap_index = stack.pop()num_list[i], num_list[swap_index] = num_list[swap_index], num_list[i]num_list[:i] = sorted(num_list[:i], reverse=True)i = len(num_list) + 1breakif i == len(num_list) + 1:res = eval(''.join(map(str, num_list[::-1])))if res > 2 ** 31 - 1:res = -1else:res = -1return res
关键字:毕业设计ppt答辩模板_电商设计工资多少钱一个月_网络推广seo公司_seo北京优化

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: