關(guān)于我的 Leetcode 題目解答谁鳍,代碼前往 Github:https://github.com/chenxiangcyr/leetcode-answers
LeetCode題目:496. Next Greater Element I
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 and nums2 are unique.
- The length of both nums1 and nums2 would not exceed 1000.
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
/* Time Complexity O(m * n)
int[] result = new int[nums1.length];
Arrays.fill(result, -1);
for(int i = 0; i < nums1.length; i++) {
boolean foundEqual = false;
for(int j = 0; j < nums2.length; j++) {
if(nums1[i] == nums2[j]) {
foundEqual = true;
}
if(nums1[i] < nums2[j] && foundEqual == true) {
result[i] = nums2[j];
break;
}
}
}
return result;
*/
/*
Time Complexity O(m + n)
*/
Stack<Integer> stack = new Stack<>();
// key: 數(shù)字导帝,value:next Greater Element
Map<Integer, Integer> map = new HashMap<>();
for(int num : nums2) {
while(!stack.isEmpty() && num > stack.peek()) {
int t = stack.pop();
map.put(t, num);
}
stack.push(num);
}
while(!stack.isEmpty()) {
int t = stack.pop();
map.put(t, -1);
}
int[] result = new int[nums1.length];
for(int i = 0; i < nums1.length; i++) {
result[i] = map.get(nums1[i]);
}
return result;
}
}
LeetCode題目:503. Next Greater Element II
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.
class Solution {
public int[] nextGreaterElements(int[] nums) {
if(nums == null || nums.length == 0) return nums;
// 存儲(chǔ)數(shù)字idx
Stack<Integer> stack = new Stack<>();
// key: 數(shù)字idx毙籽,value:next Greater Element
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < 2 * nums.length; i++) {
while(!stack.isEmpty() && nums[i % nums.length] > nums[stack.peek()]) {
int t = stack.pop();
map.put(t, nums[i % nums.length]);
}
if (i < nums.length) stack.push(i);
}
while(!stack.isEmpty()) {
int t = stack.pop();
map.put(t, -1);
}
int[] result = new int[nums.length];
for(int i = 0; i < nums.length; i++) {
result[i] = map.get(i);
}
return result;
}
}
LeetCode題目:556. Next Greater Element III
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 n and 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
class Solution {
public int nextGreaterElement(int n) {
char[] number = (n + "").toCharArray();
/*
從右邊開(kāi)始,找到第一個(gè)比右邊元素小的
例如:534976蔓搞,找到了數(shù)字4對(duì)應(yīng)的位置2
*/
int i = number.length - 1;
while(i > 0 && number[i-1] >= number[i]) {
i--;
}
// 表明所有數(shù)字已經(jīng)是升序排列了,得不到結(jié)果
if (i == 0) {
return -1;
}
i--; // 4的位置
// 找到4的右邊比4大的所有元素中,最小的一個(gè)金赦,此時(shí)找到了6
int x = number[i];
int smallest = i + 1;
for (int j = i + 1; j < number.length; j++) {
if (number[j] > x && number[j] <= number[smallest]) {
smallest = j;
}
}
// 交換 4 和 6,得到536974
char temp = number[i];
number[i] = number[smallest];
number[smallest] = temp;
// 將6的后半部分974重新排序对嚼,得到 536479
Arrays.sort(number, i + 1, number.length);
long val = Long.parseLong(new String(number));
return (val <= Integer.MAX_VALUE) ? (int) val : -1;
}
}