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)
 
 public class Solution {
/**
* @param nums1 an integer array of length m with digits 0-9
* @param nums2 an integer array of length n with digits 0-9
* @param k an integer and k <= m + n
* @return an integer array
*/
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
// Write your code here
int len1 = nums1.length;
int len2 = nums2.length;
int[] res = new int[k];
for(int i = Math.max(0, k-len2); i<=k&&i<=len1;i++){
int[] temp = merge(maxArray(nums1, i), maxArray(nums2, k-i), k);
if(isGreater(temp, 0, res, 0)){
res = temp;
}
}
return res;
} private int[] maxArray(int[] nums, int len){
int[] res = new int[len];
int j = 0;
int n = nums.length;
for(int i=0; i<n;i++){
while(j>0&&j+n-i>len&&res[j-1]<nums[i]){
j--;
}
if(j<len){
res[j] = nums[i];
j++;
}
}
return res;
} private int[] merge(int[] a, int[] b, int len){
int[] res = new int[len];
int len1 = a.length;
int len2 = b.length;
if(len1==0) return b;
if(len2==0) return a;
int i=0; int j=0;
int index=0;
while(i<len1||j<len2){
if(isGreater(a, i, b, j)){
res[index++]=a[i++];
}else{
res[index++]=b[j++];
}
}
while(i<len1){
res[index++]=a[i++];
}
while(j<len2){
res[index++]=b[j++];
}
return res;
} private boolean isGreater(int[] a, int posA, int[] b, int posB){
int len1 = a.length;
int len2 = b.length;
int i=posA; int j=posB;
while(i<len1&&j<len2&&a[i]==b[j]){
i++;
j++;
} if(i==len1) return false;
if(j==len2) return true;
return a[i]>b[j];
}
}

Create Maximum Number的更多相关文章

  1. [LintCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  2. 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  3. 321. Create Maximum Number 解题方法详解

    321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...

  4. 321. Create Maximum Number

    /* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...

  5. leetcode 402. Remove K Digits 、321. Create Maximum Number

    402. Remove K Digits https://www.cnblogs.com/grandyang/p/5883736.html https://blog.csdn.net/fuxuemin ...

  6. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  7. Leetcode: Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  8. [Swift]LeetCode321. 拼接最大数 | Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  9. 321. Create Maximum Number (c++ ——> lexicographical_compare)

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

随机推荐

  1. gitlab搭建

    环境说明: 虚拟机 centos 7 64位 内存:4GB 存储:100GB CPU: CORE 版本: gitlab 10.0.3 配置系统基础环境 配置阿里巴巴 yum 源 wget -O /et ...

  2. 7-27 Codeforces Round #499 (Div. 2)

    C. Fly 链接:http://codeforces.com/group/1EzrFFyOc0/contest/1011/problem/C 题型:binary search .math. 题意:总 ...

  3. HTML和CSS怎么用

    首页> 1.HTML和CSS是什么? ·网站和HTML页面 ·简单理解网站 ·一个房子比喻(HTML比喻成房子,CSS为装修) ·页面的整体结构:有树桩标签对嵌套组成 ·页面的组成单元:元素 · ...

  4. 《内蒙古自治区第十三届大学生程序设计竞赛试题_H 公孙玉龙》

    这个题有点小坑,最坑的地方就是  输入的b 变量  有可能 是  负数  !   负数 !  负数!    对 ,你没有看错,就是负数,坑死我了, 一直都是  content.charAt(0) 判断 ...

  5. company_credit

    /** * Created by wu-yj on 2016/5/6. */ import java.sql.{Connection, DriverManager, PreparedStatement ...

  6. 剑指offer 12:数值的整数次方

    题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. public class Solution { public double P ...

  7. 关于两栏布局,三栏布局,一级点击三角触发select的onchange事件问题

    首先看这样一个效果:,这个截图来自移动端的列表的一整行,在这个效果当中,存在两个技术点,首先选择祝福卡这个宽度是一定的,右边的部分,宽度随着手机屏幕的宽度而自适应,再一个技术点就是点击最右侧向下箭头, ...

  8. spring xml配置注入改为手动注入过程

    项目中需要使用MQ组件来接受消息,但是有的时候,在使用的时候,并不能满足spring注入的条件,无法注入.例如 在jfinal的config的afterJFinalStart中,由于jfinal集成s ...

  9. Linux系统时间, 硬件BIOS时间的校准与同步

    #宿主机系统时间: date #宿主机时间校准方法: yum install -y ntpdate ntpdate -u ntp.api.bz #centos查看设置系统时区:(可能没有) cat / ...

  10. windows,用c++调用mxnet做前向

    参考博客: https://blog.csdn.net/qq_34062105/article/details/82590553 https://blog.csdn.net/u012234115/ar ...