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. web访问速度优化分析

    请求从发出到接收完成一共经历了DNS Lookup.Connecting.Blocking.Sending.Waiting和Receiving六个阶段,时间共计38ms.请求完成之后是DOM加载和页面 ...

  2. JavaScript js 精确、保留小数方法

    //保留两位小数 //功能:将浮点数四舍五入,取小数点后2位 function toDecimal(x) { var f = parseFloat(x); if (isNaN(f)) { return ...

  3. React Native 简介:用 JavaScript 搭建 iOS 应用 (1)

    [编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...

  4. 评论 “App死亡潮:400万应用僵尸超八成,周期仅10月”

    点这里 原文: App死亡潮:400万应用僵尸超八成,周期仅10月 时间 2015-04-05 22:48:19  和讯科技相似文章 (16)原文  http://tech.hexun.com/201 ...

  5. Install WindowBuilder for Eclipse

    WindowBuilder官方下载安装说明地址:http://www.eclipse.org/windowbuilder/download.php 先祝各位能顺利安装上!以下是基于Eclipse in ...

  6. ExtJs之Ext.query

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  7. adt导入已经存在于workspace中的项目

    场景: Eclipse中某android项目被delete,但是并未勾选“delete project contents from disk(cannot be undone)”.删除后,下次再想打开 ...

  8. hdu 3590 PP and QQ

    知识储备: Anti-SG 游戏和 SJ 定理  [定义](anti-nim 游戏)  桌子上有 N 堆石子,游戏者轮流取石子.  每次只能从一堆中取出任意数目的石子,但不能不取.  取走最后一 ...

  9. 性能标准:Apdex介绍

    目前的应用程序性能测试工具有多方面的局限.每种工具都有其自己对性能的定义,产生太多混乱或矛盾的数值,并缺少简洁的结论.IT经理们无法深入了解性能,而这是软件与用户对关键应用的体验有关的事. 听云作为新 ...

  10. Hibernate笔记——hql总结

    原文:http://www.cnblogs.com/xiaoluo501395377/p/3376256.html ------------------------------------------ ...