LeetCode - 503. Next Greater Element II
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 {
public int[] nextGreaterElements(int[] nums) {
if (nums == null || nums.length <= 0)
return new int[0];
int[] ret = new int[nums.length];
for (int i=0; i<ret.length; i++)
ret[i] = -1;
for (int i=0; i<nums.length; i++) {
int flag = 0;
for (int j=i+1; j<nums.length; j++) {
if (nums[j] > nums[i]) {
ret[i] = nums[j];
flag = 1;
break;
}
}
if (flag == 0) {
for (int k=0; k<i; k++) {
if (nums[k] > nums[i]) {
ret[i] = nums[k];
break;
}
}
}
}
return ret;
}
}
LeetCode - 503. Next Greater Element II的更多相关文章
- [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】503. Next Greater Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 单调递减栈 日期 题目地址:https:/ ...
- 496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III
▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组 ▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1 ● ...
- 503. Next Greater Element II
https://leetcode.com/problems/next-greater-element-ii/description/ class Solution { public: vector&l ...
- 503 Next Greater Element II 下一个更大元素 II
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...
- [LeetCode] 496. Next Greater Element I 下一个较大的元素 I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- [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 ...
- LeetCode 503. 下一个更大元素 II(Next Greater Element II)
503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...
- [LeetCode] Next Greater Element II 下一个较大的元素之二
Given a circular array (the next element of the last element is the first element of the array), pri ...
随机推荐
- 无脑博士的试管们--dfs搜素
无脑博士有三个容量分别是A,B,C升的试管,A,B,C分别是三个从1到20的整数,最初,A和B试管都是空的,而C试管是装满硫酸铜溶液的.有时,无脑博士把硫酸铜溶液从一个试管倒到另一个试管中,直到被灌试 ...
- 【最短路】道路重建 @upcexam5797
时间限制: 1 Sec 内存限制: 128 MB 题目描述 小L的家乡最近遭遇了一场洪水,城市变得面目全非,道路也都被冲毁了.生活还要继续,于是市政府决定重建城市中的道路. 在洪水到来前,城市中共有n ...
- JSAP106
JSAP106 一.clientX.clientY 点击位置距离当前body可视区域的x,y坐标 二.pageX.pageY 对于整个页面来说,包括了被卷去的body部分的长度 三.screenX.s ...
- Wordpress上一篇文章和下一篇文章
<div class="chapter"> <div class="prev"><?php previous_post_link( ...
- windows环境下面批量修改文件夹名称
ren 1 A ren 2 B ren 3 C ren 4 D 电脑桌面新建文档 ---> 批量修改文件夹名称.txt 修改文件名称为:--->批量修改文件夹名称.bat 内容如上: 双击 ...
- [web 前端] mobx教程(一)-mobx简介
opy from : https://blog.csdn.net/smk108/article/details/84777649 Mobx是通过函数响应式编程使状态管理变得简单和可扩展的状态管理库.M ...
- MySQL体系结构图详解
体系结构图如下: 连接层 思想为解决资源的频繁分配﹑释放所造成的问题,为数据库连接建立一个“缓冲池”.原理预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕 ...
- springboot项目logback配置文件示例
logback-spring.xml <?xml version="1.0" encoding="UTF-8"?> <configuratio ...
- 洛谷 P1162 填涂颜色
题目链接:https://www.luogu.org/problemnew/show/P1162 题目描述由数字0组成的方阵中,有一任意形状闭合圈,闭合圈由数字1构成,围圈时只走上下左右4个方向.现要 ...
- Effective Java 第三版——69. 仅在发生异常的条件下使用异常
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...