题目:

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的更多相关文章

  1. 556. Next Greater Element III下一个更大的数字

    [抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...

  2. [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 ...

  3. 496. Next Greater Element I 另一个数组中对应的更大元素

    [抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...

  4. [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 ...

  5. 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 ...

  6. MYSQL 内存报错 Use 'mysqld --thread_stack=#' to specify a bigger stack.

    今天在使用mysql的过程中,连接数据库始终无法成功 最后发现是数据库无法执行增加修改的操作 :错误代码 Thread stack overrun:  11552 bytes used of a 13 ...

  7. 数据库执行sql报错Got a packet bigger than 'max_allowed_packet' bytes及重启mysql

    准备在mysql上使用数据库A,但mysql5经过重装后,上面的数据库已丢失,只得通过之前备份的A.sql重新生成数据库A. 1.执行sql报错 在执行A.sql的过程中,出现如下错误:Got a p ...

  8. HDU2929 Bigger is Better[DP 打印方案 !]

    Bigger is Better Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. HDOJ 2929 Bigger is Better

    DP....好难的DP... Bigger is Better Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. 转 Jmeter业务请求比例

    [转载]Jmeter业务请求比例1   ps:文章转自订阅号“测试那点事儿”,链接:https://mp.weixin.qq.com/s/qVD4iNO0QqRIwAIq9_E_Kw   方法二: 可 ...

  2. FrameWork内核解析之Handler消息机制(二)

    阿里P7Android高级架构进阶视频(内含Handler视频讲解)免费学习请点击:https://space.bilibili.com/474380680 一.Handler 在Android开发的 ...

  3. Json解析之FastJson

    版权声明:转载请注明出处 https://blog.csdn.net/heqiangflytosky/article/details/37659943 1.FastJson介绍 FastJson是阿里 ...

  4. React-Native 使用中的坑

    android 1.键盘会在 ScrollView 之上----不知道是不是未给ScrollView 设置高度的问题 2.navigation的标题在android居中显示办法 navigationO ...

  5. android中使用MediaPlayer和SurfaceView播放视频

    package com.test.video; import java.io.IOException; import android.media.AudioManager; import androi ...

  6. PAT 乙级练习题1001 害死人不偿命的(3n+1)猜想 (15)

    1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去, ...

  7. spring+springmvc+mybatis框架搭建

    一.开发前准备 1)ecplise4.11.0 百度网盘:https://pan.baidu.com/s/1wO9_I52lp0mYNeNTdnj80w 提取码:booa 2)jdk1.6.0_45  ...

  8. 互斥量mutex简介

    互斥量又称互斥锁.互斥量是一个可以处于两态之一的变量:解锁和加锁. 简介 编辑 如果不需要信号量的计数能力,有时可以使用信号量的一个简化版本,称为互斥量(mutex).互斥量仅仅适用于管理共享资源或一 ...

  9. mongodb的学习 (3)

    聚合函数 - 添加基础数据:db.local.save({contry:'中国',name:'小明',score:77});db.local.save({contry:'中国',name:'小红',s ...

  10. nodejs模块——fs模块 使用fs.write读文件

    fs.write() fs.read(fd,buffer,offset,length[,position],callback(err,bytesWritten,buffer))接收6个参数. 参数说明 ...