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 https://blog.csdn.net/cmershen/article/details/51550220

Leetcode 164 Maximum Gap 桶排序好题

Given an unsorted array, find the maximum difference between the successive elements in its sorted f...
  • u012614906
  • u012614906
  • 2017-01-05 21:56:14
  • 1674

Maximum Gap (Java)

Given an unsorted array, find the maximum difference between the successive elements in its sorted f...
  • tofu_jelly
  • tofu_jelly
  • 2015-01-31 10:32:02
  • 1037

LeetCode(164) Maximum Gap

题目如下: Given an unsorted array, find the maximum difference between the successive elements in its so...
  • feliciafay
  • feliciafay
  • 2015-01-02 10:59:16
  • 4094

[LeetCode]164.Maximum Gap

题目Given an unsorted array, find the maximum difference between the successive elements in its sorted...
  • SunnyYoona
  • SunnyYoona
  • 2015-03-23 22:44:39
  • 1829

[leetcode-164]Maximum Gap(c)

问题描述: Given an unsorted array, find the maximum difference between the successive elements in its s...
  • zdavb
  • zdavb
  • 2015-08-21 11:38:03
  • 601

Leetcode 164 Maximum Gap (最大差距)

一,问题描述1,给一个没有排序的数组,当排序后找出连续两个元素之间相差最大的数,并且输出这个数。2,限制条件: 需要在线性的时间和空间复杂度。如果数组长度小于2,则输出0。数组中的所有数为非负...
  • wodedipang_
  • wodedipang_
  • 2017-03-18 21:51:35
  • 271

LeetCode 164. Maximum Gap

如果想在O(n)的时间复杂度完成这道问题,需要用到桶排序的思想。学习过后,完成了这个问题。 这道问题需要找到排序之后相邻的数字的差值的最大值。所以排序是一定的,那么桶排序是少有的能在O(n)的时间复...
  • liu_460660596
  • liu_460660596
  • 2016-04-12 12:21:44
  • 198

[leetcode] 164. Maximum Gap

题目链接:https://leetcode.com/problems/maximum-gap/ 题意:给定一个无序数组,求有序情况下相邻元素差最大为多少? 要求:时间复杂度和空间复杂度均为O(n)...
  • wrm_nancy
  • wrm_nancy
  • 2016-09-24 21:33:36
  • 326

[LeetCode]164. Maximum Gap

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

leetcode 164. Maximum Gap

164. Maximum GapGiven an unsorted array, find the maximum difference between the successive elements...
  • liqinzhe11
  • liqinzhe11
  • 2017-11-28 10:14:53
  • 81
收藏助手
不良信息举报
您举报文章:Hard-题目16:164. Maximum Gap
举报原因:
原因补充:

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