当前位置:网站首页>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
总结
下一个排列
找到交互变大最小的两个数(头一个尽量靠后,后一个也尽量靠后即可)
然后对头一个后面的进行升序重拍即可
边栏推荐
- Install apache+php+mysql+phpmyadmin xampp and its error resolution
- How do microservices aggregate API documents? This wave of operation is too good
- 042. (2.11) do it when it's time to do it
- G1 garbage collector of garbage collector
- Torch learning notes (4) -- torch's dynamic calculation diagram
- How about the Moco model?
- Bidding procurement scheme management of Oracle project management system
- [combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
- Su embedded training - Day10
- Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
猜你喜欢

Transformer T5 model read slowly

G1 garbage collector of garbage collector

How to track the real-time trend of Bank of London

Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)

Analysis of the reasons why enterprises build their own software development teams to use software manpower outsourcing services at the same time

How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)

Computer graduation project PHP library book borrowing management system

FBI warning: some people use AI to disguise themselves as others for remote interview
![[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)](/img/e6/9880e708aed42dc82c94aea002020c.jpg)
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)

Sensor debugging process
随机推荐
Unity webgl optimization
The vscode code is automatically modified to a compliance code when it is formatted and saved
2022-2028 global marking ink industry research and trend analysis report
FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试
ES7 - Optimization of promise
Torch learning notes (6) -- logistic regression model (self training)
Bidding procurement scheme management of Oracle project management system
22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc
[combinatorics] exponential generating function (properties of exponential generating function | exponential generating function solving multiple set arrangement)
VLAN experiment
English語法_名詞 - 分類
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
Computer graduation design PHP makeup sales Beauty shopping mall
What kind of experience is it when the Institute earns 20000 yuan a month?
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
Niuke monthly race 31 minus integer
Torch learning notes (4) -- torch's dynamic calculation diagram
Zero length array
Reading a line from ifstream into a string variable
042. (2.11) do it when it's time to do it