博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Missing Number
阅读量:4361 次
发布时间:2019-06-07

本文共 718 字,大约阅读时间需要 2 分钟。

    这道题为简单题

  题目:

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

    For example,

      Given nums = [0, 1, 3] return 2.

    Note:

      Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

  思路:

    这个题其实很简单直接把计算列表没有减少的情况下的总和减去列表减少情况下的总和

  代码:

1 class Solution(object): 2     def missingNumber(self, nums): 3         """ 4         :type nums: List[int] 5         :rtype: int 6         """ 7         a = 0 8         b = 0 9         for i in nums:10             a += 111             b += i12         return (1+a)*a//2 - b

 

转载于:https://www.cnblogs.com/liuxinzhi/p/7545937.html

你可能感兴趣的文章
嵌套循环概念
查看>>
ASP.NET MVC Model绑定(二)
查看>>
一步一步写算法(之hash表)
查看>>
漫谈并发编程(一) - 并发简单介绍
查看>>
JDBC连接MySQL数据库及演示样例
查看>>
Beta 冲刺(1/7)
查看>>
修改 Vultr 登录密码
查看>>
CSS学习
查看>>
Centos 安装lnmp完整版
查看>>
【转】Eclipse和PyDev搭建完美Python开发环境(Ubuntu篇)
查看>>
Differences between page and segment
查看>>
字符串之strcmp
查看>>
最长公共子序列(不连续)
查看>>
微服务:Java EE的拯救者还是掘墓人?
查看>>
如何在Centos里面,把.net core程序设为开机自启动
查看>>
1920*1080pc端适配
查看>>
Nutch系列1:简介
查看>>
前端UI框架选择区别对比推荐
查看>>
栈 队列 和 双向队列
查看>>
从垃圾回收看闭包
查看>>