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 ...
随机推荐
- sudo -E的意思
1.sudo -E -E选项在man page中的解释是: -E The -E (preserve environment) option indicates to the security poli ...
- Echoprint系列--编译
近期要做一个音乐相关的client.当中一个功能是音乐识别.搜索了一些资料选择Echoprint来开发.Echoprint是开源免费的,并且多种client都支持能节约非常多时间,今天主要下载和编译源 ...
- iOS开发:iPhone6、6 plus适配
本文转载至 http://jingyan.baidu.com/article/8cdccae97a5c2b315413cda9.html 1 2 3 4 5 6 7 分步阅读 随着苹果公司持续推出新产 ...
- Delphi 发送邮件 通过Office Outlook
Delphi 发送邮件 通过Office Outlook 网上搜到的Delphi邮件发送系统,绝大多数是使用SMTP协议来发送. 但是事实上它们已经过时了,大多数邮件服务器已经屏蔽了Delphi In ...
- spring boot打包会有.war.original文件的原因 (笔记)
今天使用spring boot 2.1.1.RELEASE版本搭建项目,虽然可以直接打包成可运行的jar包,但是由于公司准备采用docker容器来管理项目,所以需要把jar包变成war包,并且war包 ...
- 一个页面从输入URL到页面加载完成发生了...待细化
一个页面从输入URL到页面加载完成发生了... 1.查找浏览器缓存 2.寻址:DNS解析 查找该域名对应的IP地址, 如果需要重定向(301),则再次发起请求 3. 进行HTTP协议会话 4.客户端发 ...
- JavaScript -获取屏窗与视窗、文档宽高
实例:1920*1080的电脑屏幕 1.获取窗口中的文档显示区域宽高 clientw = window.innerWidth; //1920(包含滚动条) clienth = window.inner ...
- for...of 与 for...in 区别
一.for...of 1.定义 for...of 语句遍历可迭代对象(包括数组.Set 和 Map 结构.arguments 对象.DOM NodeList 对象.字符串等). 2.语法 for (v ...
- .net ASPxTreeList 使用手记
ASPxTreeList在使用ASPxGridViewExporter控件做导出时,如果指定文件名是中文时会乱码可以用以下方法解决: grvExporter为ASPxGridViewExporter控 ...
- python线程间数据共享(示例演示)
``` import threading data_list = [] def task(arg): data_list.append(arg) print(data_list) def run(): ...