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 ...
随机推荐
- HDU 1316 How Many Fibs?(java,简单题,大数)
题目 /** * compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1: public int compareTo(BigInteger val) 将此 BigInteg ...
- POJ 3685
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 4428 Accepted: 1102 Descriptio ...
- nested pop animation can result in corrupted navigation bar
nested pop animation can result in corrupted navigation barFinishing up a navigation transition in a ...
- Android 内存剖析 – 发现潜在问题
简介 移动平台上的开发和内存管理紧密相关.尽管随着科技的进步,现今移动设备上的内存大小已经达到了低端桌面设备的水平,但是现今开发的应用程序对内存的需求也在同步增长.主要问题出在设备的屏幕尺寸上-分辨率 ...
- UVA 562 Dividing coins
题目描述:给出一些不同面值的硬币,每个硬币只有一个.将这些硬币分成两堆,并且两堆硬币的面值和尽可能接近. 分析:将所有能够取到的面值数标记出来,然后选择最接近sum/2的两个面值 状态表示:d[j]表 ...
- Android Source Code
一. Android 框架 http://elinux.org/Master-android Android框架层级 : Android 自下 而 上 分为 4层; -- Linux内核层; -- 各 ...
- Java框架----SSH整合回顾
1,新建工程,类型为Web Project,设置默认编码为UTF-8,并创建如下文件夹 1,Source Folder 1,src 项目源码 2,config 配置文件 3,test 单元测试 2,普 ...
- spring_150802_resource
接口Service: package com.spring.service; public interface DogPetService { public void queryAllDogPets( ...
- 叠罗汉I
叠罗汉是一个著名的游戏,游戏中一个人要站在另一个人的肩膀上.同时我们应该让下面的人比上面的人更高一点.已知参加游戏的每个人的身高,请编写代码计算通过选择参与游戏的人,我们多能叠多少个人.注意这里的人都 ...
- Android 核心分析 之七Service深入分析
Service深入分析 上一章我们分析了Android IPC架构,知道了Android服务构建的一些基本理念和原理,本章我们将深入分析Android的服务.Android体系架构中三种意义上服务: ...