【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:

  1. Input: 12
  2. Output: 21

Example 2:

  1. Input: 21
  2. 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位的正数。需要额外注意。

代码如下:

  1. class Solution(object):
  2. def nextGreaterElement(self, n):
  3. """
  4. :type n: int
  5. :rtype: int
  6. """
  7. nums = list(str(n))
  8. n = len(nums)
  9. i = n - 1
  10. while i > 0 and nums[i] <= nums[i - 1]:
  11. i -= 1
  12. if i == 0:
  13. return -1
  14. self.reverse(nums, i, n - 1)
  15. for j in range(i, n):
  16. if nums[j] > nums[i-1]:
  17. self.swap(nums, i-1, j)
  18. break
  19. ans = int("".join(nums))
  20. if ans > 1 << 31:
  21. return -1
  22. else:
  23. return ans
  24. def reverse(self, nums, i, j):
  25. """
  26. contains i and j.
  27. """
  28. for k in range(i, (i + j) / 2 + 1):
  29. self.swap(nums, k, i + j - k)
  30. def swap(self, nums, i, j):
  31. """
  32. contains i and j.
  33. """
  34. 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. 压力测试工具——apchebench(简称ab)

    ab的原理 ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx.li ...

  2. Spring Security 基于URL的权限判断

    1.  FilterSecurityInterceptor 源码阅读 org.springframework.security.web.access.intercept.FilterSecurityI ...

  3. 4 — springboot中的jsr303检验

    1.导入依赖 <!--JSR303校验的依赖 --> <dependency> <groupId>org.springframework.boot</grou ...

  4. C语言中不用 + 和 - 求两个数之和

    (二)解题 题目大意:不用+或者-实现两个整数的加法 解题思路:不用+或者-,就自然想到位运算,无非就是与或非来实现二进制的加法 首先,我们来看一位二进制的加法和异或运算 A B A&B A^ ...

  5. A Child's History of England.4

    Still, the Britons would not yield. They rose again and again, and died by thousands, sword in hand. ...

  6. 大数据学习day23-----spark06--------1. Spark执行流程(知识补充:RDD的依赖关系)2. Repartition和coalesce算子的区别 3.触发多次actions时,速度不一样 4. RDD的深入理解(错误例子,RDD数据是如何获取的)5 购物的相关计算

    1. Spark执行流程 知识补充:RDD的依赖关系 RDD的依赖关系分为两类:窄依赖(Narrow Dependency)和宽依赖(Shuffle Dependency) (1)窄依赖 窄依赖指的是 ...

  7. Linux网络管理(一)之配置主机名与域名

    Linux网络管理(一)之配置主机名与域名参考自:[1]修改主机名(/etc/hostname和/etc/hosts区别) https://blog.csdn.net/shmily_lsl/artic ...

  8. ORACLE 服务器验证

    位于$ORACLE_HOME/network/admin/sqlnet.oraSQLNET.AUTHENTICATION_SERVICES=none|all|ntsnone:关闭操作系统认证,只能密码 ...

  9. 【Java 泛型】之 <? super T> 和<? extends T> 中 super ,extends如何理解?有何异同?

    Java 泛型 <? super T> 和<? extendsT>中 super ,extends怎么 理解?有何不同? 简介 前两篇文章介绍了泛型的基本用法.类型擦除以及泛型 ...

  10. [学习总结]3、Android---Scroller类(左右滑动效果常用的类)

    参考资料:http://blog.csdn.net/vipzjyno1/article/details/24592591 非常感谢这个兄弟! 在android学习中,动作交互是软件中重要的一部分,其中 ...