Leetcode 268.缺失数字 By Python

给定一个包含 0, 1, 2, ..., nn 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。

示例 1:

输入: [3,0,1]
输出: 2

示例 2:

输入: [9,6,4,2,3,5,7,0,1]
输出: 8

说明:
你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现?



思路

因为给定的序列也是从0开始,所以可以进行排序,比较索引和索引对应的值,如果两个不等于说明就确实一个值了,还要注意一个情况是,没出现的数字是n

代码

class Solution(object):
    def missingNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        for i in range(len(nums)):
                if i != nums[i]:
                    return i
                if i == len(nums)-1:
                    return len(nums)

Ps.或者可以比较未缺失情况下的值和给定的序列的值的差就可以找到缺失数字

return len(nums)*(len(nums)+1)//2 - sum(nums)

原文地址:https://www.cnblogs.com/MartinLwx/p/9692077.html

时间: 2024-07-30 23:43:45

Leetcode 268.缺失数字 By Python的相关文章

leetcode 374. 猜数字大小(python)

我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-1,1 或 0): -1 : 我的数字比较小 1 : 我的数字比较大 0 : 恭喜!你猜对了!示例 : 输入: n = 10, pick = 6输出: 6 class Solution(object): def guessNumber(self, n): "&q

力扣-268.缺失数字 题解

题目 给定一个包含 0, 1, 2, ..., n?中?n?个数的序列,找出 0 .. n?中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例?2: 输入: [9,6,4,2,3,5,7,0,1] 输出: 8 说明: 你的算法应具有线性时间复杂度.你能否仅使用额外常数空间来实现? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/missing-number 著作权归领扣网络所有.商业转载请联系官方授权,非商业

268. 缺失数字

地址:https://leetcode-cn.com/problems/missing-number/ <?php /** 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: */ class Solution { /** * @param Integer[] $nums * @return Integer */ function missingNumber1($nums)

[LeetCode] 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 c

[leetcode]Evaluate Reverse Polish Notation @ Python

原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples

将字符串转化为数字的python实现

将字符串转化为数字的python实现 将字符串转化为数字的python实现,例如将字符串"1234567.8"转化为 1234567.8 这也是学习python中的一个简单的练习题,代码如下: # coding=UTF-8 将字符串转化为数字 from functools import reduce import math def char2int(s): return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9'

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点.... class Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): if None =

[leetcode]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归

[leetcode]Search for a Range @ Python

原题地址:https://oj.leetcode.com/problems/search-for-a-range/ 题意: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not