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 ...
随机推荐
- 前端学习笔记——HTML
1.前端的三把利器 HTML:我们把它比喻成赤裸的一个人 CSS:华丽的衣服 JS/JavaScript:赋予这个人的行为,也就是动起来 2.什么是HTML? HTML 是用来描述网页的一种语言.ht ...
- Django框架(二十一)—— Django rest_framework-权限组件
目录 Django rest_framework-权限组件 一.权限组件的使用 1.使用语法 2.全局使用.局部使用.局部禁用权限 二.源码分析 1.Book中没有as_view 2.APIView的 ...
- 常用Javascript方法
一,检测是否是Array 1,通过constructor检测 function isArray(value){ return value && typeof value === 'ob ...
- CentOS6.5下面OpenSSH低版本升级至7.3
升级前版本: openssl-1.0.1e-48.el6_8.1.x86_64 openssh-5.3p1-118.1.el6_8.x86_64 升级后版本: OpenSSL 1.0.2j OpenS ...
- ionic ios上状态栏和app重叠解决方案
干货文章 ·2018-03-22 01:33:01 官方issues: https://github.com/ionic-team/ionic/issues/13294 解决办法: 1.在 confi ...
- Kali Linux 开启SSH
Kali Linux安装好默认是不开启ssh的,需要手工开启 1.修改/etc/ssh/sshd_config 配置文件: root@mykali:~# vi /etc/ssh/sshd_confi ...
- MYSQL增量备份与恢复
vim /etc/my.cnf在[mysqld]下添加max_binlog_size = 1024000 //二进制日志最大1M 要进行mysql的增量备份,首先要开启二进制日志功能方法一:在/etc ...
- DOM0级事件绑定之js的onclick事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js 连等操作,,
奥术大师 var hu = { a : , c : , name : }; (function (){ var ccc = bbb = aaa = hu; })() console.log(bbb)* ...
- express 使用art-template模板引擎
下载express-art-template art-template - app.js中配置 - 注册一个模板引擎 - `app.engine('.html',express-art-templat ...