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

LeetCode:164. Maximum Gap

时间:2019-01-06 22:29:01      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:long   nbsp   一个   turn   排序   for   标签   nts   code   

这道题比较简单,虽然不知道为什么被贴上了困难的标签~

贴上题目:

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
Return 0 if the array contains less than 2 elements.

中文翻译:

现在有一个无序数组,找出数组在排序后,相邻元素差值的最大值

如果元素个数少于两个,就返回0.

emmmmm~

思路很暴力:排序,求差值。

上代码:

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

 AC,击败88%的代码。

LeetCode:164. Maximum Gap

标签:long   nbsp   一个   turn   排序   for   标签   nts   code   

原文地址:https://www.cnblogs.com/Scotton-Wild/p/10230663.html

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