Hard-题目16:164. Maximum Gap

原创 2016年05月31日 23:31:47

题目原文:
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.

Try to solve it in linear time/space.

Return 0 if the array contains less than 2 elements.

You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
题目大意:
给出一个无序的数组,求出排序后相邻两个元素差的最大值。如果数组长度小于2,则返回0.
题目分析:
懒得做了,直接按题面要求做。
源码:(language:java)

public class Solution {
    public int maximumGap(int[] nums) {
        if(nums.length<2)
            return 0;
        Arrays.sort(nums);
        int maxgap = Integer.MIN_VALUE;
        for(int i = 1;i<nums.length;i++) {
            if(nums[i]-nums[i-1]>maxgap)
                maxgap=nums[i]-nums[i-1];
        }
        return maxgap;
    }
}

成绩:
4ms,beats 92.15%,众数6ms,27.29%
Cmershen的碎碎念:
不知道leetcode后台的test case怎么设计的,有很多题用朴素解法比绞尽脑汁想出的低复杂度算法还要快。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

leetcode 164. Maximum Gap 最大间隔 + 一个很好的桶排序示范

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

leetcode164~Maximum Gap

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

164. Maximum Gap【H】【80】【桶排序】【VIP】

Given an unsorted array, find the maximum difference between the successive elements in its sorted f...
  • sscssz
  • sscssz
  • 2016年05月13日 21:55
  • 398

[LeetCode164] Maximum Gap

Given an unsorted array, find the maximum difference between the successive elements in its sorted f...
  • sbitswc
  • sbitswc
  • 2015年08月15日 13:33
  • 1078

LeetCode #164 Maximum Gap

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

164. Maximum Gap

Given an array nums, there is a sliding window of size k which is moving from the very left of the a...

164. Maximum Gap

问题描述Given an unsorted array, find the maximum difference between the successive elements in its sort...

[LeetCode]164. Maximum Gap

原题链接:https://leetcode.com/problems/maximum-gap/ 这道题如果是O(nlgn)方法肯定很简单,排序后找最大gap就行了。但如果要求O(n)时间完成的话,难...

[LeetCode]problem 164. Maximum Gap

TAG桶排序浮点数不精确link方法一眼看过去完全不知道该怎么做。看了题解觉得很高端的样子,再后来多看了下,发现本质其实就是一个线性时间排序的问题!线性时间排序,有计数排序、桶排序、基数排序。计数排序...

[164]Maximum Gap

Given an unsorted array, find the maximum difference between the successive elements in its sorted f...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目16:164. Maximum Gap
举报原因:
原因补充:

(最多只允许输入30个字)