LeetCode: Next Greater Element I
stack和map用好就行
public class Solution {
public int[] nextGreaterElement(int[] findNums, int[] nums) {
Stack maxNumSeq = new Stack();
Map<Integer, Integer> greaterNum = new HashMap<Integer, Integer>();
for (int i = nums.length-1; i >= 0; i--) {
while (maxNumSeq.empty() != true) {
int num = (int)maxNumSeq.peek();
if (nums[i] < num) {
greaterNum.put(nums[i], num);
maxNumSeq.push(nums[i]);
break;
}
else maxNumSeq.pop();
}
if (maxNumSeq.empty() == true) {
maxNumSeq.push(nums[i]);
greaterNum.put(nums[i], -1);
}
}
int[] ans = new int[findNums.length];
for (int i = 0; i < ans.length; i++) {
ans[i] = greaterNum.get(findNums[i]);
}
return ans;
}
}
LeetCode: Next Greater Element I的更多相关文章
- LeetCode——Next Greater Element I
LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...
- [LeetCode] Next Greater Element III 下一个较大的元素之三
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- [LeetCode] Next Greater Element II 下一个较大的元素之二
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [LeetCode] Next Greater Element I 下一个较大的元素之一
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- LeetCode Next Greater Element III
原题链接在这里:https://leetcode.com/problems/next-greater-element-iii/description/ 题目: Given a positive 32- ...
- [leetcode]Next Greater Element
第一题:寻找子集合中每个元素在原集合中右边第一个比它大的数. 想到了用哈希表存这个数的位置,但是没有想到可以直接用哈希表存next great,用栈存还没找到的数,没遍历一个数就考察栈中的元素小,小的 ...
- [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] 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] 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 ...
随机推荐
- 尽管以C++为基础,但 Java 是一种更纯粹的面向对象程序设计语言
“尽管以C++为基础,但 Java 是一种更纯粹的面向对象程序设计语言”. 无论C++还是Java 都属于杂合语言.但在 Java 中,设计者觉得这种杂合并不象在 C++里那么重要.杂合语言 允许采用 ...
- Windows Server2008R2中导入Excel
使用Microsoft.ACE.OLEDB对Excel进行操作: string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + & ...
- 【转】VC++计算当前时间点间隔N天的时间(不使用CTimeSpan类)
转自:http://blog.csdn.net/fzuim/article/details/61199351 涉及到有效期的设置,需要计算N天时间间隔的时间. C++ Code 123456789 ...
- dropload 使用表
移动端下拉刷新.上拉加载更多插件 依赖 (dependence) Zepto 或者 jQuery 1.7以上版本,推荐jQuery 2.x版本(二者不要同时引用) Zepto or jQuery 1. ...
- Python 基础之列表去重的几种玩法
列表去重 1.方法1 借助一个临时列表 ids = [1,2,3,3,4,2,3,4,5,6,1] news_ids = [] for id in ids: if id not in news_ids ...
- 【BZOJ2090/2089】[Poi2010]Monotonicity 2 动态规划+线段树
[BZOJ2090/2089][Poi2010]Monotonicity Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k].选出一个长度 ...
- flex组合流动布局实例---利用css的order属性改变盒子排列顺序
flex弹性盒子 <div class="container"> <div class="box yellow"></div> ...
- mysql 中调用存储过程之后,连接断开不可用
解决方法: 由 mysql_real_connect(&m_mysql,host,user,passwd,Db,0,NULL,0) == NULL 改为 mysql_real_connect( ...
- Learn How To Cross Over The Wall
1.一个proxy的实现 http://blog.codingnow.com/2011/05/xtunnel.html 2.SOCK5 RFC http://www.faqs.org/rfcs/rfc ...
- 【我的Android进阶之旅】 Android Studio插件之Jenkins插件介绍
一Jenkins插件功能介绍 1Jenkins任务列表 2切换Jenkins分组 3构建Jenkins任务 4进入构建Jenkins任务的页面 5进入最后一次构建Jenkins任务的页面 6增加Jen ...