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 ...
随机推荐
- 2014-6-28 NOIP模拟赛
[今天我出的三道题目全部是图论哦,请大家轻虐] 1.魔术球问题弱化版(ball.c/.cpp/.pas) 题目描述 假设有 n 根柱子,现要按下述规则在这 n 根柱子中依次放入编号为 1,2,3,…的 ...
- uoj#418. 【集训队作业2018】三角形(线段树合并)
传送门 好迷啊--膜一下ljz 考虑每个操作,如果把操作按先后顺序放到序列上的话,操作一就是把\(w_i\)的石子放到某个节点,那么就是在序列末端加入\(w_i\),然后根据贪心肯定要把它所有儿子的石 ...
- [Xcode 实际操作]九、实用进阶-(14)使用富文本CoreText框架创建丰富多彩的文本
目录:[Swift]Xcode实际操作 本文将演示如何使用富文本CoreText框架创建丰富多彩的文本图形. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] imp ...
- js 判断当前操作系统 ios, android, 电脑端
一 . js判断移动端的操作系统(ios或Android) $(function () { var u = navigator.userAgent; var isAndroid = u.index ...
- nacos启动
nacos下载 https://github.com/alibaba/nacos 1.执行数据库脚本 2.修改配置文件application.propertiesspring.datasource.p ...
- Android Activity生命周期(转)
转自 http://blog.csdn.net/android_tutor/article/details/5772285
- python之文件路径截取 & endswith()
文件路径截取: >>> import os >>> path = '/etc/singfor/passwd/sunny/test.log' >>> ...
- servlet小型应用服务器搭建通过tomcat发布web项目
1.servlet简介:Servlet 是一个 Java程序,是在服务器上运行以处理客户端请求并做出响应的程序 2.servlet的生命周期图解: 3.各阶段: 4.基本的servlet代码: pub ...
- C++中this指针的理解
C++中this指针的理解 先要理解class的意思.class应该理解为一种类型,象int,char一样,是用户自定义的类型.用这个类型可以来声明一个变量,比如int x, myclass my等等 ...
- siege官方文档(译)(二)
WHY DO I NEED IT? Siege was written for both web developers and web systems administrators. siege是为了 ...