classSolution: defnextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: m, n = len(nums1), len(nums2) res = [0] * m for i inrange(m): j = nums2.index(nums1[i]) k = j + 1 while k < n and nums2[k] < nums2[j]: k += 1 res[i] = nums2[k] if k < n else -1 return res
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicint[] nextGreaterElement(int[] nums1, int[] nums2) { intm= nums1.length, n = nums2.length; int[] res = newint[m]; for (inti=0; i < m; ++i) { intj=0; while (j < n && nums2[j] != nums1[i]) { ++j; } intk= j + 1; while (k < n && nums2[k] < nums2[j]) { ++k; } res[i] = k < n ? nums2[k] : -1; } return res; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassSolution { publicint[] NextGreaterElement(int[] nums1, int[] nums2) { int m = nums1.Length, n = nums2.Length; int[] res = newint[m]; for (int i = 0; i < m; ++i) { int j = 0; while (j < n && nums2[j] != nums1[i]) { ++j; } int k = j + 1; while (k < n && nums2[k] < nums2[j]) { ++k; } res[i] = k < n ? nums2[k] : -1; } return res; } }
classSolution { public: vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2){ int m = nums1.size(); int n = nums2.size(); vector<int> res(m); for (int i = 0; i < m; ++i) { int j = 0; while (j < n && nums2[j] != nums1[i]) { ++j; } int k = j + 1; while (k < n && nums2[k] < nums2[j]) { ++k; } res[i] = k < n ? nums2[k] : -1; } return res; } };
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var nextGreaterElement = function(nums1, nums2) { const m = nums1.length, n = nums2.length; const res = newArray(m).fill(0); for (let i = 0; i < m; ++i) { let j = 0; while (j < n && nums2[j] !== nums1[i]) { ++j; } let k = j + 1; while (k < n && nums2[k] < nums2[j]) { ++k; } res[i] = k < n ? nums2[k] : -1; } return res; };
funcnextGreaterElement(nums1, nums2 []int) []int { m, n := len(nums1), len(nums2) res := make([]int, m) for i, num := range nums1 { j := 0 for j < n && nums2[j] != num { j++ } k := j + 1 for k < n && nums2[k] < nums2[j] { k++ } if k < n { res[i] = nums2[k] } else { res[i] = -1 } } return res }
classSolution: defnextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: res = {} stack = [] for num inreversed(nums2): while stack and num >= stack[-1]: stack.pop() res[num] = stack[-1] if stack else -1 stack.append(num) return [res[num] for num in nums1]
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { publicint[] nextGreaterElement(int[] nums1, int[] nums2) { Map<Integer, Integer> map = newHashMap<Integer, Integer>(); Deque<Integer> stack = newArrayDeque<Integer>(); for (inti= nums2.length - 1; i >= 0; --i) { intnum= nums2[i]; while (!stack.isEmpty() && num >= stack.peek()) { stack.pop(); } map.put(num, stack.isEmpty() ? -1 : stack.peek()); stack.push(num); } int[] res = newint[nums1.length]; for (inti=0; i < nums1.length; ++i) { res[i] = map.get(nums1[i]); } return res; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicint[] NextGreaterElement(int[] nums1, int[] nums2) { Dictionary<int, int> dictionary = new Dictionary<int, int>(); Stack<int> stack = new Stack<int>(); for (int i = nums2.Length - 1; i >= 0; --i) { int num = nums2[i]; while (stack.Count > 0 && num >= stack.Peek()) { stack.Pop(); } dictionary.Add(num, stack.Count > 0 ? stack.Peek() : -1); stack.Push(num); } int[] res = newint[nums1.Length]; for (int i = 0; i < nums1.Length; ++i) { res[i] = dictionary[nums1[i]]; } return res; } }