下面的分桶个数做的不太好,原来的解法是用的

int gap = (big - small) / vlen;
if (gap == 0) {
gap = 1;
}

下面是现在的Java解法:

package com.company;

import java.util.*;

class Solution {
public int maximumGap(int[] nums) {
if (nums.length < 2) {
return 0;
} // 用 Radix 排序
int small = Integer.MAX_VALUE;
int big = 0;
for (int num : nums) {
if (num < small) {
small = num;
}
if (num > big) {
big = num;
}
}
System.out.println("big is " + big + " small " + small);
int gap = (big - small - 1) / (nums.length - 1) + 1;
if (gap == 0) {
return 0;
}
int gap_num = (big - small) / gap + 1;
int[] first = new int[gap_num];
int[] second = new int[gap_num];
// [ )
System.out.println("gap is " + gap + " len is " + nums.length + "big is " + big + " small " + small);
for (int num : nums) {
int index = (num - small) / gap;
if (first[index] == 0 || num < first[index]) {
first[index] = num;
}
if (second[index] == 0 || num > second[index]) {
second[index] = num;
}
}
int ret = -1;
int last = -1;
for (int i=0; i<gap_num; i++) {
if (first[i] == 0) {
continue;
}
if (last == -1) {
last = second[i];
ret = last - first[i];
}
else {
if (first[i] - last > ret) {
ret = first[i] - last;
}
if (second[i] - first[i] > ret) {
ret = second[i] - first[i];
}
last = second[i];
} }
return ret;
}
} public class Main { public static void main(String[] args) {
System.out.println("Hello!");
Solution solution = new Solution(); int[] nums = {1,1,1,1,1,5,5,5,5,5}; int ret = solution.maximumGap(nums);
System.out.printf("Get ret: %s\n", ret); /*Iterator<List<Integer>> iterator = ret.iterator();
while (iterator.hasNext()) {
Iterator iter = iterator.next().iterator();
while (iter.hasNext()) {
System.out.printf("%d,", iter.next());
}
System.out.println();
}*/ System.out.println(); }
}

maximum-gap(经过了提示)的更多相关文章

  1. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  2. [LintCode] Maximum Gap 求最大间距

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  3. 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)

    前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...

  4. Maximum Gap

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  5. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

  6. 【leetcode 桶排序】Maximum Gap

    1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...

  7. 【LeetCode】164. Maximum Gap (2 solutions)

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  8. 由Maximum Gap,对话桶排序,基数排序和统计排序

    一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...

  9. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  10. 【刷题-LeetCode】164 Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

随机推荐

  1. C#加密NodeJS解密

    C#代码: class Program { static void Main(string[] args) { Console.WriteLine(", "abcdefghijkl ...

  2. swift 类 与 结构体

    这两天突然有人问我  swift里面 类和 结构体  有什么区别? 说实在的本人目前不太看好swift,相信很多人也是,oc 都 很成熟了. 本人目前不打算深入了解swift的原因swift  语言 ...

  3. SCRUM团队的三个角色

    Scrum团队中包括三个角色,他们分别是产品负责人.开发团队和 Scrum Master. Scrum 团队是自组织.跨职能的完整团队.自组织团队决定如何最好地完成他们的工作,而不是由团队外的其他人来 ...

  4. For和While在C和MATLAB中的区别——MATLAB的大坑

    For和while是常见的循环关键字,在许多语言中都是通用的.但是想必不是所有人,都被其中的区别困扰过,尤其是MATLAB“程序员”. x=[,,,,,,]; i=; while i<=leng ...

  5. EXT4.2--Ext Designer 使用

    前言: “画EXT”是一个美好的想法,如果有一款可视化工具能够只需进行拖拽而设计EXT,生成代码--那真是一件美丽的事.然而现实是,即使是为Eclipse装上EXT插件,用上idea,手写代码的提示也 ...

  6. 为什么数据可以从pl/sql查出来而使用ado.net查询,结果却是空?

    1.背景 一条记录(如select * from A where a='1'),使用pl/sql作为条件可以查询出记录,但用ado.net sql查询结果却是空. 2.原因 a字段的数据类型的char ...

  7. Unity3D脚本中文系列教程(十六)

    Unity3D脚本中文系列教程(十五) ◆ function OnPostprocessAudio (clip:AudioClip):void 描述:◆  function OnPostprocess ...

  8. node.js 安装、图文详解

    网上的教程很多,有些模糊不清,有些版本太旧,有些是.exe安装,本文讲述windows系统下简单nodejs .msi环境配置.最新版是Current version: v0.10.26.官网下载地址 ...

  9. lintcode 中等题:A + B Problem A + B 问题

    题目: 中等 A + B 问题 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 如果 a=1 并且 b=2,返回3 注意 你不需要从输入流读入数据,只需要根据aplusb的两个参数 ...

  10. 在mybatis执行SQL语句之前进行拦击处理

    转载自:http://blog.csdn.net/hfmbook/article/details/41985853 比较适用于在分页时候进行拦截.对分页的SQL语句通过封装处理,处理成不同的分页sql ...