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 ...
随机推荐
- IO Processing
Types of IO IRP Buffer Management 首先区分一下page的内存与nonpaged的内存,内存如果用页管理,就难免面对被swap out的命运:但是如果用nonpaged ...
- 设置 Quartus II 的仿真时间大于 1us
Quartus II 仿真的默认时长是 1us. 设置时钟时看到 End time 想修改时长,把默认的 1us 改成 10us. 然后提示 End time 不合法.(只能设置为 0 到 1us) ...
- Uva10817_Headmaster's Headache
大致题意就是: 一个学校招聘人,自带老师m个,n个求职的人,需要讲授s个课程,已经知道了每个人工资,问怎么才能让各科至少有两个老师(自带的必须要) 这题刚看的时候大概知道是要状态转移,可问题是,状态转 ...
- 20140904 atoi字符串转化为整数源码
1.atoi源码 #include<stdio.h> #include<assert.h> bool isdigit1(char c) { ') return true; el ...
- Gitlab CI持续集成 - GitLab Runner 安装与注册
GitLab Runner安装 需要添加gitlab官方库: # For Debian/Ubuntu/Mint curl -L https://packages.gitlab.com/install/ ...
- C# dynamic类型序列化和反序列化之Newtonsoft.Json
原文:C# dynamic类型序列化和反序列化之Newtonsoft.Json 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011127019/ ...
- python--函数基础一
一.函数意义:可读性,复用性强 # def my_len(s): #def 关键词开头,my_len函数名 # i=0 # for k in s: # i+=1 # print(i) # return ...
- 2018焦作网络赛-E- Jiu Yuan Wants to Eat
题目描述 You ye Jiu yuan is the daughter of the Great GOD Emancipator. And when she becomes an adult, s ...
- 命令行窗口编译执行java
1:首先配置java环境变量 新建系统环境变量 名称:JAVA_HOME 内容:D:\Program Files\Java\jdk1.7.0_13 为你jdk所在的文件夹位置 修改path路径 ...
- Mysql学习笔记(001)-常见命令
常见命令 SHOW DATABASES; /* use test; create database myemployees; use myemployees; create table employe ...