You are given two arrays (without duplicates) nums1
and nums2
where nums1
’s elements are subset of nums2
. Find all the next greater numbers for nums1
's elements in the corresponding places of nums2
.
The Next Greater Number of a number x in nums1
is the first greater number to its right in nums2
. If it does not exist, output -1 for this number.
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. Output: [-1,3,-1] Explanation: For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1. For number 1 in the first array, the next greater number for it in the second array is 3. For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4]. Output: [3,-1] Explanation: For number 2 in the first array, the next greater number for it in the second array is 3. For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
Note:
- All elements in
nums1
andnums2
are unique.
- The length of both
nums1
andnums2
would not exceed 1000.
* key point : 当一段序列S递减遇到A不再满足递减时,序列S中比A小的数的next larger都是A
* 模拟一下这个过程就是先往右遍历数组,不再满足条件就往回写next larger,然后接着往右遍历
*
* 这个过程就是stack,如果要说怎么想到的,先考虑递增递减的特殊情况
* 递增比较简单,直接跟右边的数判断就好了,
* 递减的话要一直遍历
* 接着考虑递增 + 递减,递减 + 递增
* 一点点从简单到复杂,说实话,蛮考验人的。。。
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Stack<Integer> stack = new Stack<Integer>();
for(int i=0; i<nums2.length; i++) {
while(!stack.isEmpty() && stack.peek() < nums2[i])
map.put(stack.pop(), nums2[i]);
stack.push(nums2[i]);
}
int[] ret = new int[nums1.length];
for(int i=0; i<nums1.length; i++)
ret[i] = map.containsKey(nums1[i]) ? map.get(nums1[i]) : -1;
return ret;
}
}
Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number.
Example 1:
Input: [1,2,1] Output: [2,-1,2] Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number;
The second 1's next greater number needs to search circularly, which is also 2.
Note: The length of given array won't exceed 10000.
* 可以直接new一个2倍的数组
* 或者不用额外空间,控制下标
class CopyArray {
public int[] nextGreaterElements(int[] nums) {
int[] a = new int[nums.length * 2];
for(int i=0; i<a.length; i++)
a[i] = nums[i % nums.length];
Stack<Integer> s = new Stack<Integer>();
int[] ret = new int[nums.length];
Arrays.fill(ret, -1);
for(int i=0; i<a.length; i++) {
while(!s.isEmpty() && a[s.peek()] < a[i]) {
int t = s.pop();
if(t < nums.length) ret[t] = a[i];
}
s.add(i);
}
return ret;
}
}
class Solution {
public int[] nextGreaterElements(int[] nums) {
Stack<Integer> s = new Stack<Integer>();
int[] ret = new int[nums.length];
Arrays.fill(ret, -1);
for(int i=0; i<2*nums.length; i++) {
while(!s.isEmpty() && nums[s.peek() % nums.length] < nums[i % nums.length]) {
int t = s.pop();
if(t < nums.length) ret[t] = nums[i % nums.length];
}
s.add(i);
}
return ret;
}
}
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer nand is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
Example 1:
Input: 12 Output: 21
Example 2:
Input: 21 Output: -1
* 思路都是类似的,从后往前遍历,一直遍历到不再不再递增
* 思路应该也可以沿用之前的stack解法
class Solution {
public int nextGreaterElement(int n) {
String s = "" + n;
int i = s.length() - 2;
for(; i>=0; i--) {
if(s.charAt(i) < s.charAt(i+1))
break;
}
if(i == -1) return -1;
StringBuilder sb = new StringBuilder();
sb.append(s.substring(0, i));
// 从后开始往前找,找到第一个比该字符大的,since有序,可以用二分优化
int j = s.length() - 1;
while(s.charAt(j) <= s.charAt(i)) j--;
sb.append(s.charAt(j));
sb.append(new StringBuilder(s.substring(j+1)).reverse());
sb.append(s.charAt(i));
sb.append(new StringBuilder(s.substring(i+1, j)).reverse());
try {
return Integer.valueOf(sb.toString());
} catch (Exception e) {
return -1;
}
}
}