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

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. NodeJS - Express 3.0下ejs模板使用 partial展现 片段视图

    如果你也在看Node.js开发指南,如果你也在一步一步实现 microBlog 项目!也许你会遇到本文提到的问题,如果你用的是Express 3.0 本书实例背景是 Express 2.0 而如今升级 ...

  2. UI控件tag属性和魔法数字的处理

    说明:tag属性有很大的用处,它就好像每个UI控件的id,当多个按钮指向同一个监听方法时,可以给方法带参数UIButton,然后根据不同的tag值 来判断执行哪个按钮的监听事件: - (IBActio ...

  3. 复习linq

    复习linq linq的英文是language integrated query.其中query的意思就是疑问或者计算机用语就是从资料库中提取信息的要求,可以理解为查询的意思.那么它翻译过来的话就是集 ...

  4. adb 选择设备

    在adb中有多个设备时,可以先adb devices列举出设备,然后可以通过adb -s <设备名>  [其他参数] 对某个设备进行操作. 例如: adb -s 0123456789ABC ...

  5. 关于StringBuilder

    写在前面的话 很久没有更新博客了,来上海实习身边的一切波动挺大的,还好我走过来了,博客园:一路有你! StringBuilder 相信大家对StringBuilder类型一定不陌生,我们Coding经 ...

  6. VMware Workstation 10安装Centos6.4操作步骤说明

    1.在网上下载VMware Workstation 10, 百度软件中心助手安装程序高速下载,下载完成后默认是自动启动安装的,而原来的安装程序文件保存在:  C:\Users\用户名\Document ...

  7. 【C++基础】构造函数

    说说你对构造函数的理解? 构造函数:对象创建时,利用特定的值构造对象(不是构造类),将对象初始化(保证数据成员有初始值),是类的一个public 函数 ①   与类同名 ②   无返回值 ③   声明 ...

  8. Chp11: Sorting and Searching

    Common Sorting Algo: Bubble Sort: Runime: O(n2) average and worst case. Memory: O(1). void BubbleSor ...

  9. hdu 4699 Editor 模拟栈

    思路:刚开始用STL中的栈,一直RE……,之后改为手动模拟栈操作,在注意点细节就可以了!!! 代码如下: #include<cstdio> #include<cstring> ...

  10. hdu 1309 Loansome Car Buyer

    纯粹的阅读理解题………… ;}