LeetCode题解(0556):计算调换整数的位使整数值大于当前值的最小值(Python)
发布日期:2021-06-29 19:57:59
浏览次数:3
分类:技术文章
本文共 752 字,大约阅读时间需要 2 分钟。
题目:(中等)
标签:字符串
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( N ) O(N) O(N) | O ( N ) O(N) O(N) | 40ms (64.23%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。
解法一(排序法):
class Solution: def nextGreaterElement(self, n: int) -> int: lst = [int(ch) for ch in str(n)] now = [] for i in range(len(lst) - 1, -1, -1): n = lst[i] if not now or now[-1] <= n: bisect.insort_left(now, n) else: this = now.pop(bisect.bisect_right(now, n)) bisect.insort_left(now, n) ans = lst[:i] + [this] + now ans = int("".join([str(v) for v in ans])) return ans if ans < 2 ** 31 else -1 return -1
转载地址:https://dataartist.blog.csdn.net/article/details/108011527 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年05月02日 01时51分03秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
setInterval
2019-04-30
window.open关闭窗体后刷新
2019-04-30
easyui.dialog
2019-04-30
js保存指定html
2019-04-30
会改bug不值钱,预防bug才值钱
2019-04-30
confluence--团队协同与知识管理工具
2019-04-30
C#获取应用程序Uri
2019-04-30
C#使用EPPlus插件导出Excel
2019-04-30
IIS web site windows验证
2019-04-30
.Net Mvc PagedList
2019-04-30
vue--学习一
2019-04-30
C# string[] 转list<long>
2019-04-30
sql server解析xml字段
2019-04-30
vue学习一
2019-04-30
基于Jenkins的.Net Core应用自动部署--学习一
2019-04-30
bootstrap-vue
2019-04-30
C# WebUtils
2019-04-30
ETL
2019-04-30
CR(变更请求)
2019-04-30