268. Missing Number@python
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的更多相关文章
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- 【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&Python] Problem 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- 268. Missing Number序列中遗失的数字
[抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 268. Missing Number -- 找出0-n中缺失的一个数
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
随机推荐
- hdu1850(nim博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1850 题意:中文题诶- 思路:nim博弈 可以将本题抽象成一般nim博弈,那么有: 1. 对于所有元素 ...
- Mybatis分页中遇到的坑2
站在巨人的肩膀上 http://crocutax.com/blog/mybatis-one-to-many-nestes-query-and-page-query Mybatis一对多嵌套查询和 ...
- jvm 调优(转)
转自 http://pengjiaheng.iteye.com/blog/538582 年轻代的设置很关键 JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系 ...
- [题解]luogu_P2613有理数取余
#include<bits/stdc++.h> #define ll long long using namespace std; ; inline int read(){ ,fix=;c ...
- C# 基础之密封类
C#密封类 一.密封类 1. 密封类的定义 如果我们不希望自己编写的类被继承:如果有的类已经没有再被继承的必要,这时,我们可以使用sealed修饰符在类中进行声明,以达到该类不能派生其它类的目的,该类 ...
- C# 常量与只读属性的区别
public readonly string name ----这个name是个只读属性,不需要在定义时初始化值,而是可以在构造函数中完成初始化. public const int age =18 ...
- C++中this指针的理解
C++中this指针的理解 先要理解class的意思.class应该理解为一种类型,象int,char一样,是用户自定义的类型.用这个类型可以来声明一个变量,比如int x, myclass my等等 ...
- 模拟ssh、黏包、hashlib模块(MD5)
待补充..... 一.模拟ssh 二.黏包 1.黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) res=subprocess.Popen(cm ...
- eclipse打开jsp的方式怎么设置成默认
https://jingyan.baidu.com/article/4ae03de34137be3eff9e6b93.html
- [已读]JavaScript高级程序设计(第2版)
经典红皮书~~