当前位置:网站首页>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
总结
下一个排列
找到交互变大最小的两个数(头一个尽量靠后,后一个也尽量靠后即可)
然后对头一个后面的进行升序重拍即可
边栏推荐
- [combinatorics] exponential generating function (properties of exponential generating function | exponential generating function solving multiple set arrangement)
- Prototype inheritance..
- Sensor debugging process
- [enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
- Graduation summary
- 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
- How to draw non overlapping bubble chart in MATLAB
- Computer graduation design PHP campus address book telephone number inquiry system
- How to expand the capacity of golang slice slice
- [combinatorics] generating function (positive integer splitting | unordered non repeated splitting example)
猜你喜欢

Computer graduation design PHP sports goods online sales system website

CV in transformer learning notes (continuously updated)

English語法_名詞 - 分類

Mysql45 lecture learning notes (II)

Transformer T5 model read slowly

How to draw non overlapping bubble chart in MATLAB
![[Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University](/img/52/b97c618a8f2eb29ad0ccca221bb5c1.jpg)
[Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University

Theoretical description of linear equations and summary of methods for solving linear equations by eigen

Redis cache avalanche, penetration, breakdown

多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
随机推荐
Computer graduation project PHP library book borrowing management system
English grammar_ Adjective / adverb Level 3 - multiple expression
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
Computer graduation design PHP makeup sales Beauty shopping mall
Bidding procurement scheme management of Oracle project management system
What London Silver Trading software supports multiple languages
[combinatorics] exponential generating function (concept of exponential generating function | permutation number exponential generating function = combinatorial number ordinary generating function | e
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
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
[Yu Yue education] world reference materials of Microbiology in Shanghai Jiaotong University
统计图像中各像素值的数量
FBI warning: some people use AI to disguise themselves as others for remote interview
After nohup NPM start &, close the shell window directly, and the process closes accordingly
Torch learning notes (5) -- autograd
[combinatorics] exponential generating function (example of exponential generating function solving multiple set arrangement)
2022-2028 global scar care product industry research and trend analysis report
How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
企业级自定义表单引擎解决方案(十二)--表单规则引擎2