bigger is greater
题目:
Lexicographical order is often known as alphabetical order when dealing with strings. A string is greater than another string if it comes later in a lexicographically sorted list.Given a word, create a new word by swapping some or all of its characters. This new word must meet two criteria: (1)It must be greater than the original word. (2)It must be the smallest word that meets the first condition.Complete the function biggerIsGreater below to create and return the new string meeting the criteria. If it is not possible, return no answer.
附上链接:Bigger is Greater
初步逻辑
这个逻辑很难用语言描述,直接把我的实现代码贴上来
def biggerIsGreater(s):
i = 1
j = i + 1
while j > len(w) or w[-j] >= w[-i]:
if j > len(w):
i += 1
j = i + 1
if j > len(w):
return 'no answer'
else:
j += 1
else:
l = list(s)
l.insert(-j, s[-i])
l.pop(-i)
s = ''.join(l)
s = ''.join((lambda x, y: x + y)(list(s[-j + 1:]), list(s[:-j + 1])))
return s
逻辑本身没有问题,但是运行下去会发现,有三个例子是超时的
优质逻辑
发现了这个问题后,我却找不到解决办法,因为以往的超时都是由多重循环嵌套引起的,可这次明明只有一层循环,依然发生了超时的现象
于是,借助网站万能的Discussion,发现了其他人的一个很简洁的代码,如下
def biggerIsGreater(s):
for i in range(len(s) - 1)[::-1]:
if s[i] < s[i + 1]:
for j in range(i + 1, len(s))[::-1]:
if s[i] < s[j]:
lis = list(s)
lis[i], lis[j] = lis[j], lis[i]
return "".join(lis[:i + 1] + lis[i + 1:][::-1])
return 'no answer'
可它明明是两层嵌套循环,为什么要比我的运行的快呢。。。
初步感觉是因为我else里面的列表、字符串的操作占用了大量的时间,可是具体原因还是不清楚,有清楚的大佬愿意给我解释一下吗
bigger is greater的更多相关文章
- 556. Next Greater Element III下一个更大的数字
[抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...
- [LeetCode] 496. Next Greater Element I_Easy tag: Stack
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- 496. Next Greater Element I 另一个数组中对应的更大元素
[抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...
- [LeetCode] 556. Next Greater Element III 下一个较大的元素 III
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- spark - tasks is bigger than spark.driver.maxResultSize
Error ERROR TaskSetManager: Total size of serialized results of 8113 tasks (1131.0 MB) is bigger tha ...
- MYSQL 内存报错 Use 'mysqld --thread_stack=#' to specify a bigger stack.
今天在使用mysql的过程中,连接数据库始终无法成功 最后发现是数据库无法执行增加修改的操作 :错误代码 Thread stack overrun: 11552 bytes used of a 13 ...
- 数据库执行sql报错Got a packet bigger than 'max_allowed_packet' bytes及重启mysql
准备在mysql上使用数据库A,但mysql5经过重装后,上面的数据库已丢失,只得通过之前备份的A.sql重新生成数据库A. 1.执行sql报错 在执行A.sql的过程中,出现如下错误:Got a p ...
- HDU2929 Bigger is Better[DP 打印方案 !]
Bigger is Better Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDOJ 2929 Bigger is Better
DP....好难的DP... Bigger is Better Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- Device Drivers
Types of Device Drivers Windows可能会有User-mode的驱动,但是我们只关注Kernel-Mode的驱动. WDM Drivers WDM是一种驱动模型,是比较常用的 ...
- oracle知识博客链接
http://blog.csdn.net/YiQiJinBu/article/category/1100395/1
- c# 排序算法可视化
最近在 b 站上看了一个排序算法的动画,所以想自己写一个类似的项目. 项目使用 Graphics 在 winform 的窗体上绘图.(新建项目时选择控制台项目,注意添加引用:System.Drawin ...
- sync - 清空文件系统缓冲区
总览 (SYNOPSIS) sync [OPTION] 描述 (DESCRIPTION) 强迫把更改的块写入磁盘, 并更新超级块. --help 显示帮助然后终止. --version 显示版本信息然 ...
- 群晖修改启用root账号密码
DSM6.0以后,官方修改了系统的ROOT密码;需要修改才能启用并使用 软件准备 PUTTY点击下载 DSM中开启SSH 控制面板-终端机和SNMP-启动SSH 打开PUTTY 输入DSM IP地址 ...
- C# 进制转换(二进制、十六进制、十进制互转) 转载 https://www.cnblogs.com/icebutterfly/p/8884023.html
C# 进制转换(二进制.十六进制.十进制互转)由于二进制数在C#中无法直接表示,所以所有二进制数都用一个字符串来表示例如: 二进制: 1010 表示为 字符串:"1010" int ...
- mysql莫名报"unknown column ... in 'on clause'"
今天遇见个会诡异的问题 一个web程序本地调试的好好的,结果发布到服务器上程序就报错了,报"unknown column ... in 'on clause'",网上搜了下,说是m ...
- ES6 数组扩展(总结)
1.扩展运算符 将一个数组转为用逗号分隔的参数序列 console.log(1, ...[2, 3, 4], 5) // 1 2 3 4 5 2.Array.from() 将两类对象转为真正的数组 类 ...
- Qt 【遍历文件夹文件,为listwidgetItem设置图标】
效果图如上,通过qt本身的api去获取文件夹中文件的信息并且将其显示到listwidget上.只遍历当前文件夹的文件, 若文件夹中的子文件夹含有符合筛选器的文件那么并不会一同筛选出来 参考百度:htt ...
- tengine日志切割-配置分钟级别日志自动切割
tengine日志切割-配置分钟级别日志自动切割 安装 以安装最新版本的tengine-2.1.2版本 下载连接 tengine支持许多变量 变量 $conn_requests 当前请求在长连接上的序 ...