leetcode 321 Create Max Number
leetcode 321 Create Max Number
greedy的方法,由于有两个数组,我们很自然的想到从数组1中选i个数,数组2中选k-i个数,这样我们只需要遍历max(0, k-数组2长度n) ~ 数组1长度,然后保存合并i和k-i这两个部分之后得到的最大值即可。 那么还剩下这几个问题:
1) 从一个数组中不改变元素顺序地选出i个元素,使他们顺序排列代表的十进制数最大。例:[1,4,3,6,2], i = 3, 结果为[4,6,2]
2) 如何合并数组1的i个元素和数组2的k-i个元素
3) 比较两个数组所代表的十进制数的大小
第一个问题,我们需要使用栈的概念,遍历数组,每次比较栈顶元素与当前元素i,若i比栈顶元素大,则pop栈顶,继续比较,直到栈空或者i比栈顶元素小。注意,我们还需要时刻检测,i之后的元素是否足够补上pop出栈的空缺,若不够则不能再pop。例:[1,4,3,6,2], 当index为3时,栈中有[4,3],6和3比,大于,弹出3,继续比4,大于,这时候却不能弹出4,因为6后面只有一个数,不够补上pop2次的空缺。
第二个问题,分别用两个index i, j指向数组1和2,比较他们指向的元素,将大的元素放入新的大小为k的数组。注意,如何比较呢?可能指向元素相等,可能某一个数组已经到头了。详细过程在第三个问题列出
第三个问题,如何比较?本题有两个地方需要使用比较,第一个就是主函数中比较不同i值导致的不同长度为k的数组,这个较为简单,因为两个数组长度一致,只需要用两个index,i,j分别遍历两个数组即可,返回nums1[i]和nums2[j]中较大的那一个,若相等则i++,j++,若i=j=k-1还是相等,说明两个数组代表的十进制数相同,返回哪一个都可以。第二个地方就是上一个问题中提到的,比较两个index指向元素的大小,一部分与之前解法相同,元素相等就继续比较后面的元素,不过有一点需要区别的,这里的两个数组可能是不同长度的,所以有可能i或j中某一个指向的位置没有数组元素,即这个数组已经到头了,这种情况,显然就可以返回另一个数组的index指向的元素。
代码如下:
class Solution {
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
int len1 = nums1.length, len2 = nums2.length;
int[] res = new int[k];
for(int i=0; i<=k; i++){
if((k - i) <= len2 && i <= len1){
int[] tmp = new int[k];
tmp = merge(singleNum(nums1, i), singleNum(nums2, k-i));
if(compare(tmp, 0, res, 0))
res = tmp;
}
}
return res;
}
public int[] singleNum(int[] nums, int k){
int len = nums.length;
int[] res = new int[k];
int j = 0;
for(int i=0; i<len; i++){
while(len-i+j > k && j > 0 && res[j-1] < nums[i])
j--;
if(j < k)
res[j++] = nums[i];
}
return res;
}
public int[] merge(int[] nums1, int[] nums2){
int k = nums1.length + nums2.length;
int[] res = new int[k];
for(int i=0, j=0, idx = 0; idx < k; idx++){
res[idx] = compare(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
}
return res;
}
private boolean compare(int[] nums1, int idx1, int[] nums2, int idx2){
while(idx1 < nums1.length && idx2 < nums2.length && nums1[idx1] == nums2[idx2]){
idx1++;
idx2++;
}
return idx2 == nums2.length || (idx1 < nums1.length && nums1[idx1] > nums2[idx2]);
}
}
leetcode 321 Create Max Number的更多相关文章
- [LeetCode] 321. Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
- 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 ...
- 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[] ...
- 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 ...
- 321 Create Maximum Number 拼接最大数
已知长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,直观地表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中 ...
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
随机推荐
- c# 中反射里的invoke方法的参数
一个最简单的C#反射实例,首先编写类库如下: namespace ReflectionTest { public class WriteTest { //带参数的公共方法 public void Wr ...
- 【珍惜时间】vuepro
老规矩放上大大的github开源地址:https://github.com/goodheart222/vuepro 我们再来看看项目的效果,初步根据效果做到心中有数 看到效果的话,我们会发现,肯定是有 ...
- 莫烦pytorch学习笔记(一)——torch or numpy
Q1:什么是神经网络? Q2:torch vs numpy Numpy:NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(neste ...
- wget: command not found 解决方案
wget: command not found 解决方案 wget command not found 解决方案 问题分析 解决方案 方法一yum安装wget 方法二rpm安装 问题分析 安装的是Ce ...
- canvas石英钟
canvas石英钟:demo <!DOCTYPE html> <html> <head lang="en"> <meta charset= ...
- centos 7 安装dotnet core
dotnetcore 支持的Linux 版本请看官网:https://docs.microsoft.com/zh-cn/dotnet/core/linux-prerequisites?tabs=net ...
- 34 N皇后问题Ⅱ
原题网址:https://www.lintcode.com/zh-cn/old/problem/n-queens-ii/ 34. N皇后问题 II 描述 笔记 数据 评测 讨论区 根据n皇后问题, ...
- 转:五种I/O模型和select函数简介
源地址:http://blog.csdn.net/jnu_simba/article/details/9070955 一.五种I/O模型 1.阻塞I/O 我们在前面所说的I/O模型都是阻塞I/O,即调 ...
- Java(8)中List的遍历方式总结
本篇文章主要讲述了List这一集合类型在Java,包括Java8中的遍历方式,不包括其他的过滤,筛选等操作,这些操作将会在以后的文章中得到提现,由List可以类推到Set等类似集合的遍历方式. pub ...
- Nmap扫描原理(下)
转自:https://blog.csdn.net/qq_34398519/article/details/89055999 3 Nmap高级用法 3.1 防火墙/IDS规避 防火墙与ID ...