【leetcode】1228.Missing Number In Arithmetic Progression
题目如下:
解题思路:题目很简单。先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失。
代码如下:
class Solution(object):
def missingNumber(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
arr.sort()
diff = (arr[-1] - arr[0])/(len(arr))
for i in range(len(arr)-1):
if arr[i] + diff != arr[i+1]:
return arr[i] + diff
return arr[0]
【leetcode】1228.Missing Number In Arithmetic Progression的更多相关文章
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】163. Missing Range
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given a sorted integer array where the rang ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
随机推荐
- 基于vant实现一个问卷调查
实现背景 最近学习<vue实战>,第二篇进阶篇有一个练习 刚好最近在研究vue移动端框架vant 两者结合,实现这么个小项目 实现代码 新建 vue单文件 L0529L.vue <t ...
- 完全卸载oracle11g步骤(不可行,直接用oracle自用删除就行)
完全卸载oracle11g步骤:1. 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务.2. 开始->程序->oracle - OraHome ...
- poj1061(扩展欧基里德定理)
题目链接:https://vjudge.net/problem/POJ-1061 题意:在一个首位相接的坐标轴上,A.B开始时分别位于X,Y处,每个单位时间向右移动m,n米,问是否能相遇,坐标轴长L. ...
- 更新到.netcore3.0后找不到dotnet-ef的解决办法
在项目根目录下建立global.json文件 { "sdk": { "version": "2.2.402" } } 或使用命令 dotne ...
- PythonError解决方案
# Pip Error pip install * error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visua ...
- js 判断图片是否存在
有的时候 虽然图片的路径是正确的 但是有可能由于某些原因 导致图裂了 或者网络加载失败 那这样的应该怎么判断呢? 如下: function isHasImg(pathImg){ var Img ...
- lamp项目上线流程简述 (ubuntu16.04 )
1 新建一个sudo用户,而不是直接用root操作 ① 新建用户可参考 https://www.cnblogs.com/bushuwei/p/10880182.html ② 赋予sudo权限: ...
- java学习笔记(5)多线程
一.简介(过段时间再写,多线程难度有点大) --------------------------------------- 1.进程:运行时的概念,运行的应用程序 2.线程:应用程序内部并发执行的代码 ...
- 什么是RESTful API、WSGI、pecan
RESTful API REST的全称是Representational State Transfer(表征状态转移), 是Roy Fielding在他的博士论文Architectural Style ...
- python3:tuple元组
https://www.runoob.com/python3/python3-tuple.html 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. Py ...