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 ...
随机推荐
- Fiddler2汉化版使用说明
fiddler是一款免费且功能强大的数据包抓取软件,它能够快速的抓取HTTP会话以及支持监视.还可设置断点等诸多实用功能,非常适合计算机工作者们分析数据使用.本文就为大家详细介绍一下fiddler的功 ...
- 已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列
1.我的思路先将b链表连接在a链表的后面,这个很容易实现,将a链表最后的结点中的p.next改为指向b链表的头结点即可. 再将这个新链表用选择排序即可. 代码如下: #include<stdio ...
- Unity 3D 游戏上线之后的流水总结
原地址:http://tieba.baidu.com/p/2817057297?pn=1 首先.unity 灯光烘焙 :Unity 3D FBX模型导入.选项Model 不导入资源球.Rig 不导入骨 ...
- tcpkill清除异常tcp连接
tcpkill清除异常tcp连接 在linux系统中,遇到TCP链接迟迟不能释放的情况,类似FIN_WAIT1.FIN_WAIT2的状态,释放时间不确定,而且对应的程序已经关闭,相应的端口也不再监听, ...
- sshd_config配置 详解
原文:http://blog.licess.org/sshd_config/ # 1. 关于 SSH Server 的整体设定,包含使用的 port 啦,以及使用的密码演算方式 Port 22 # S ...
- C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\Team Foundation\4.0\Cache\VersionControl.config is not valid and cannot be loaded.
Recently, we experienced a strange problem with TFS 2010. We spent a few days before we figured it o ...
- HBase Java简单示例--转载
Hbase采用Java实现,原生客户端也是Java实现,其他语言需要通过thritf接口服务间接访问Hbase的数据. Hbase作为大数据存储数据库,其写能力非常强,加上Hbase本身就脱胎于Had ...
- POJ 1305 Fermat vs. Pythagoras (毕达哥拉斯三元组)
设不定方程:x^2+y^2=z^2若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组.若gcd(x,y,z)=1,则称为本原的毕达哥拉斯三元组. 定理:正整数x,y,z构成一个本原的毕达 ...
- I/O复用:异步聊天
一.I/O复用 在<TCP套接字编程>的同步聊天程序中,我们看到TCP客户同时处理两个输入:标准输入和TCP套接字.考虑在客户阻塞于标准输入fgets调用时,服务器进程被杀死,服务器TCP ...
- maven本地仓库.m2文件夹路径讲解
Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Depen ...