create-maximum-number(难)
https://leetcode.com/problems/create-maximum-number/
这道题目太难了,花了我很多时间。最后还是参考了别人的方法。还少加了个greater方法。很难。
package com.company; import java.util.*; // https://discuss.leetcode.com/topic/32272/share-my-greedy-solution/2 class Solution {
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
int[] ret = new int[k];
if (nums1.length + nums2.length < k) {
return ret;
} for (int i=Math.max(0, k-nums2.length); i<=k && i<= nums1.length; i++) {
int[] tmp = merge(maxArr(nums1, i), maxArr(nums2, k-i)); if (greater(tmp, 0, ret, 0)) {
ret = tmp;
} }
return ret;
} // 必须要有greater函数
boolean greater(int[] nums1, int i, int[] nums2, int j) {
while (i < nums1.length && j < nums2.length && nums1[i] == nums2[j]) {
i++;
j++;
}
return j == nums2.length || (i < nums1.length && nums1[i] > nums2[j]);
} int[] merge(int[] nums1, int[] nums2) {
int[] ret = new int[nums1.length + nums2.length]; int t1 = 0;
int t2 = 0;
for (int i=0; i<ret.length; i++) {
if (greater(nums1, t1, nums2, t2)) {
ret[i] = nums1[t1++];
}
else {
ret[i] = nums2[t2++];
}
} return ret;
} int[] maxArr(int[] nums, int k) {
int[] ret = new int[k];
int j = 0;
int len = nums.length;
for (int i=0; i<len; i++) {
while (j>0 && j+len-i>k && ret[j-1] < nums[i]) {
j--;
}
if (j < k) {
ret[j] = nums[i];
j++;
}
} return ret;
} } public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
int[] nums1 = {6, 7};
int[] nums2 = {6, 0, 4};
int[] ret = solution.maxNumber(nums1, nums2, 5);
for (int i=0; i<ret.length; i++) {
System.out.printf("ret:%d\n", ret[i]);
}
System.out.println(); } }
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 ...
- 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 ...
随机推荐
- API文档管理工具-数据库表结构思考.
API文档管理工具-数据库表结构思考. PS: 管理工具只是为了方便自己记录API的一些基本信息,方便不同的开发人员 (App Developer, Restful API Developer)之间的 ...
- 关于gzip压缩
关于gzip压缩 http://httpd.apache.org/docs/2.0/mod/mod_deflate.html http://www.phpchina.com/resource/manu ...
- HOWTO: Create native-looking iPhone/iPad applications from HTML, CSS and JavaScript
HOWTO: Create native-looking iPhone/iPad applications from HTML, CSS and JavaScript Though it's not ...
- 彻底理解js中this的指向
首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...
- 13test06:花朵数
#include<iostream> using namespace std; #define N a//定义宏时后面不加:否则会把':'一起定义为宏. int getP(int,int) ...
- Smarty模板学习
1.基本语法 所有的smarty模板标签都被加上了定界符.默认情况下是 { 和},但它们是可被改变的. 在smarty里,所有定界符以外的内容都是静态输出的,或者称之为不可改变.当smarty遇到了模 ...
- java基础知识回顾之接口
/* abstract class AbsDemo { abstract void show1(); abstract void show2(); } 当一个抽象类中的方法都是抽象的时候,这时可以将该 ...
- (10)nehe教程4--旋转
旋转: 在这一课里,我将教会你如何旋转三角形和四边形.左图中的三角形沿Y轴旋转,四边形沿着X轴旋转. 上一课中我教给您三角形和四边形的着色.这一课我将教您如何将这些彩色对象绕着坐标轴旋转.其实只需在上 ...
- BFS+贪心 HDOJ 5335 Walk Out
题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...
- LevelDB系列之整体架构
LevelDb本质上是一套存储系统以及在这套存储系统上提供的一些操作接口.为了便于理解整个系统及其处理流程,我们可以从两个不同的角度来看待LevleDb:静态角度和动态角度.从静态角度,可以假想整个系 ...