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.
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]
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的更多相关文章
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- 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 ...
- 321. Create Maximum Number 解题方法详解
321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...
- 321. Create Maximum Number
/* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...
- 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 ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- Leetcode: Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [Swift]LeetCode321. 拼接最大数 | Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- 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 ...
随机推荐
- android开发_文本按钮 与 输入框
1 TextView: 属性与值 android:text="文本" android:textSize="20sp" //sp为 ...
- html2canvas文字重叠(手机端)
发现情况: 1.设置文字居中,文字自动换行后文字有重叠 text-align: center; 解决办法: text-align: left; text-align: justify;等 2.使用 ...
- Linux 文件内容查看(cat、tac、nl 、more 、less、head、tail )
Linux系统中使用以下命令来查看文件的内容: cat: 由第一行开始显示文件内容tac :从最后一行开始显示,可以看出 tac 是 cat 的倒著写!nl: 显示的时候,顺道输出行号!more ...
- Vivado HLS 工具
干什么的 Vivado HLS工具可以将C语言高级综合为硬件. 为什么要使用HLS 可以在更高的抽象层次描述功能,而不是在传统的RTL级别 一个潜在的用处是,系统设计划分成硬件部分和软件部分之后,软件 ...
- JavaScript形而上的For循环中的Break
break相当于循环中的GOTO,需避免使用. 下面是一个break使用例子. 找出第一个months小于7的项目. const cats = [ { name: 'Mojo', months: 84 ...
- 项目中使用的artTemplate笔记
1.注意数据格式为 var results = { data:[ {name:'xiaoming',age:'18'},{name:'xiaohong',age:'18'},{name:'xiaogo ...
- Win10提示“因为文件共享不安全,所以你不能连接到文件共享”如何处理
在使用Windows10 1803版本系统连接CentOS6.5下搭建的Samba服务时,发现打开共享文件会遇到以下提示: 其实,该问题是Win10版本不兼容导致的.微软官方说明:https://go ...
- [python]Git
Git 修改默认编辑器 git config –global core.editor vim 提交发生变化得文件 # 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted ...
- Mysql 更新时间(加上或者减去一段时间)
Mysql时间加减函数为date_add().date_sub() 定义和用法 DATE_ADD() 函数向日期添加指定的时间间隔. DATE_SUB() 函数向日期减少指定的时间间隔. 语法 DAT ...
- [GXOI/GZOI2019]旅行者
就我感觉这道题很神仙吗/kel 仔细想想应该也是一种适用范围挺广的做法. 考虑我们可以通过dijkstra在O(nlogn)求出一个点集到另外一个点集的最短路. 那么我们可以通过一些划分点集的方式使得 ...