zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    传奇检测命令大全(常用命令)
    传奇版本中利用NPC迅速给人物加血脚本制作
    传奇泡点地图制作脚本
    双击包裹物品自动解包设置方法_传奇版本技术
    传奇地图事件触发脚本
    单机架设传奇服务器第47课:定时器OnTimer功能详解
    单机架设传奇服务器:机器人运行脚本文件
    检测某个地图某个怪物的数量的脚本
    检测当前人物是否在安全区脚本命令
    假人配置说明
  • 原文地址:https://www.cnblogs.com/xinqiwm2010/p/6836093.html
Copyright © 2011-2022 走看看