【LeetCode】503. Next Greater Element II 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/minesweeper/description/
题目描述
Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn’t exist, output -1 for this number.
Example 1:
Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number;
The second 1's next greater number needs to search circularly, which is also 2.
Note: The length of given array won’t exceed 10000.
题目大意
找出一个数组中每个数字的下一个更大的数字,可以看做是循环数组。
解题方法
暴力解法
额,直接使用暴力解法的话。可以使用%
符号来实现循环数组。可惜超时了。
代码:
class Solution(object):
def nextGreaterElements(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
_len = len(nums)
res = [-1] * _len
for i in xrange(_len):
for j in xrange(i + 1, _len * 2):
if nums[j % _len] > nums[i]:
res[i] = nums[j % _len]
break
return res
单调递减栈
下面的引用自 http://blog.csdn.net/Cloudox_/article/details/62881181
在遍历数组的过程中,如果是往后遇到大的数,那就是第一个更大的数,如果一直遇到不断小的数,才会一直找不到,我们可以用一个栈来记录,遇到比栈顶小的数字就放入栈中,遇到比栈顶大的数字就说明这是栈顶数字的下一个更大的数,就将其放在结果数组的对应位置上,栈顶的元素出栈,继续比较新的栈顶的数,如果还是大,那么继续记录,出栈,直到栈顶的数比新数要小,那么就可以将新数入栈了。因为我们要将找到的更大的数放在对应位置上,所以栈中记录的应该是元素位置,而不是具体的数字,但比较的时候还是比较原来的数组中这个位置的数字,这一点要想清楚。此外,因为会出现循环寻找的情况,所以数组我们可能遍历两次。这个做法会快很多。
官方解读的ppt做的很好~也推荐看下:https://leetcode.com/problems/next-greater-element-ii/solution/
注意,栈里保存的是索引,一定不要忘记。
同样适用单调栈的题目有:962. Maximum Width Ramp
代码:
class Solution(object):
def nextGreaterElements(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res = [-1] * len(nums)
stack = []
for i in range(len(nums)) * 2:
while stack and (nums[stack[-1]] < nums[i]):
res[stack.pop()] = nums[i]
stack.append(i)
return res
C++代码如下:
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
const int N = nums.size();
vector<int> res(N, -1);
stack<int> stack;
for (int i = 0; i < N * 2; i++) {
while (!stack.empty() && nums[stack.top()] < nums[i % N]) {
res[stack.top()] = nums[i % N];
stack.pop();
}
if (i < N)
stack.push(i);
}
return res;
}
};
日期
2018 年 3 月 6 日
2018 年 12 月 28 日 —— 元旦假期到了
【LeetCode】503. Next Greater Element II 解题报告(Python & C++)的更多相关文章
- [LeetCode] 503. Next Greater Element II 下一个较大的元素 II
Given a circular array (the next element of the last element is the first element of the array), pri ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- LeetCode - 503. Next Greater Element II
Given a circular array (the next element of the last element is the first element of the array), pri ...
- LeetCode 496 Next Greater Element I 解题报告
题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
随机推荐
- MariaDB—备份数据库
1> 备份单个数据库 mysqldump -uroot -plichao123 --database students1 > stundents.sql; 2>查看备份文件 3> ...
- yum和apt-get的用法和区别
一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包 ...
- 28-Merge Two Sorted Lists
easy 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- LetNet、Alex、VggNet分析及其pytorch实现
简单分析一下主流的几种神经网络 LeNet LetNet作为卷积神经网络中的HelloWorld,它的结构及其的简单,1998年由LeCun提出 基本过程: 可以看到LeNet-5跟现有的conv-& ...
- 大数据学习----day27----hive02------1. 分桶表以及分桶抽样查询 2. 导出数据 3.Hive数据类型 4 逐行运算查询基本语法(group by用法,原理补充) 5.case when(练习题,多表关联)6 排序
1. 分桶表以及分桶抽样查询 1.1 分桶表 对Hive(Inceptor)表分桶可以将表中记录按分桶键(某个字段对应的的值)的哈希值分散进多个文件中,这些小文件称为桶. 如要按照name属性分为3个 ...
- midi的一些概念,包括一些音乐的概念
参考:http://www.yueqixuexi.com/jita/20180918205363.html https://blog.csdn.net/meicheng777/article/deta ...
- SSH服务及通过SSH方式登录linux
SSH服务及通过SSH方式登录linux 1.检查SSH服务转自:[1]Linux之sshd服务https://www.cnblogs.com/uthnb/p/9367875.html[2]Linux ...
- RecyclerView实现侧滑删除、置顶、滑动
1.首先在build.gradle里添加 compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.2.1' 2.设置recyclerView的item布 ...
- GO 数字运算
大整数运算 // bigint project main.go package main import ( "fmt" "math" "math/bi ...
- 【Linux】【Services】【Disks】bftfs
1. 简介 1.1 Btrfs(B-tree,Butter FS,Better FS) 1.2. 遵循GPL,由oracle在2007年研发,支持CoW 1.3. 主要为了替代早期的ext3/ext4 ...