Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode321. 拼接最大数 | Create Maximum Number大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

Given two arrays of length m and n with digits 0-9 represenTing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array

Given two arrays of length @H_823_19@m and n with digits 0-9 represenTing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits.

Note: You should try to optimize your time and space complexity.

Example 1:

Input:
nums1 = 
nums2 = 
k = 
Output:
[3,4,6,5][9,1,2,5,8,3]5[9,3]

Example 2:

Input:
nums1 = 
nums2 = 
k = 
Output:
[6,7][6,4]5[6,7,4]

Example 3:

Input:
nums1 = 
nums2 = 
k = 
Output:
[3,9][8,9]3[9,9]

说明: 请尽可能地优化你算法的时间和空间复杂度。

示例 1:

输入:
nums1 = 
nums2 = 
k = 
输出:
[3,3]

示例 2:

输入:
nums1 = 
nums2 = 
k = 
输出:
[6,4]

示例 3:

输入:
nums1 = 
nums2 = 
k = 
输出:
[3,9]
380 ms

@H_801_66@ 1 class Solution {
@H_801_66@ 2     func maxnumber(_ nums1: [Int],_ nums2: [Int],_ k: int) -> [Int] {
@H_801_66@ 3         let m = nums1.count
@H_801_66@ 4         let n = nums2.count
@H_801_66@ 5         
@H_801_66@ 6         var res = [Int]()
@H_801_66@ 7         
@H_801_66@ 8         let c = max(0,k-n)
@H_801_66@ 9         
@H_801_66@10         for i in c...min(k,m) {
@H_801_66@11             let r1 = maxNumArr(nums1,i)
@H_801_66@12             let r2 = maxNumArr(nums2,k-i)
@H_801_66@13             let tmp = maxNums(r1,r2,k)
@H_801_66@14             if isGreater(tmp,res,0,0) {
@H_801_66@15                 res = tmp
@H_801_66@16             }
@H_801_66@17         }
@H_801_66@18         
@H_801_66@19         return res
@H_801_66@20     }
@H_801_66@21     
@H_801_66@22     func maxNumArr(_ nums : [Int],_ k : int) -> [Int] {
@H_801_66@23         var res = [Int]()
@H_801_66@24         for i in 0..<nums.count {
@H_801_66@25             let num = nums[i]
@H_801_66@26             while !res.isEmpty && num > res.last! && nums.count + res.count > k + i {
@H_801_66@27                 res.removeLast()
@H_801_66@28             }
@H_801_66@29             res.append(num)
@H_801_66@30             conTinue
@H_801_66@31         }
@H_801_66@32         return res
@H_801_66@33     }
@H_801_66@34     
@H_801_66@35     func maxNums(_ num1 : [Int],_ num2 : [Int],_ k : int) -> [Int] {
@H_801_66@36         var res = [Int]()
@H_801_66@37         var i = 0,j = 0
@H_801_66@38         for _ in 0..<k {
@H_801_66@39             if isGreater(num1,num2,i,j) {
@H_801_66@40                 res.append(num1[i])
@H_801_66@41                 i+=1
@H_801_66@42             }else {
@H_801_66@43                 res.append(num2[j])
@H_801_66@44                 j+=1
@H_801_66@45             }
@H_801_66@46         }
@H_801_66@47         
@H_801_66@48         return res
@H_801_66@49     }
@H_801_66@50     
@H_801_66@51     func isGreater(_ nums1: [Int],_ nums2 : [Int],_ i : Int,_ j : int) -> Bool {
@H_801_66@52         var i = i,j = j
@H_801_66@53         while i < nums1.count,j < nums2.count && nums1[i] == nums2[j] {
@H_801_66@54             i+=1
@H_801_66@55             j+=1
@H_801_66@56         }
@H_801_66@57         
@H_801_66@58         return j == nums2.count || (i < nums1.count && nums1[i] > nums2[j])
@H_801_66@59     }
@H_801_66@60 }

大佬总结

以上是大佬教程为你收集整理的[Swift]LeetCode321. 拼接最大数 | Create Maximum Number全部内容,希望文章能够帮你解决[Swift]LeetCode321. 拼接最大数 | Create Maximum Number所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。