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 ...
随机推荐
- 第一百五十三节,封装库--JavaScript,表单验证--备注字数验证
封装库--JavaScript,表单验证--备注字数验证 效果图 html <div id="reg"> <h2 class="tuo"> ...
- 【python】计算器
from __future__ import division import sys from math import * from PyQt4.QtCore import * from PyQt4. ...
- 怎样从Mysql官网下载mysql.tar.gz版本的安装包
今天学习在Linux上部署项目,用到了Mysql,因此想要下载适用于Linux的安装版本,在Mysql官网找了半天,终于找到怎样下载了,这里写出来,以后大家找的时候就好找了. 第一步:在百度输入My ...
- SQL语法:查询此表有另外一个表没有的数据
select bei.ExamItem_Code2,*from PeisPatientExamItem ppeijoin PeisPatientFeeItem ppfion ppfi.ID_Patie ...
- box-sizing与calc()与flex
1,Syntax: /* Keyword values */ box-sizing: content-box; box-sizing: border-box; /* Global values */ ...
- Objective-C规范注释心得——同时兼容appledoc(docset、html)与doxygen(html、pdf)的文档生成
作者:zyl910 手工写文档是一件苦差事,幸好现在有从源码中抽取注释生成文档的专用工具.对于Objective-C来说,目前最好用的工具是appledoc和doxygen.可是这两种工具对于注释的要 ...
- Powershell Exchange Message Per Day Sent and Reveive
Powershell Exchange Message Per Day Sent and Reveive # Initialize some variables used for counting a ...
- attributes["wv"].nodeValue
w 获取自定义属性的值 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- app返回之前app焦点的操作方法
var hdWin,hdfocus: THandle; trdID: Cardinal; //获取前置app窗口句柄 hdWin := GetForegroundWindow;//FindWindow ...
- 【转】Spring AOP 实现原理与 CGLIB 应用
AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理.安全检查.缓存.对象池管理等.AOP 实现的关键就在于 ...