LeetCode 268. Missing Number–Python解法–数学题


LeetCode题解专栏:LeetCode题解
LeetCode 所有题目总结:LeetCode 所有题目总结
大部分题目C++,Python,Java的解法都有。


题目地址:Missing Number - LeetCode


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?


之所以会想到做这道题,是因为之前在看书《算法新解》的前言部分就讲了这道题目。

最容易想到的是使用排序算法,但肯定不是最优的。

另外一种略好的做法是使用set,但额外空间占用比较多。

我想到的Python解法如下:

class Solution:def missingNumber(self, nums: List[int]) -> int:number=len(nums)l=[0]*numberfor i in nums:if i<number:l[i]=1for i in range(0,number):if l[i]==0:return ireturn number

时间复杂度为O(n),空间复杂度为O(1)。

更优的解法是直接算,

Python解法如下:

class Solution:def missingNumber(self, nums):expected_sum = len(nums)*(len(nums)+1)//2actual_sum = sum(nums)return expected_sum - actual_sum

LeetCode 268. Missing Number--Python解法--数学题相关推荐

  1. leetcode 268. Missing Number

    原地哈希.代码没怎么优化,思路清晰. int missingNumber(vector<int>& nums) {int size = nums.size();for (int i ...

  2. LeetCode刷题之python解法(持续更新)

    1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...

  3. 【Leetcode】5 longestPalindrome python解法

    leetcode第5题 longestPalindrome 最长回文子串 python解法 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 输入: " ...

  4. LeetCode 15. 3Sum--Java,Python解法

    题目地址: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...

  5. 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  6. 268. Missing Number(缺失数字)

    题目地址:https://leetcode.com/problems/missing-number/description/ Given an array containing n distinct ...

  7. Leet Code OJ 268. Missing Number [Difficulty: Medium]

    题目: Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is miss ...

  8. LeetCode之Missing Number

    1.题目 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is m ...

  9. 268 missing number

    问题:给定一个包含n个不同数字的数组,从0开始,例如0,1,2,-n.找到数组中缺了那个数字.例如输入nums=[0,1,3],输出2. 思路:高斯 从0到n的和为sum=(1+n)*n/2.只要计算 ...

最新文章

  1. 如何用jsp在线自动批改_推荐:5个好用的免费自动化在线营销工具
  2. 值得二刷的让MySQL性能“飞起来”的方法总结
  3. .classpath 和.project文件含义
  4. python的22个基本语法
  5. angularjs探秘五 举足轻重的scope
  6. 前端学习(1689):前端系列javascript之如何搞定面试题
  7. android默认exported_android:exported 属性详解-阿里云开发者社区
  8. babel-loader jsx SyntaxError:意外令牌[重复]
  9. 6步学会VS封装DLL
  10. CASS11.0.0.8 for AutoCAD2010-2023安装教程
  11. 基于Swing与JavaFx的音乐播放器——轻音
  12. OpenGL学习笔记:GLAD和第一个窗口
  13. “数据打通”不等于“数据共融”,智能数据营销解决方案了解一下
  14. 高并发下如何快速使用MQ实现缓冲流量,削峰填谷
  15. ubuntu 16.04 Titanxp 安装cuda10.0 cudnn7.6 环境
  16. openlayers3中,在地图上添加静态边界线
  17. foo/bar/baz/qux
  18. 我的世界基岩版红石教程(超简单)2
  19. mysql 8 全文检索_MySQL 8中使用全文检索示例
  20. 设置了相对定位relative之后,改变top值,如何去掉多余空白?

热门文章

  1. Nat. Mach. Intell. | 生物医学关系抽取的机器学习新框架
  2. NLP(5) | 命名实体识别
  3. 图神经网络(GNN)的简介
  4. 基于K-gram的winnowing特征提取剽窃查重检测技术(概念篇)
  5. c语言执行得不出答案,C语言这个答案为什么是b?
  6. 15万人调查发现:做博后越久,一辈子挣的钱越少
  7. Nature综述:植物与微生物组的相互作用:从群落装配到植物健康(上)
  8. 如何开启win10内置Linux子程序
  9. python语言标号_Python 编码为什么那么蛋疼?
  10. R语言ggplot2可视化在图形中添加箭头:直线箭头、弧形箭头