【LeetCode】556. Next Greater Element III 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/next-greater-element-iii/description/

题目描述:

Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.

Example 1:

Input: 12
Output: 21

Example 2:

Input: 21
Output: -1

题目大意

同样的数字进行重新排列组合,找出下一个更大组合方式情况下,比当前的数字更大一点的那个。

注意是个32位的正数。

解题方法

31. Next Permutation解题方式基本一模一样,先找到从后向前数,第一个降序的位置,把此位置后面的翻转。再把这个降序数字和后面第一个比它大的位置交换即可。

如果从第n个数字到最后都是递减的并且第n-1个数字小于第n个,说明从第n个数字开始的这段序列是字典序组合的最后一个,在下一个组合中他们要倒序变为字典序第一个,然后从这段序列中找出第一个大于第n-1个数字的数和第n-1个数字交换就可以了。

举个栗子,当前组合为12431,可以看出431是递减的,同时4>2,这样我们把431倒序,组合就变为12134,然后从134中找出第一个大于2的数字和2交换,这样就得到了下一个组合13124。对于完全递减的组合例如4321在倒序之后就可以结束了。

大体和Leetcode的这个解答方式类似:

十分需要注意的是,找到了下一个更大的数字,有可能超出了题目中所说的32位的正数。需要额外注意。

代码如下:

class Solution(object):
def nextGreaterElement(self, n):
"""
:type n: int
:rtype: int
"""
nums = list(str(n))
n = len(nums)
i = n - 1
while i > 0 and nums[i] <= nums[i - 1]:
i -= 1
if i == 0:
return -1
self.reverse(nums, i, n - 1)
for j in range(i, n):
if nums[j] > nums[i-1]:
self.swap(nums, i-1, j)
break
ans = int("".join(nums))
if ans > 1 << 31:
return -1
else:
return ans def reverse(self, nums, i, j):
"""
contains i and j.
"""
for k in range(i, (i + j) / 2 + 1):
self.swap(nums, k, i + j - k) def swap(self, nums, i, j):
"""
contains i and j.
"""
nums[i], nums[j] = nums[j], nums[i]

参考资料:

  1. https://leetcode.com/articles/next-permutation/
  2. https://blog.csdn.net/tstsugeg/article/details/50261517

日期

2018 年 8 月 27 日 ———— 就要开学了!

【LeetCode】556. Next Greater Element III 解题报告(Python)的更多相关文章

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

  2. LeetCode 496 Next Greater Element I 解题报告

    题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...

  3. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  4. 【LeetCode】503. Next Greater Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 单调递减栈 日期 题目地址:https:/ ...

  5. 【LeetCode】496. Next Greater Element I 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接遍历查找 字典保存位置 日期 题目地址:http ...

  6. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

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

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

  8. 556. Next Greater Element III

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  9. 【LeetCode】980. Unique Paths III解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

随机推荐

  1. linux—查看所有的账号以及管理账号

    用过Linux系统的人都知道,Linux系统查看用户不是会Windows那样,鼠标右键看我的电脑属性,然后看计算机用户和组即可. 那么Linux操作系统里查看所有用户该怎么办呢?用命令.其实用命令就能 ...

  2. Python与Perl的相似与差别

    Python version 3.7版本 00.命令行交互 命令行交互 Perl Python perl -e <Perl代码>     #Unix/Linux/Windows/DOS 直 ...

  3. mysql数据查询语言DQL

    DB(database)数据库:存储数据的'仓库',保存了一系列有组织的数据 DBMS(Database Management System)数据库管理系统:用于创建或管理DB SQL(Structu ...

  4. day35前端基础之BOM和DOM

    day35前端基础之BOM和DOM BOM操作 简介 BOM(Browser Object Model)是指浏览器对象模型,它使 JavaScript 有能力与浏览器进行"对话". ...

  5. Redis(一)【基础入门】

    目录 一.大型网站的系统特点 二.大型网站架构发展历程 三.从NoSQL说起 四.Redis简介 五.Redis安装 1.上传并解压 2.安装C语言编译环境 3.修改安装位置 4.编译安装 5.启动R ...

  6. tomcat 8 内存优化

    在Linux环境下设置Tomcat JVM,在/opt/tomcat/bin/catalina.sh文件中找到"# ----- Execute The Requested Command&q ...

  7. go channel 概述 - 管道

    概述 unix/linux OS 的一个进程的输出可以是另一个进程的输入,这些进程使用stdin与stdout设备作为通道,在进程之间传递数据. 同样的,GO中有io.Reader与io.Writer ...

  8. golang vendor

    安装参考 https://blog.csdn.net/huwh_/article/details/77169858 Go 1.5引入了vendor文件夹,其对语言使用,go命令没有任何影响.若某个路径 ...

  9. 【Linux】【Basis】网络

    Linux网络属性配置                           计算机网络:          TCP/IP:协议栈(使用)             ISO,OSI:协议栈(学习)     ...

  10. 【Java 8】Stream API

    转自 Java 8 Stream Java8的两个重大改变,一个是Lambda表达式,另一个就是本节要讲的Stream API表达式.Stream 是Java8中处理集合的关键抽象概念,它可以对集合进 ...