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的更多相关文章

  1. LeetCode——Next Greater Element I

    LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...

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

  3. [LeetCode] Next Greater Element II 下一个较大的元素之二

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  4. [LeetCode] Next Greater Element I 下一个较大的元素之一

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  5. LeetCode Next Greater Element III

    原题链接在这里:https://leetcode.com/problems/next-greater-element-iii/description/ 题目: Given a positive 32- ...

  6. [leetcode]Next Greater Element

    第一题:寻找子集合中每个元素在原集合中右边第一个比它大的数. 想到了用哈希表存这个数的位置,但是没有想到可以直接用哈希表存next great,用栈存还没找到的数,没遍历一个数就考察栈中的元素小,小的 ...

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

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

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

随机推荐

  1. Photoshop脚本之eps转换成jpg

    function saveEPS( doc, saveFile ) { var saveOptions = new JPEGSaveOptions( ); saveOptions.encoding = ...

  2. Struts1中actionform和action属于MVC哪一层,为什么?

    Struts1中actionform和action属于MVC哪一层,为什么? 解答:actionform和action属于MVC的Model层,Action用来处理业务逻辑,actionform保存用 ...

  3. 你 get 了无数技能,为什么一事无成

    前 几日看到阮一峰老师的发的一句话,颇有感慨,「你只是坐在电脑前,往网上发表了一段文字或者一张图片,随便什么,就能够接触到多少陌生的灵魂.这就是我热 爱互联网的原因」.我打心底认为这是一个最好的时代, ...

  4. springboot如何直接读取webapp下页面?

    公司改用springboot的时候,将页面相关的文件都放在了src/main/webapp下,我直接通过main方式启动的时候,无法读取到src/mian/webapp下文件,但是通过spring-b ...

  5. JStorm开发经验+运维经验总结

    1.开发经验总结  ——12 Sep 2014 · 8 revisions 在jstorm中, spout中nextTuple和ack/fail运行在不同的线程中, 从而鼓励用户在nextTuple里 ...

  6. [MongoDB]学习笔记--User管理

    1. 创建一个超级用户 use admin db.createUser( { user: "adminUserName", pwd: "userPassword" ...

  7. fis3 部署手册

    为什么使用FIS3 项目上线一段时间后如果更新JS或CSS文件,而客户端已经对该文件缓存过了,那就有可能会无法及时更新而继续采用旧的JS或CSS文件,无法达到想要的效果. 处理类似情况最有效的解决方案 ...

  8. 160628、利用Oracle rownum让表排序字段值连续

    利用Oracle rownum让表排序字段值连续 1.需求说明 表(eval_index)中有字段如下: 表字段 描述 说明 ID 主键 GROUP_ID 分组编号 SORT_NUM 排序序号 按照分 ...

  9. less-符号之逗号,空格,父级选择器

    Less符号 逗号 example: .test() { box-shadow+: inset 0 0 10px #555; } .study { .test(); box-shadow+: 0 0 ...

  10. 网站漏洞扫描工具(appscan,mdcsoft-ips)

    网站漏洞扫描工具:主要应用网站漏洞扫描工具,其原理是通过工具通过对网站的代码阅读,发现其可被利用的漏洞进行告示,通过前人收集的漏洞编成数据库,根据其扫描对比做出. 具体网站扫描工具有:appscan, ...