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. uoj#282. 长度测量鸡(构造)

    传送门 打表题--只有\(n\leq 3\)有解否则无解→_→ 或者严格证明的话是这样,因为算上端点一共\(n+1\)个点,共\(\frac{n(n+1)}{2}\)个点对,所以点对之间两两距离不相等 ...

  2. [Xcode 实际操作]九、实用进阶-(8)实现App的Setting设置:添加和读取程序的配置信息

    目录:[Swift]Xcode实际操作 本文将演示如何实现添加和读取程序的配置信息. 在项目文件夹[DemoApp]上点击鼠标右键->[New File]创建一个设置束文件 ->[Sett ...

  3. Java基础 使用Properties类

  4. HDU 1875(最小生成树)

    #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> ...

  5. 类加载机制 + Classloader.loadClass(String name)和Class.forName(String name)

    Classloader.loadClass(String name)和Class.forName(String name)的区别 Java的类在jvm中的加载大致分为加载,链接或者叫link(里面包含 ...

  6. shell 中的if语句

    if [ t != "." -a t != ".." ] 之前一直不知道 -a 是什么意思,后来才知道     -a = and  ;    -o = or

  7. Function ereg() is deprecated in

    PHP 5.3 ereg() 无法正常使用,提示"Function ereg() is deprecated Error".问题根源是php中有两种正则表示方法,一个是posix, ...

  8. 实战:liunx定时清理日志脚本

    参考https://blog.csdn.net/qq_39291929/article/details/79054452 1.需求:我们在 /var/log下面有   EmcsYn.log   和   ...

  9. android开发学习 ------- 【转】 android事件分发机制 和 自定义view涉及的事件分发

    参考  https://blog.csdn.net/carson_ho/article/details/54136311   ,写的很完美,原理入门的一篇博客,看这一篇就够了 https://www. ...

  10. Gridview基础

    gridview是封装好的,直接在设计界面使用,基本不需要写代码 1.绑定数据源 GridView最好与LinQDatasourse配合使用,相匹配绑定数据: 2.外观控制—— 点开有自动套用格式 布 ...