在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You are given two arrays (without duplicates) The Next Greater Number of a number x in 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:
给定两个没有重复元素的数组
示例 1: 输入: nums1 = [4,1,2], nums2 = [1,3,4,2]. 输出: [-1,3,-1] 解释: 对于num1中的数字4,你无法在第二个数组中找到下一个更大的数字,因此输出 -1。 对于num1中的数字1,第二个数组中数字1右边的下一个较大数字是 3。 对于num1中的数字2,第二个数组中没有下一个更大的数字,因此输出 -1。 示例 2: 输入: nums1 = [2,4], nums2 = [1,2,3,4]. 输出: [3,-1] 解释: 对于num1中的数字2,第二个数组中的下一个较大数字是3。 对于num1中的数字4,第二个数组中没有下一个更大的数字,因此输出 -1。 注意:
16ms 1 class Solution { 2 func nextGreaterElement(_ findNums: [Int], _ nums: [Int]) -> [Int] { 3 4 // 9,8,7,6,5,10 -> 10 is the next greater for all 5 var result = [Int]() 6 var dict = [Int: Int]() 7 var stack = [Int]() 8 9 for num in nums { 10 while !stack.isEmpty && stack.last! < num { 11 let last = stack.removeLast() 12 dict[last] = num 13 } 14 15 stack.append(num) 16 } 17 18 for num in findNums { 19 result.append(dict[num] ?? -1) 20 } 21 22 return result 23 } 24 } 20ms 1 class Solution { 2 func nextGreaterElement(_ findNums: [Int], _ nums: [Int]) -> [Int] { 3 4 var ans = [Int]() 5 var stack = [Int]() 6 var dict = [Int:Int]() 7 8 for (index, item) in nums.enumerated() { 9 dict[item] = index 10 ans.append(0) 11 } 12 13 for val in nums { 14 while stack.count > 0 && val > stack.last! { 15 ans[dict[stack.last!]!] = val 16 stack.removeLast() 17 } 18 stack.append(val) 19 } 20 var newAns = [Int]() 21 for val in findNums { 22 if let x = dict[val] { 23 newAns.append(ans[x]==0 ? -1 : ans[x]) 24 } 25 } 26 return newAns 27 } 28 } 24ms 1 class Solution { 2 func nextGreaterElement(_ findNums: [Int], _ nums: [Int]) -> [Int] { 3 var dict = [Int:Int]() 4 var stack = [Int]() 5 6 for num in nums { 7 while stack.count > 0 && stack.last! < num { 8 dict[stack.popLast()!] = num 9 } 10 stack.append(num) 11 } 12 13 return findNums.map { dict[$0] ?? -1 } 14 } 15 } 32ms 1 class Solution { 2 func findindex(_ nums: [Int], _ search: Int, _ count: Int) -> Int { 3 for i in 0..<count { 4 if (nums[i] == search) { 5 return i 6 } 7 } 8 return -1 9 } 10 func nextGreaterElement(_ findNums: [Int], _ nums: [Int]) -> [Int] { 11 var output = [Int]() 12 var index = -1 13 var flag = false 14 let count = nums.count 15 for i in findNums { 16 index = findindex(nums, i, count) 17 if (index != (count-1)) { 18 flag = false 19 for j in nums[index..<count] { 20 if (i<j) { 21 output.append(j) 22 flag = true 23 break 24 } 25 } 26 if (!flag) { 27 output.append(-1) 28 } 29 } else { 30 output.append(-1) 31 } 32 } 33 return output 34 } 35 } 40ms 1 class Solution { 2 func nextGreaterElement(_ nums1: [Int], _ nums2: [Int]) -> [Int] { 3 var dict = [Int: Int]() 4 5 for (index, num) in nums2.enumerated() { 6 dict[num] = index 7 } 8 9 var result: [Int] = [] 10 for num in nums1 { 11 var index: Int = dict[num]! 12 var max: Int = -1 13 while index < nums2.count { 14 if num < nums2[index] { 15 max = nums2[index] 16 break 17 } 18 19 index += 1 20 } 21 22 result.append(max) 23 } 24 25 return result 26 } 27 } 44ms 1 class Solution { 2 func nextGreaterElement(_ nums1: [Int], _ nums2: [Int]) -> [Int] { 3 var nextGreater = [Int]() 4 var stack = [Int]() 5 var dict = [Int:Int]() 6 7 for num in nums2 { 8 while !stack.isEmpty && num > stack.last! { 9 dict[stack.removeLast()] = num 10 } 11 12 stack.append(num) 13 } 14 15 for num in nums1 { 16 nextGreater.append(dict[num] ?? -1) 17 } 18 19 return nextGreater 20 } 21 } 68ms 1 class Solution { 2 func nextGreaterElement(_ findNums: [Int], _ nums: [Int]) -> [Int] { 3 var result: [Int] = [] 4 var item = findNums.count-1 5 while item >= 0 { 6 var itemResult = -1 7 var itemFinded = -1 8 for index in 0..<nums.count { 9 if findNums[item] == nums[index] { 10 itemFinded = index 11 } 12 if findNums[item] < nums[index] && itemFinded != -1 { 13 itemResult = nums[index] 14 break 15 } 16 } 17 result.insert(itemResult, at: 0) 18 item = item - 1 19 } 20 return result 21 } 22 } 72ms 1 class Solution { 2 func nextGreaterElement(_ findNums: [Int], _ nums: [Int]) -> [Int] { 3 var res:[Int] = [] 4 for i in 0..<findNums.count { 5 for j in 0..<nums.count{ 6 if findNums[i] == nums[j] { 7 for k in j...nums.count-1 { 8 if (findNums[i] < nums[k]) { 9 res.append(nums[k]) 10 break 11 }else if (k == (nums.count-1)){ 12 res.append(-1) 13 } 14 } 15 16 } 17 } 18 } 19 return res 20 } 21 } 72ms 1 class Solution { 2 func nextGreaterElement(_ nums1: [Int], _ nums2: [Int]) -> [Int] { 3 var nextGreater = [Int]() 4 var stack = [Int]() 5 var dict = [Int:Int]() 6 7 for num in nums2 { 8 while !stack.isEmpty && num > stack.last! { 9 dict[stack.removeLast()] = num 10 } 11 12 stack.append(num) 13 } 14 15 for num in nums1 { 16 nextGreater.append(dict[num] ?? -1) 17 } 18 19 return nextGreater 20 } 21 }
|
请发表评论