[LeetCode&Python] Problem 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if 0 not in nums:
return 0
m=max(nums)
s=sum(nums)
ms=(m+1)*m/2
if ms==s:
return m+1
return (m+1)*m/2-sum(nums)
[LeetCode&Python] Problem 268. Missing Number的更多相关文章
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- [LeetCode&Python] Problem 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation
Given two integers L and R, find the count of numbers in the range [L, R](inclusive) having a prime ...
- LeetCode Array Easy 268. Missing Number
Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...
- [LeetCode&Python] Problem 693. Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [LeetCode&Python] Problem 136. Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 268. Missing Number@python
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
随机推荐
- VMware进入BIOS
在虚拟机关机状态下,点击“虚拟机”--“电源”--“打开电源时进入固件”即自动启动进入bios
- WebSphere概要文件的创建与删除
一.创建单server服务器 /was/bin/manageprofiles.sh -create -profileName server1 \ -profilePath /was/profiles/ ...
- 牛客网 PAT 算法历年真题 1002 :数字分类 (20)
1002 :数字分类 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 给定一系列正整数,请按要求对数字 ...
- get url img
selenium 1● 了解selenium 这个是做 web页面测试,模拟用户测试 =====> pip install selenium 安装 2● django 用于接 ...
- zhuan 常用图像数据集:标注、检索
目录(?)[+] 1.搜狗实验室数据集: http://www.sogou.com/labs/dl/p.html 互联网图片库来自sogou图片搜索所索引的部分数据.其中收集了包括人物.动物. ...
- learning ddr mode register MR1
- Ubuntu16.04无法使用WiFi
本人联想431,安装ubuntu16.04 lts,打开之后没有wife,参考这个解决 http://blog.csdn.net/bubblem/article/details/53575017 U ...
- ldap+flask+python2实现统一认证里面的那些编码神坑
首先想吐槽下,直接接手别人的项目,而且是经过四五个人手的项目,是怎么个痛苦.两三套代码django.flask.tornado应有尽有,代码里,掰开手指头就可数的全英文注释,几台服务器没交接清楚,所有 ...
- day35 数据库介绍和初识sql
今日内容: 1. 代码: 简易版socketsever 2.数据库(mysql)简单介绍和分类介绍 3.mysql root修改密码 4.修改字符集编码 5.初识sql语句 1.简易版socketse ...
- Java多线程习题 ===重点 ,错题积累
多线程重点,错题分析 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: . 12: 13: 14: 15: