题目:

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. AVL树,C语言实现,完整代码,先贴上,讲解稍后

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> #de ...

  2. Python List reverse() 与list[::-1]

    reverse() https://www.runoob.com/python/att-list-reverse.html list[::-1] https://blog.csdn.net/usern ...

  3. Rust <1>:数据类型、变量、可变性、常量、隐藏

    rust 是强类型语言,所有变量.常量都必须有明确的数据类型:很多情况下,省略类型声明,编译器可自动推导,但不是所有情况下都会成功. rust 有整型.浮点型.布尔型.字符型.数组.元组.枚举.结构体 ...

  4. ELK日志分析平台.1-搭建

    ELK日志分析平台.1-搭建 2017-12-28 | admin 一.简介1.核心组成    ELK由Elasticsearch.Logstash和Kibana三部分组件组成:    Elastic ...

  5. JavaScript_DOM详解

    节点操作: 查看对象属性的值obj.getAttribute() 如: //获取图片 var imgs = document.getElementsByTagName("img") ...

  6. 项目案例之GitLab的数据迁移

    项目案例之GitLab的数据迁移 链接:https://pan.baidu.com/s/1CgaEv12cwfbs5RxcNpxdAg 提取码:fytm 复制这段内容后打开百度网盘手机App,操作更方 ...

  7. 前端学习(十四)js回顾和定时器(笔记)

    回顾知识点:    作用域:        1.全局变量:在任何位置都可以使用的变量        2.局部变量:只能在函数内部使用的变量        3.闭包:子函数可以使用父函数的局部变量 -- ...

  8. propTypes和 defaultProps

    propTypes和 defaultProps propTypes: 可以 用来做类型的校验 限制类型 isRequired 必须要求传递 要使用必须先引入: import PropTypes fro ...

  9. 开启.NET Core 3时代,DevExpress v19.2.5带你全新启航

    DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...

  10. Java高并发网络编程(四)Netty

    在网络应用开发的过程中,直接使用JDK提供的NIO的API,比较繁琐,而且想要进行性能提升,还需要结合多线程技术. 由于网络编程本身的复杂性,以及JDK API开发的使用难度较高,所以在开源社区中,涌 ...