当前位置:网站首页>leetcode:556. Next larger element III [simulation + change as little as possible]
leetcode:556. Next larger element III [simulation + change as little as possible]
2022-07-03 18:39:00 【White speed Dragon King's review】
analysis
If it has reached the maximum, return -1
otherwise , There must be a number in front that is smaller than that in the back , We exchange it
Then we need to make the front number as far back as possible
After the exchange , We need to rearrange the numbers after this number ( Ensure the minimum ascending order )
Also pay attention to 32 Bit integer range , otherwise -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 Rearranging behind
temp = lst[idx + 1:]
temp.sort()
lst[idx + 1:] = temp[:]
ans = int(''.join(lst))
return ans if ans <= 2 ** 31 - 1 else -1
summary
Next spread
Find the two minimum numbers that make the interaction larger ( The first one should try to stay back , The latter one can also be as far back as possible )
Then you can remake the first one in ascending order
边栏推荐
- 4. Load balancing and dynamic static separation
- Multifunctional web file manager filestash
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- Use of unsafe class
- Summary and Reflection on the third week of winter vacation
- Real time split network (continuous update)
- Setinterval CPU intensive- Is setInterval CPU intensive?
- How about the Moco model?
- Computer graduation project PHP library book borrowing management system
- [leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
猜你喜欢
How many convolution methods does deep learning have? (including drawings)
Raft 日志复制
How to draw non overlapping bubble chart in MATLAB
Read the paper glodyne global topology preserving dynamic network embedding
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
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
VLAN experiment
2022-2028 global marking ink industry research and trend analysis report
虚拟机和开发板互Ping问题
随机推荐
Typescript configuration
Reading a line from ifstream into a string variable
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
Redis cache avalanche, penetration, breakdown
Read the paper glodyne global topology preserving dynamic network embedding
How to analyze the rising and falling rules of London gold trend chart
189. Rotation array
企业级自定义表单引擎解决方案(十二)--表单规则引擎2
[combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
English grammar_ Noun classification
[untitled]
Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
SSH 远程执行命令简介
After nohup NPM start &, close the shell window directly, and the process closes accordingly
[combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
Summary and Reflection on the third week of winter vacation