164. Maximum Gap

1. 题目

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.
Example 1:
Input: [3,6,9,1]
Output: 3
Explanation: The sorted form of the array is [1,3,6,9], either
(3,6) or (6,9) has the maximum difference 3.
Example 2:
Input: [10]
Output: 0
Explanation: The array contains less than 2 elements, therefore return 0.

2. 题目分析
一开始看错题目了,还以为是排序后,找某个差值数量最多的那个(英语渣的悲剧),后面一直提交发现都有用例过不去,然后重新读题目,发现只要找差值最大的那个,这不是so easy吗?不明白为什么这题也是middle难度。

3. 解题思路
先排序,然后遍历找最大差值。

4. 代码实现(java)

package com.algorithm.leetcode.sort;import java.util.Arrays;/*** Created by 凌 on 2019/1/19.* 注释:164. Maximum Gap*/
public class MaximumGap {public static void main(String[] args) {//        int[] nums = {3,6,9,1};int[] nums = {15252,16764,27963,7817,26155,20757,3478,22602,20404,6739,16790,10588,16521,6644,20880,15632,27078,25463,20124,15728,30042,16604,17223,4388,23646,32683,23688,12439,30630,3895,7926,22101,32406,21540,31799,3768,26679,21799,23740};int count = maximumGap(nums);System.out.println(count);for (int i = 0; i < nums.length; i++) {System.out.printf("%d \t",nums[i]);}}public static int maximumGap(int[] nums) {if (nums == null || nums.length <2){return 0;}Arrays.sort(nums);int difference = 0;for (int i = 1; i < nums.length; i++) {if (difference < nums[i] - nums[i-1]){difference=nums[i] - nums[i-1];}}return difference;}
}

164. Maximum Gap 1相关推荐

  1. 164. Maximum Gap

    题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...

  2. leetcode 164. Maximum Gap | 164. 最大间距(桶排序)

    题目 https://leetcode.com/problems/maximum-gap/ 题解 桶排序,用数组模拟桶 class Solution {public int maximumGap(in ...

  3. kali linux 2019教程,[教程]KALI LINUX 2.0 2019 更新国内源

    2019年最新版本KALI 为 KALI 2019.1 下载地址:https://www.kali.org/downloads/ 有的新入门的朋友可能会问,为什么每次都无法手动更新 例如:Update ...

  4. LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本

    1. Array (1) 27. Remove Element(Easy) 给定数组nums和值val,在适当位置删除该值的所有实例并返回新长度. 思路: 不需要使用排序,如果等于该值,则将n-1的值 ...

  5. LeetCode github集合,附CMU大神整理笔记

    Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...

  6. LeetCode解题报告汇总

    LeetCode解题报告: [LeetCode]1.Two Sum - Yoona - 博客频道 - CSDN.NET [LeetCode]2.Add Two Numbers - Yoona - 博客 ...

  7. LEETCODE-刷题个人笔记 Python(1-400)

    按tag分类,250/400的重点题目 LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本 1.Two Sum(easy) 给定一个整型数组,找出能相加起来等于一个特定目标数字 ...

  8. 算法原理系列:木桶排序

    算法原理系列:木桶排序 木桶排序是一种用标记来替代比较操作的排序手段,适用范围较窄,但效率极高,时间复杂度为 O(n) O(n),在生活中,我们也经常能看到一些木桶排序的实际案例,比如扑克牌排序时,我 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

最新文章

  1. 工作笔记---巡检记录
  2. 从飞天到倚天 阿里云底层自研技术大爆发
  3. 专利申请超全球!新崛起的中国人工智能,还有哪些你不知道的事
  4. winpcap4.1.2手动清理关键
  5. Winupdatelist
  6. LVM是逻辑盘卷管理1
  7. Java的内存--存储
  8. oracle自动把SID给改了,Oracle SID修改方式的详解
  9. 【Python】Python中内置的%操作符
  10. 泡沫股价、外卖小哥要失业了?测试员还要不要进美团?一文带你了解背后真相
  11. 【Python机器学习及实践】笔记
  12. 小程序wxParse
  13. 对抗神经网络(python-一维数据)
  14. Invalid injected android support version ‘202.7660.26.42.7322048‘, expected to be of the form ‘w.x.y
  15. 转载1:拓扑结构介绍及其种类
  16. 修复计算机u盘,u盘 修复,教您u盘损坏怎么修复
  17. SICP练习题1.14
  18. Type of the default value for 'songs' prop must be a function
  19. rabbitmq的exclusive 排他队列
  20. case语句使用举例

热门文章

  1. Influx 产品常见问题及使用技巧(2)
  2. 淘宝、一淘、淘宝商城 - 马云内部邮件谈分拆
  3. 2018中国大学生程序设计竞赛 - 网络选拔赛
  4. 2.网页布局之切切豆腐
  5. Go游戏服务器开发的一些思考(三十):排行榜服务器设计思路
  6. 春气融和,再创佳绩 | 菊风中标华融湘江银行远程视频银行项目
  7. Tribal项目介绍
  8. 谈谈 雷达信号处理之脉冲压缩
  9. 同样诞生于顶尖大学,一个市值千亿,一个曾经仅次腾讯百度,如今却。。。...
  10. LeetCode1309