当前位置:网站首页>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
边栏推荐
- Coordinate layer conversion tool (video)
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- 平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
- 189. Rotation array
- 毕业总结
- Summary and Reflection on the third week of winter vacation
- Raft 日志复制
- Graduation summary
- Golang string (string) and byte array ([]byte) are converted to each other
- 198. Looting - Dynamic Planning
猜你喜欢
2022-2028 global sepsis treatment drug industry research and trend analysis report
Multifunctional web file manager filestash
Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
Raft 日志复制
Unity webgl optimization
[Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University
Install apache+php+mysql+phpmyadmin xampp and its error resolution
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
Recommend a simple browser tab
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
随机推荐
Niuke monthly race 31 minus integer
Win 11 major updates, new features love love.
Su embedded training - Day10
This diversion
多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
Software development freelancer's Road
Class exercises
shell 脚本中关于用户输入参数的处理
[combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
简述服务量化分析体系
2022-2028 global solid phase extraction column industry research and trend analysis report
Database creation, addition, deletion, modification and query
Sepconv (separable revolution) code recurrence
Coordinate layer conversion tool (video)
What is SQL get connection
[Yu Yue education] world reference materials of Microbiology in Shanghai Jiaotong University
Naoqi robot summary 27
[combinatorics] generating function (positive integer splitting | unordered non repeated splitting example)
4. Load balancing and dynamic static separation