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. jquery图片上传前预览剪裁

    http://www.webmotionuk.co.uk/jquery/image_upload_crop.php http://keleyi.com/a/bjad/liuvpkke.htm 不错的d ...

  2. noip 模拟赛 After 17(递推+特殊的技巧)

    来源:Violet_II T1 好神的一题,我竟然没做出来QAQ 首先我们发现,答案是sigma(x[i]*x[j], i>j)+sigma(y[i]*y[j], i>j).显然只需要讨论 ...

  3. java ssh介绍(1)

    今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...

  4. python解释器的分类及特点

    CPython 当从Python官方网站下载并安装好Python2.7后,就直接获得了一个官方版本的解释器:Cpython,这个解释器是用C语言开发的,所以叫CPython,在命名行下运行python ...

  5. nginx https配置+nginx跳转到万网虚拟主机

    server { listen 443 ssl; server_name www.104dh.com 104dh.com; ssl on; ssl_certificate cert104/152678 ...

  6. Google I/O 2013 – Volley: Easy, Fast Networking for Android

    1.什么是volley          Volley是Ficus Kirpatrick在Gooogle I/O 2013发布的一个处理和缓存网络请求的库,能使网络通信更快,更简单,更健壮.Volle ...

  7. Django - 请求与响应、表单、中间件、上下文处理器

    请求与响应篇 一.HttpRequest对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象.视图函数的第一个参数(request)是HttpRequest对象在djang ...

  8. C++获取某个文件夹下的所有文件

    获取某个文件夹下的所有文件,返回各文件的路径加文件名 path为某文件夹的路径:eg. char * filePath = "C:\\Users\\WUQP\\Desktop\\test_d ...

  9. c#学习笔记之使用 TableLayoutPanel 控件设置窗体布局

    使用 TableLayoutPanel 控件设置窗体布局 在 Visual Studio IDE 左侧,找到“工具箱”选项卡. 选择“工具箱”选项卡,随即将显示工具箱.(或者,在菜单栏上,依次选择“视 ...

  10. MySQL Connector/C++ C++连接mysql

    MySQL :: MySQL Connector/C++ Developer Guide :: 1 Introduction to Connector/C++ https://dev.mysql.co ...