当前位置:网站首页>leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
2022-07-03 18:38:00 【白速龙王的回眸】
分析
如果已经最大就返回-1
否则,肯定有一个前面的数比后面的要小的,我们交换它
然后我们要让前面的数尽量靠后即可
交换完之后,我们要对这个数后面的数进行重排(保证最小升序)
还要注意要在32位整数范围内,否则-1
ac code
class Solution:
def nextGreaterElement(self, n: int) -> int:
flag = False
s = str(n)
if len(s) == 1:
return -1
for i in range(1, len(s)):
if s[i] > s[i - 1]:
flag = True
if not flag:
return -1
lst = list(s)
idx = 0
for i in range(len(lst)):
for j in range(i):
if lst[len(s) - 1 - j] > lst[len(s) - 1 - i]:
lst[len(s) - 1 - j], lst[len(s) - 1 - i] = lst[len(s) - 1 - i], lst[len(s) - 1 - j]
flag = False
idx = len(s) - 1 - i
break
if not flag:
break
# idx后面的重排
temp = lst[idx + 1:]
temp.sort()
lst[idx + 1:] = temp[:]
ans = int(''.join(lst))
return ans if ans <= 2 ** 31 - 1 else -1
总结
下一个排列
找到交互变大最小的两个数(头一个尽量靠后,后一个也尽量靠后即可)
然后对头一个后面的进行升序重拍即可
边栏推荐
- Sepconv (separable revolution) code recurrence
- Naoqi robot summary 27
- 199. Right view of binary tree - breadth search
- Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
- 2022-2028 global petroleum pipe joint industry research and trend analysis report
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- Golang string (string) and byte array ([]byte) are converted to each other
- [combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
- JS_ Array_ sort
- [combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation example 2 | extended to integer solution)
猜你喜欢
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
12、 Service management
Install apache+php+mysql+phpmyadmin xampp and its error resolution
VLAN experiment
An academic paper sharing and approval system based on PHP for computer graduation design
2022-2028 global scar care product industry research and trend analysis report
Computer graduation design PHP campus address book telephone number inquiry system
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
2022-2028 global marking ink industry research and trend analysis report
4. Load balancing and dynamic static separation
随机推荐
SQL injection -day16
Redis cache avalanche, penetration, breakdown
Unity webgl optimization
编程中常见的 Foo 是什么意思?
199. Right view of binary tree - breadth search
What is the function of registering DLLs- What does registering a DLL do?
Unity2018 to wechat games without pictures
组策略中开机脚本与登录脚本所使用的用户身份
Shell script return value with which output
189. Rotation array
Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
[Godot] add menu button
FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试
What does foo mean in programming?
What problems can cross-border e-commerce sellers solve with multi platform ERP management system
Prototype inheritance..
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
[combinatorics] generating function (positive integer splitting | unordered non repeated splitting example)
Torch learning notes (6) -- logistic regression model (self training)