Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

原题地址: Missing Number

难度: Easy

题意: 存在一个长度为n的数组,其中数值包括在[0, n]之中,返回缺少的那个数

思路1:

(1)类似217. Contains Duplicate@python,采用正负计数方式,将数组中的值与数组索引对应.

(2)遍历数组,将值对应的索引变为负数

注意: 存在0这个数值,如果0对应的索引就是缺少的值,那么很可能找不到要求的值,所以给数组添加一个值,同时也防止 out of range .

代码:

class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
nums.append(n+1)
for i in range(n):
idx = abs(nums[i])
nums[idx] = -nums[idx] tmp = None
for i in range(n+1):
if nums[i] > 0:
return i
elif nums[i] == 0:
tmp = i
return tmp

时间复杂度: O(n)

空间复杂度: O(1)

思路2:

思路1这种方式不够简洁,处理0这个干扰项.因为数组长度为n,数组内的值在0-n之间,并且缺少一个值.因此,相当于0这个值代替的缺少的值,采用和相减的方式可以求出缺少的值

class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
total = (1 + n) * n / 2
return total - sum(nums)

时间复杂度: O(n)

空间复杂度: O(1)

268. Missing Number@python的更多相关文章

  1. <LeetCode OJ> 268. Missing Number

    268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...

  2. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  3. 【LeetCode】268. Missing Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...

  4. [LeetCode&Python] Problem 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  5. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  6. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  7. 268. Missing Number序列中遗失的数字

    [抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  8. [LeetCode] 268. Missing Number 缺失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  9. 268. Missing Number -- 找出0-n中缺失的一个数

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

随机推荐

  1. [Xcode 实际操作]九、实用进阶-(19)重写父类的绘图方法,使用图形上下文绘制自定义图形

    目录:[Swift]Xcode实际操作 本文将演示如何使用图形上下文,绘制自定义图形. 使用快捷键[Command]+[N]创建一个新的类文件. (在项目文件夹[DemoApp]上点击鼠标右键[New ...

  2. assembly x86(nasm)串比较

    预留字符串口令,输入口令串与预留密码串比较.若匹配则显示“MATCH!CONGRATULATION”,否则显示“NOMATCH!”,并让用户重新输入,程序能对口令进行测试,但测试次数最多3次,若3次输 ...

  3. Mycat(1)

    https://www.jianshu.com/p/26513f428ecf https://blog.csdn.net/fly910905/article/details/87101059 http ...

  4. E: 软件包 ffmpeg 没有可供安装的候选者

    问题:在DSO安装依赖项ffmpeg时遇到“E: 软件包 ffmpeg 没有可供安装的候选者”这一问题. 解决:在Ubuntu上gstreamer0.10-ffmpeg属于额外的版权受限程序,gstr ...

  5. lucene原理及java实现

    https://blog.csdn.net/liuhaiabc/article/details/52346493 https://blog.csdn.net/yang307511977/article ...

  6. 洛谷P3603 || bzoj 4763 雪辉 && bzoj4812: [Ynoi2017]由乃打扑克

    https://www.luogu.org/problemnew/show/P3603 https://www.lydsy.com/JudgeOnline/problem.php?id=4763 就是 ...

  7. Windows如何利用输入法简单的打出 ‘↑’ ‘↓’ ‘↖’等箭头

    ‘↑’  shang ‘↓’ xia ‘←’ zuo ‘→’ you ‘↖’ zuoshang ‘↙’  zuoxia ‘↗’  youshang ‘↘’  youxia

  8. ZSP12项目的总结

    前言:一款测量仪器做出来容易,想好做好还是需要投入更多的时间和心血. 项目概述:硬件已经定型,在C8051F020基础上的软件开发. 一 关于C8051F单片机:虽然自己整过8051单片机,但那已经是 ...

  9. json_encode 中文处理

    在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它. 这个函数的功能是将数值转换成json数据存储格式. ...

  10. office 导出问题

    就用程序池右击项目高级设置 应用程序池的项目中的标识改为 LocalSystem 启用32位应用程序设为true或false