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. Codevs 1794 修剪花卉

    1794 修剪花卉   题目描述 Description ZZ对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题. 一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿 ...

  2. bzoj3453: tyvj 1858 XLkxc(拉格朗日插值)

    传送门 \(f(n)=\sum_{i=1}^ni^k\),这是自然数幂次和,是一个以\(n\)为自变量的\(k+1\)次多项式 \(g(n)=\sum_{i=1}^nf(i)\),因为这东西差分之后是 ...

  3. tpc-ds99 工具使用

    安装部署 tpc-ds-99 工具 解压文件 unzip tpc-ds-tool.zip 进入目录 cd v2.3.0/tools 拷贝Makefile文件 cp Makefile.suite Mak ...

  4. 初识DetNet:确定性网络的前世今生

    在刚刚落幕的2019中国 SDN/NFV/AI大会上,确定性网络(Deterministic Networking)成为了大家讨论的热点话题之一.随着工业物联网(IIoT)的兴起和工业4.0的提出,T ...

  5. C 语言实例 - 两个矩阵相加

    C 语言实例 - 两个矩阵相加 C 语言实例 C 语言实例 使用多维数组将两个矩阵相加. 实例 #include <stdio.h> int main(){ ][], b[][], sum ...

  6. MyBatis逆向工程代码的生成以及使用详解(持续更新)

    逆向工程简介什么是逆向工程:        mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java.ma ...

  7. tensorflow:实战Google深度学习框架第三章

    tensorflow的计算模型:计算图–tf.Graph tensorflow的数据模型:张量–tf.Tensor tensorflow的运行模型:会话–tf.Session tensorflow可视 ...

  8. ZROI提高组模拟赛05总结

    ZROI提高组模拟赛05总结 感觉是目前为止最简单的模拟赛了吧 但是依旧不尽人意... T1 有一半的人在30min前就A掉了 而我花了1h11min 就是一个简单的背包,我硬是转化了模型想了好久,生 ...

  9. 简单了解junit的使用

    普通使用: 在没有用测试框架之前,我们要用一个main方法来跑代码,而有了像junit这样的测试框架后,就可以不用次次写个main方法了. 直接上代码,注释里有说明: package com.stuP ...

  10. 547 Friend Circles 朋友圈

    班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...