Create Maximum Number

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 must be preserved. Return an array of the k digits. You should try to optimize your time and space complexity.

 

Example

Given nums1 = [3, 4, 6, 5], nums2 = [9, 1, 2, 5, 8, 3], k = 5
return [9, 8, 6, 5, 3]

Given nums1 = [6, 7], nums2 = [6, 0, 4], k = 5
return [6, 7, 6, 0, 4]

Given nums1 = [3, 9], nums2 = [8, 9], k = 3
return [9, 8, 9]

这道题是参考网上的解法,是道难题。。注意比较两个字符数串的大小 而不是当前字符数字的大小 (参考反例 60 604)
 
 1 public class Solution {
 2     /**
 3      * @param nums1 an integer array of length m with digits 0-9
 4      * @param nums2 an integer array of length n with digits 0-9
 5      * @param k an integer and k <= m + n
 6      * @return an integer array
 7      */
 8     public int[] maxNumber(int[] nums1, int[] nums2, int k) {
 9         // Write your code here
10         int len1 = nums1.length;
11         int len2 = nums2.length;
12         int[] res = new int[k];
13         for(int i = Math.max(0, k-len2); i<=k&&i<=len1;i++){
14             int[] temp = merge(maxArray(nums1, i), maxArray(nums2, k-i), k);
15             if(isGreater(temp, 0, res, 0)){
16                 res = temp;
17             }
18         }
19         return res;
20     }
21 
22     private int[] maxArray(int[] nums, int len){
23         int[] res = new int[len];
24         int j = 0;
25         int n = nums.length;
26         for(int i=0; i<n;i++){
27             while(j>0&&j+n-i>len&&res[j-1]<nums[i]){
28                 j--;
29             }
30             if(j<len){
31                 res[j] = nums[i];
32                 j++;
33             }
34         }
35         return res;
36     }
37 
38     private int[] merge(int[] a, int[] b, int len){
39         int[] res = new int[len];
40         int len1 = a.length;
41         int len2 = b.length;
42         if(len1==0) return b;
43         if(len2==0) return a;
44         int i=0; int j=0;
45         int index=0;
46         while(i<len1||j<len2){
47             if(isGreater(a, i, b, j)){
48                 res[index++]=a[i++];
49             }else{
50                 res[index++]=b[j++];
51             }
52         }
53         while(i<len1){
54             res[index++]=a[i++];
55         }
56         while(j<len2){
57             res[index++]=b[j++];
58         }
59         return res;
60     }
61 
62     private boolean isGreater(int[] a, int posA, int[] b, int posB){
63         int len1 = a.length;
64         int len2 = b.length;
65         int i=posA; int j=posB;
66         while(i<len1&&j<len2&&a[i]==b[j]){
67             i++;
68             j++;
69         }
70         
71         if(i==len1) return false;
72         if(j==len2) return true;
73         return a[i]>b[j];
74     }
75 }

 

以上是关于Create Maximum Number的主要内容,如果未能解决你的问题,请参考以下文章

Create Maximum Number

[LeetCode] Create Maximum Number

[LeetCode] Create Maximum Number

LeetCode-Create Maximum Number

Leetcode 321: Create Maximum Number

[LintCode] Create Maximum Number 创建最大数