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(难)的更多相关文章

  1. [LintCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  2. 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 ...

  3. 321. Create Maximum Number 解题方法详解

    321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...

  4. 321. Create Maximum Number

    /* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...

  5. 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 ...

  6. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  7. Leetcode: Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  8. [Swift]LeetCode321. 拼接最大数 | Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  9. Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  10. 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 ...

随机推荐

  1. Swift-1-基本概念

    // Playground - noun: a place where people can play // 通过代码快速了解swift常用知识,需要一定object-c基础 import UIKit ...

  2. pthread_barrier_init,pthread_barrier_wait简介

    pthread_barrier 系列函数在<pthread.h>中定义,用于多线程的同步,它包含三个函数: --pthread_barrier_init() --pthread_barri ...

  3. 动态修改 NodeJS 程序中的变量值

    如果一个 NodeJS 进程正在运行,有办法修改程序中的变量值么?答案是:通过 V8 的 Debugger 接口可以!本文将详细介绍实现步骤. 启动一个 HTTP Server 用简单的 Hello ...

  4. 改写Form的submit

    表单的一些应用常识: 1.在用户第一次提交完表单后应防止用户不耐烦而多次点击submit按钮,需要在onsubmit事件中制止用户的重复行为. 2.不要简单粗暴的用reset()重置表单,如果用户不想 ...

  5. Activity学习(四)——简单切换

    理论学习Activity之后,我们就来具体的实战,Activity之间相互切换依靠的是“ 意图 ”(Intent),这个 Intent 包含了要跳转到的Activity的一些信息,因为Activity ...

  6. Linux多线程之同步2 —— 生产者消费者模型

    思路 生产者和消费者(互斥与同步).资源用队列模拟(要上锁,一个时间只能有一个线程操作队列). m个生产者.拿到锁,且产品不满,才能生产.当产品满,则等待,等待消费者唤醒.当产品由空到不空,通知消费者 ...

  7. mysql 死锁检查

    今天看了一篇关于死锁检查的blog. Advanced InnoDB Deadlock Troubleshooting – What SHOW INNODB STATUS Doesn’t Tell Y ...

  8. Gradle Goodness: Using and Working with Gradle Version

    To get the current Gradle version we can use the gradleVersion property of the Gradle object. This r ...

  9. JSTL标签库中fmt标签,日期,数字的格式化

    首先介绍日期的格式化:(不要嫌多哦) JSTL格式化日期(本地化) 类似于数字和货币格式化,本地化环境还会影响生成日期和时间的方式. <%@ page pageEncoding="UT ...

  10. Linux下find一次查找多个指定类型文件,指定文件或者排除某类文件,在 GREP 中匹配多个关键 批量修改文件名等

    http://blog.sina.com.cn/s/blog_62e7fe670101dg9d.html linux下二进制文件查找: strings 0000.ts | grep -o " ...