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. c# 实现遍历 DataTable 和DataSet (简单的方式)

    今天 做一个小程序 ,遇到了这样一个 问题就是 怎样简单的 遍历一个 DataTable 一. DataTable table= DBhelper.GetDataTable(str);foreach( ...

  2. MathType与Origin是怎么兼容的

    MathType作为一款常用的公式编辑器,可以与很多的软件兼容使用.Origin虽然是一款专业绘图与数据分析软件,但是在使用过程中也是可以用到MathType.它可以帮助Origin给图表加上标签,或 ...

  3. 转(解决GLIBC_2.x找不到的编译问题)

    Linux/CentOS 升级C基本运行库CLIBC的注意事项(当想解决GLIBC_2.x找不到的编译问题) 分类: 开发环境 Linux2014-09-24 10:32 8933人阅读 评论(5)  ...

  4. Android无线测试之—UiAutomator UiObject API介绍六

    手势操作 1.手势相关操作 2.相关API介绍 返回值 API 描述 boolean performMultiPointerGesture(PointerCoords[]... touches) 执行 ...

  5. IOS开发之自定义键盘

     本文转载至 http://blog.csdn.net/majiakun1/article/details/41242069 实际开发过程中,会有自定义键盘的需求,比如,需要添加一个表情键盘.本文提供 ...

  6. GS与NGP通信(不断跟新)

  7. Web的本质以及第一个Django实例.

       Web框架的本质:    所有的Web应用本质上就是一个socket服务器, 而用户的浏览器就是一个socket客户端. import socket sk = socket.socket() s ...

  8. docker 中安装 FastDFS 总结

    如题,参考各资料后,安装FastDFS总结.基于已有docker镜像 https://hub.docker.com/r/luhuiguo/fastdfs/ docker pull luhuiguo/f ...

  9. 爬虫入门【9】Python链接Excel操作详解-openpyxl库

    Openpyx是一个用于读写Excel2010各种xlsx/xlsm/xltx/xltm文件的python库. 现在大多数用的都是office2010了,如果之前之前版本的可以使用xlrd读,xlwt ...

  10. POJ 3037 Skiing(Dijkstra)

    Skiing Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4668   Accepted: 1242   Special ...