[Swift]LeetCode496. 下一個更大元素 I | Next Greater Element I

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-yvmdfqok-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

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.github

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:app

  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.

 給定兩個沒有重複元素的數組 nums1 和 nums2 ,其中nums1 是 nums2 的子集。找到 nums1 中每一個元素在 nums2 中的下一個比其大的值。this

nums1 中數字 x 的下一個更大元素是指 x 在 nums2 中對應位置的右邊的第一個比 x 大的元素。若是不存在,對應位置輸出-1。spa

示例 1:code

輸入: nums1 = [4,1,2], nums2 = [1,3,4,2].
輸出: [-1,3,-1]
解釋:
    對於num1中的數字4,你沒法在第二個數組中找到下一個更大的數字,所以輸出 -1。
    對於num1中的數字1,第二個數組中數字1右邊的下一個較大數字是 3。
    對於num1中的數字2,第二個數組中沒有下一個更大的數字,所以輸出 -1。

示例 2:htm

輸入: nums1 = [2,4], nums2 = [1,2,3,4].
輸出: [3,-1]
解釋:
    對於num1中的數字2,第二個數組中的下一個較大數字是3。
    對於num1中的數字4,第二個數組中沒有下一個更大的數字,所以輸出 -1。

注意:

  1. nums1nums2中全部元素是惟一的。
  2. nums1nums2 的數組大小都不超過1000。

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 }
相關文章
相關標籤/搜索