当前位置:网站首页>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
总结
下一个排列
找到交互变大最小的两个数(头一个尽量靠后,后一个也尽量靠后即可)
然后对头一个后面的进行升序重拍即可
边栏推荐
- 2022-2028 global scar care product industry research and trend analysis report
- [combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
- [combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
- [combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
- Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
- An academic paper sharing and approval system based on PHP for computer graduation design
- [enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
- JS_ Array_ sort
- FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试
- SQL injection -day16
猜你喜欢

English語法_名詞 - 分類

FBI warning: some people use AI to disguise themselves as others for remote interview

Have you learned the correct expression posture of programmers on Valentine's day?

Computer graduation project PHP library book borrowing management system
![[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

Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come

2022-2028 global solid phase extraction column industry research and trend analysis report

VLAN experiment

Valentine's day, send you a little red flower~

What London Silver Trading software supports multiple languages
随机推荐
English语法_形容词/副词3级 - 倍数表达
2022-2028 global petroleum pipe joint industry research and trend analysis report
组策略中开机脚本与登录脚本所使用的用户身份
SSH 远程执行命令简介
Class exercises
webcodecs
php-fpm的max_chindren的一些误区
Computer graduation design PHP campus address book telephone number inquiry system
Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)
[combinatorics] generating function (example of using generating function to solve the number of solutions of indefinite equation)
Typescript configuration
English語法_名詞 - 分類
图像24位深度转8位深度
[combinatorics] generating function (positive integer splitting | unordered non repeated splitting example)
G1 garbage collector of garbage collector
Setinterval CPU intensive- Is setInterval CPU intensive?
How to read the source code [debug and observe the source code]
Torch learning notes (5) -- autograd
[Godot] add menu button
Ping problem between virtual machine and development board