[Leetcode學習-c++&java]Next Greater Element I ~ III
問題:Next Greater Element I
難度:easy
說明:
題目給出陣列 A 是陣列 B 的一個子串,然後要求找到 A 裡面元素,在 B 裡面所在位置,然後從 B 裡面那個位置開始,找到第一個比該元素值大的元素,然後返回所有比 A 大元素的集合。
題目連線:https://leetcode.com/problems/next-greater-element-i/
輸入範圍:
- All elements in
nums1
andnums2
are unique. - The length of both
nums1
andnums2
would not exceed 1000.
輸入案例:
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1] // 注意 1 在 nums2 是第一個,右邊比他大第一個是 3
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.
我的程式碼:
簡單,暴力的話就來個 三重迴圈,遍歷 A 集合,然後找到 B 集合相等的元素,然後 再 從該位置開始遍歷 B 集合,找到右邊第一個元素即可。
不過可以加一個 map 做快取,能夠避免 A 重複元素。
我再將 B 所有元素都弄成 map,然後遍歷 A 直接拿更快。
Java:
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int[] greater = new int[nums1.length];
HashMap<Integer, Integer> cache = new HashMap<Integer, Integer>();
for(int j = 0;j < nums2.length;j ++)
for(int t = j + 1;t < nums2.length;t ++)
if(nums2[j] < nums2[t]) {
cache.put(nums2[j], nums2[t]);
break;
}
for(int i = 0;i < nums1.length;i ++) {
if(cache.containsKey(nums1[i])) greater[i] = cache.get(nums1[i]);
else greater[i] = -1;
}
return greater;
}
}
C++:
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
map<int, int> cache;
vector<int> greater;
for(int i = 0;i < nums2.size();i ++)
for(int j = i + 1;j < nums2.size();j ++)
if(nums2[j] > nums2[i]) {
cache[nums2[i]] = nums2[j];
break;
}
for(int i = 0;i < nums1.size();i ++)
if(cache.count(nums1[i])) greater.push_back(cache[nums1[i]]);
else greater.push_back(-1);
return greater;
}
};
問題:Next Greater Element II
難度:medium
說明:
題目給出一個陣列,然後陣列是頭尾相接,你需要把裡面所有元素都找出右邊第一個大的元素。
題目連線:https://leetcode.com/problems/next-greater-element-ii/
輸入範圍:
- The length of given array won't exceed 10000.
輸入案例:
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.
我的程式碼:
暴力是在太好解了,但是這樣子又會導致執行時間變慢,
暴力(壓個行):
Java:
class Solution {
public int[] nextGreaterElements(int[] nums) {
int[] greater = new int[nums.length];
for(int i = 0, len = nums.length, j, t;i < len;greater[i] = j < len ? nums[t] : -1, i ++)
for(j = 1, t = (i + j) % len;j < len && nums[t] <= nums[i];j ++, t = (i + j) % len);
return greater;
}
}
也可以弄個單調棧,這個比較中規中矩,O(m*n)時間複雜度
Java:
class Solution {
public int[] nextGreaterElements(int[] nums) {
int top = 0, len = nums.length, i;
int[] greater = new int[len];
if(nums.length == 0) return greater;
int[][] mStack = new int[len][2];
for(i = 1, mStack[top][0] = nums[0];i < len;i ++) {
while(top >= 0 && nums[i] > mStack[top][0]) {
greater[mStack[top][1]] = nums[i]; top --;
}
mStack[++ top][0] = nums[i];
mStack[top][1] = i;
}
while(top >= 0) {
for(i = 0;i < len;i ++) {
while(top >= 0 && nums[i] > mStack[top][0]) {
greater[mStack[top][1]] = nums[i]; top --;
}
}
greater[mStack[top --][1]] = -1;
}
return greater;
}
}
C++:
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int len = nums.size(), top = 0, i;
vector<int> greater(len);
if (!len) return greater;
int (*mStack)[2] = new int[len][2];
for (i = 1, mStack[top][0] = nums[0], mStack[top][1] = 0; i < len; i++) {
while (top >= 0 && nums[i] > mStack[top][0]) {
greater[mStack[top][1]] = nums[i]; top--;
}
mStack[++top][0] = nums[i];
mStack[top][1] = i;
}
for (i = 0; i < len; i++) {
while (top >= 0 && nums[i] > mStack[top][0]) {
greater[mStack[top][1]] = nums[i]; top--;
}
}
while (top >= 0) greater[mStack[top--][1]] = -1;
return greater;
}
};
問題:Next Greater Element III
難度:medium
說明:
題目給出一個數字 N,然後把位數字數量相同,並且各個位數字組合成的 新 數字是最小的,剛好比 N 大的數字,如果不存在或者超過 int 就返回 -1。
題目連線:https://leetcode.com/problems/next-greater-element-iii/
輸入範圍:
1 <= n <= 231 - 1
輸入案例:
Example 1:
Input: n = 12
Output: 21
Example 2:
Input: n = 21
Output: -1
我的程式碼:
剛好比 N 大,然後又是最小的一個
1、先從尾部開始找 第一個 N[i - 1] < N[i];
2、把 N[i] ~ N[len] 裡面 > N[i - 1] 並且是最小的數字和 N[i - 1] 對換;
3、最後把 N[i + 1] ~ N[len] 重新排序
C++和java只是比較順序不同
Java:
class Solution {
public int nextGreaterElement(int n) {
char[] chs = String.valueOf(n).toCharArray();
int[] nums = new int[chs.length];
for(int i = chs.length; i -- > 0;) nums[i] = chs[i] - '0';
for(int i = nums.length; i -- > 1;) {
if(nums[i - 1] < nums[i]) { // 先找出 下降的數字
int ti = i;
// 從 i 開始找出最小的 > nums[i - 1]的數
while(ti + 1 < nums.length && nums[i - 1] < nums[ti + 1]) ti ++;
// 然後置換
nums[i - 1] ^= nums[ti];
nums[ti] ^= nums[i - 1];
nums[i - 1] ^= nums[ti];
// 最後重排序一遍
for(int j = i, end = chs.length - 1; j < end; j ++, end --) {
int tj = j;
while(tj + 1 <= end && nums[j] >= nums[tj + 1]) tj ++;
nums[tj] ^= nums[j];
nums[j] ^= nums[tj];
nums[tj] ^= nums[j];
}
// 轉為數字輸出
for(int j = nums.length;j -- > 0;) chs[j] = (char)(nums[j] + '0');
try { // 我決定用 try...catch 這個奇淫技巧
return Integer.valueOf(new String(chs));
} catch (Exception e) {
return -1;
}
}
}
return -1;
}
}
C++:
class Solution {
public:
int nextGreaterElement(int n) {
int index = 0, tens = 10, nums[32];
long long res = 0;
for (; n; n /= tens)
nums[index++] = n % tens;
for (int i = 0; i < index - 1; i++) {
if (nums[i] > nums[i + 1]) {
int ti = i + 1, begin = 0;
for (; ti - 1 >= 0 && nums[ti - 1] > nums[i + 1]; ti--);
swap(nums[ti], nums[i + 1]);
for (int j = i; j > begin; j--, begin ++) {
int tj = j;
while (tj - 1 >= begin && nums[j] >= nums[tj - 1]) tj--;
swap(nums[tj], nums[j]);
}
for (int j = index; j -- > 0;)
if(res * tens <= INT_MAX) res = res * tens + nums[j];
else return -1;
return res;
}
}
return - 1;
}
};
相關文章
- LeetCode演算法題-Next Greater Element I(Java實現)LeetCode演算法Java
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解題報告LeetCode
- LeetCode之Convert BST to Greater Tree(Kotlin)LeetCodeKotlin
- LeetCode-House Robber IIILeetCode
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- LeetCode 之 JavaScript 解答第169題 —— 求眾數 I(Majority Element)LeetCodeJavaScript
- leetcode Remove ElementLeetCodeREM
- LeetCode-Shortest Word Distance IIILeetCode
- 【LeetCode】House Robber III(337)LeetCode
- Best Time to Buy and Sell Stock III -- LeetCodeLeetCode
- [leetcode]remove-elementLeetCodeREM
- Leetcode-Remove ElementLeetCodeREM
- Remove Element leetcode javaREMLeetCodeJava
- Find Peak element leetcodeLeetCode
- element 學習借鑑 p1
- leetcode260-single number iiiLeetCode
- Leetcode-Best Time to Buy and Sell Stock IIILeetCode
- Best Time to Buy and Sell Stock III leetcode javaLeetCodeJava
- uc/os-iii學習筆記-訊息傳遞筆記
- Leetcode學習LeetCode
- 【LeetCode】如何學習LeetCode?LeetCode
- ABP VNext學習日記8
- ABP VNext學習日記7
- 精通one,學習another,關注next
- Leetcode 31 Next PermutationLeetCode
- Leetcode-Next PermutationLeetCode
- Next Permutation leetcode javaLeetCodeJava
- Element-UI / dom.js 的學習UIJS
- leetcode-27. Remove ElementLeetCodeREM
- Leetcode 27 Remove-ElementLeetCodeREM
- leetcode27_Remove ElementLeetCodeREM
- LeetCode-Majority Element IILeetCode
- Leetcode-Find Peak ElementLeetCode
- leetcode刷題--Majority ElementLeetCode
- [LeetCode] Remove Element 移除元素LeetCodeREM
- Leetcode——437. 路徑總和 IIILeetCode
- leetcode best-time-to-buy-and-sell-stock-iii(Java)LeetCodeJava
- [LintCode/LeetCode] Contains Duplicate IIILeetCodeAI