[ 命名建議 ] LeetCode 496. Next Greater Element I #117
Answered
by
twy30
LPenny-github
asked this question in
Q&A
-
|
LeetCode: 線上教材: nums1 和 nums2 我猶豫很久,還是不知如何命名,因此沒有為他們改名 orz 再麻煩 @twy30 給予命名建議 orz 感激不盡 using System.Collections.Generic;
public class Solution
{
public int[] NextGreaterElement(int[] nums1, int[] nums2)
{
Stack<int> nextGreaterNumber = new Stack<int>();
Dictionary<int, int> validPair = new Dictionary<int, int>();
for (int i = 0; i < nums2.Length; ++i)
{
while (nextGreaterNumber.Count > 0 && nextGreaterNumber.Peek() < nums2[i])
{
validPair.Add(nextGreaterNumber.Pop(), nums2[i]);
}
nextGreaterNumber.Push(nums2[i]);
}
int[] output = new int[nums1.Length];
for (int j = 0; j < nums1.Length; ++j)
{
output[j] = validPair.ContainsKey(nums1[j])? validPair[nums1[j]]: -1;
}
return output;
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
twy30
Jan 19, 2021
Replies: 1 comment 1 reply
-
|
你好 😊
能理解 😅 可以考慮:
Stack<int> nextGreaterNumber = new Stack<int>();可以考慮:
Dictionary<int, int> validPair = new Dictionary<int, int>();可以考慮:
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
LPenny-github
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@LPenny-github
你好 😊
能理解 😅
可以考慮:
nums1:inputNumbersoutput是由「一一處理此 array 的成員」而來,所以對應到inputNumbersnums2:nextGreaterNumberCandidatesnextGreaterNumber的候選者」可以考慮:
nextGreaterNumberStacknextGreaterNumberCandidateStack可以考慮:
nextGreaterNumbersnextGreaterNumberTablenextGreaterNumberLookupTable