码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】Maximum Gap

时间:2015-05-16 20:03:02      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

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.

Credits:
Special thanks to @porker2008 for adding this problem and creating all test cases.

 

 1 class Solution {
 2 public:
 3     int maximumGap(vector<int>& nums) {
 4         if(nums.size()<2) return 0;
 5         sort(nums.begin(),nums.end());    
 6         int max=0;
 7         for(int i=1;i<nums.size();i++)
 8         {
 9             if(nums[i]-nums[i-1]>max)
10                 max=nums[i]-nums[i-1];
11         }
12         return max;
13 
14     }
15 };

 

【leetcode】Maximum Gap

标签:

原文地址:http://www.cnblogs.com/jawiezhu/p/4508293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!