stack和map用好就行

  1. public class Solution {
  2. public int[] nextGreaterElement(int[] findNums, int[] nums) {
  3. Stack maxNumSeq = new Stack();
  4. Map<Integer, Integer> greaterNum = new HashMap<Integer, Integer>();
  5. for (int i = nums.length-1; i >= 0; i--) {
  6. while (maxNumSeq.empty() != true) {
  7. int num = (int)maxNumSeq.peek();
  8. if (nums[i] < num) {
  9. greaterNum.put(nums[i], num);
  10. maxNumSeq.push(nums[i]);
  11. break;
  12. }
  13. else maxNumSeq.pop();
  14. }
  15. if (maxNumSeq.empty() == true) {
  16. maxNumSeq.push(nums[i]);
  17. greaterNum.put(nums[i], -1);
  18. }
  19. }
  20. int[] ans = new int[findNums.length];
  21. for (int i = 0; i < ans.length; i++) {
  22. ans[i] = greaterNum.get(findNums[i]);
  23. }
  24. return ans;
  25. }
  26. }

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. 第一百五十三节,封装库--JavaScript,表单验证--备注字数验证

    封装库--JavaScript,表单验证--备注字数验证 效果图 html <div id="reg"> <h2 class="tuo"> ...

  2. 【python】计算器

    from __future__ import division import sys from math import * from PyQt4.QtCore import * from PyQt4. ...

  3. 怎样从Mysql官网下载mysql.tar.gz版本的安装包

     今天学习在Linux上部署项目,用到了Mysql,因此想要下载适用于Linux的安装版本,在Mysql官网找了半天,终于找到怎样下载了,这里写出来,以后大家找的时候就好找了. 第一步:在百度输入My ...

  4. SQL语法:查询此表有另外一个表没有的数据

    select bei.ExamItem_Code2,*from PeisPatientExamItem ppeijoin PeisPatientFeeItem ppfion ppfi.ID_Patie ...

  5. box-sizing与calc()与flex

    1,Syntax: /* Keyword values */ box-sizing: content-box; box-sizing: border-box; /* Global values */ ...

  6. Objective-C规范注释心得——同时兼容appledoc(docset、html)与doxygen(html、pdf)的文档生成

    作者:zyl910 手工写文档是一件苦差事,幸好现在有从源码中抽取注释生成文档的专用工具.对于Objective-C来说,目前最好用的工具是appledoc和doxygen.可是这两种工具对于注释的要 ...

  7. Powershell Exchange Message Per Day Sent and Reveive

    Powershell Exchange Message Per Day Sent and Reveive # Initialize some variables used for counting a ...

  8. attributes["wv"].nodeValue

    w 获取自定义属性的值 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  9. app返回之前app焦点的操作方法

    var hdWin,hdfocus: THandle; trdID: Cardinal; //获取前置app窗口句柄 hdWin := GetForegroundWindow;//FindWindow ...

  10. 【转】Spring AOP 实现原理与 CGLIB 应用

    AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理.安全检查.缓存.对象池管理等.AOP 实现的关键就在于 ...