2018-09-24 21:52:38

一、Next Greater Element I

问题描述:

问题求解:

本题只需要将nums2中元素的下一个更大的数通过map保存下来,然后再遍历一遍nums1即可。

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int[] res = new int[nums1.length];
Map<Integer, Integer> map = new HashMap<>();
Stack<Integer> stack = new Stack<>();
for (int num : nums2) {
while (!stack.isEmpty() && stack.peek() < num) map.put(stack.pop(), num);
stack.push(num);
}
for (int i = 0; i < nums1.length; i++) {
res[i] = map.getOrDefault(nums1[i], -1);
}
return res;
}

二、Next Greater Element II

问题描述:

问题求解:

本题和上一题应该来说是差不多的,都可以使用Stack来对数据进行维护,有两个需要注意的地方,一个是本题中的循环性,解决循环性的方法就是在数组后再加上一个数组即可。

二一个是本题中并没有说是无重复元素的,因此只能在栈中保存index值。

栈中保存的只有第一次遍历的下标值,第二次就没有必要再进行压栈操作了,另外,栈中保存的是递减的数的index。

    public int[] nextGreaterElements(int[] nums) {
int[] res = new int[nums.length];
Arrays.fill(res, -1);
Stack<Integer> stack = new Stack<>();
int n = nums.length;
for (int i = 0; i < 2 * n; i++) {
int num = nums[i % n];
while (!stack.isEmpty() && nums[stack.peek()] < num) res[stack.pop()] = num;
if (i < n) stack.push(i);
}
return res;
}

三、Next Greater Element III

问题描述:

问题求解:

就是一条按字典序的下一个Permutation。

    public int nextGreaterElement(int n) {
char[] c = String.valueOf(n).toCharArray();
int idx = c.length - 2;
while (idx >= 0 && c[idx] >= c[idx + 1]) idx--;
if (idx == -1) return -1;
int j = c.length - 1;
while (j >= 0 && c[j] <= c[idx]) j--;
swap(c, idx, j);
reverseSort(c, idx + 1, c.length - 1);
long res = Long.valueOf(String.valueOf(c));
return res >= Integer.MAX_VALUE ? -1 : (int)res;
} private void swap(char[] c, int i, int j) {
char tmp = c[i];
c[i] = c[j];
c[j] = tmp;
} private void reverseSort(char[] c, int start, int end) {
if (start >= end) return;
while (start < end) {
swap(c, start, end);
start++;
end--;
}
}

下一个更大的数 Next Greater Element的更多相关文章

  1. LeetCode 503. 下一个更大元素 II(Next Greater Element II)

    503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...

  2. LeetCode 556. 下一个更大元素 III(Next Greater Element III)

    556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并 ...

  3. LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35

    496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组 nums1 和 nums2,其中 nums1 是 nums2 的子集.找到  ...

  4. [Swift]LeetCode503. 下一个更大元素 II | Next Greater Element II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  5. [Swift]LeetCode496. 下一个更大元素 I | Next Greater Element I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  6. [Swift]LeetCode556. 下一个更大元素 III | Next Greater Element III

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  7. 503 Next Greater Element II 下一个更大元素 II

    给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...

  8. Leetcode 503. 下一个更大元素 II

    1.题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应 ...

  9. [Leetcode]下一个更大元素II

    题目 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地 ...

随机推荐

  1. [VS] - "包含了重复的“Content”项。.NET SDK 默认情况下包括你项目中的“Content”项。

    copy to :http://www.cnblogs.com/jinzesudawei/p/7376916.html VS 2017 升级至  VS 2017 v15.3 后,.Net Core 1 ...

  2. Git本地分支和远程分支关联

    转载:https://blog.csdn.net/cherishhere/article/details/52606884 转载:https://blog.zengrong.net/post/1746 ...

  3. Docker基础学习-尚硅谷

    视频地址:链接: https://pan.baidu.com/s/15sJuEh5cVTJ7-vWSH7vffw 提取码: zf25 笔记:

  4. 牛客网数据库SQL实战(1-5)

    1.查找最晚入职员工的所有信息 CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` date NOT NULL, `f ...

  5. Python3 tkinter基础 Frame bind 敲击键盘事件 将按键打印到console中

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. Restful framework【第十篇】响应器(渲染器)

    基本使用 -响应器(一般用默认就可以了) -局部配置 renderer_classes=[JSONRenderer,] -全局配置 'DEFAULT_RENDERER_CLASSES': ( 'res ...

  7. 转载:Systemd 命令

    目录 一.由来 二.Systemd 概述 三.系统管理 3.1 systemctl 3.2 systemd-analyze 3.3 hostnamectl 3.4 localectl 3.5 time ...

  8. 论文笔记:Show, Attend and Tell: Neural Image Caption Generation with Visual Attention

    Show, Attend and Tell: Neural Image Caption Generation with Visual Attention 2018-08-10 10:15:06 Pap ...

  9. ES6中新增的数组知识

    JSON数组格式转换 JSON的数组格式就是为了前端快速的把JSON转换成数组的一种格式,我们先来看一下JSON的数组格式怎么写. let  json = {     '0': 'xzblogs', ...

  10. ToString()格式和用法大全,C#实现保留两位小数的方法

    C,货币,2.5.ToString("C"),¥2.50.D,十进制数,25.ToString("D5"),00025.E,科学型,25000.ToString ...