Leetcode 268. Missing Number

这篇具有很好参考价值的文章主要介绍了Leetcode 268. Missing Number。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Problem

Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Algorithm

Sum all the numbers as x x x and use n ( n + 1 ) 2 − x \frac{n(n+1)}{2} - x 2n(n+1)x.文章来源地址https://www.toymoban.com/news/detail-626162.html

Code

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        sum_, n_ = 0, len(nums)
        for num in nums:
            sum_ += num
        return n_ * (n_ + 1) // 2 - sum_

到了这里,关于Leetcode 268. Missing Number的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode --- 1863. Sum of All Subset XOR Totals 解题报告

    The  XOR total  of an array is defined as the bitwise  XOR  of  all its elements , or  0  if the array is  empty . For example, the  XOR total  of the array  [2,5,6]  is  2 XOR 5 XOR 6 = 1 . Given an array  nums , return  the  sum  of all  XOR totals  for every  subset  of  nums .  Note:  Subsets with the  same  elements should be c

    2024年02月15日
    浏览(40)
  • LeetCode --- 1971. Find if Path Exists in Graph 解题报告

    There is a  bi-directional  graph with  n  vertices, where each vertex is labeled from  0  to  n - 1  ( inclusive ). The edges in the graph are represented as a 2D integer array  edges , where each  edges[i] = [ui, vi]  denotes a bi-directional edge between vertex  ui  and vertex  vi . Every vertex pair is connected by  at most one  edge, and

    2024年02月07日
    浏览(32)
  • LeetCode --- 1869. Longer Contiguous Segments of Ones than Zeros 解题报告

    Given a binary string  s , return  true  if the  longest  contiguous segment of  1 \\\' s is  strictly longer  than the  longest  contiguous segment of  0 \\\' s in  s , or return  false  otherwise . For example, in  s = \\\" 11 01 000 10\\\"  the longest continuous segment of  1 s has length  2 , and the longest continuous segment of  0 s has length 

    2024年02月15日
    浏览(25)
  • LeetCode --- 1880. Check if Word Equals Summation of Two Words 解题报告

    The  letter value  of a letter is its position in the alphabet  starting from 0  (i.e.  \\\'a\\\' - 0 ,  \\\'b\\\' - 1 ,  \\\'c\\\' - 2 , etc.). The  numerical value  of some string of lowercase English letters  s  is the  concatenation  of the  letter values  of each letter in  s , which is then  converted  into an integer. For example, if  s = \\\"acb\\\" , we

    2024年02月13日
    浏览(31)
  • LeetCode --- 1790. Check if One String Swap Can Make Strings Equal 解题报告

    You are given two strings  s1  and  s2  of equal length. A  string swap  is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices. Return  true   if it is possible to make both strings equal by performing  at most one string swap  on  exactly one  of the strings.  Otherwise, re

    2024年02月10日
    浏览(42)
  • 【Leetcode】268.丢失的数字

    给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数。 示例1: 示例2: 示例3:

    2024年01月17日
    浏览(22)
  • leetcode268. 丢失的数字

    这题简单的有点过分了吧。。。 一开始还纳闷会不会有重复的元素,后来看到[0,n]范围,那么肯定有n+1个数字,然后要在n 个数字里面找谁没有,那肯定没有重复的元素,如果有重复,就不止缺少一个元素了。 思路: 排序之后,看谁跟下标不一样,就返回它之前的那个值;如

    2024年02月12日
    浏览(22)
  • 区间dp 解题报告

    **区间dp:**就是对于区间的一种动态规划,对于某个区间,它的合并方式可能有很多种,我们需要去枚举所有的方式,通常是去枚举区间的分割点,找到最优的方式(一般是找最少消耗)。 区间dp写法:( 石子合并(弱化版) 问题描述:略。 转移方程: F ( i , j ) = m i n i ≤ k

    2024年02月13日
    浏览(24)
  • [ARC114D] Moving Pieces on Line 解题报告

    AT题面 有一个红色的数轴,相邻两个整点之间连有一条边,所有边初始为红色。数轴上有 (n) 个棋子,将一个棋子从 (a) 位置移到 (b) 位置,可以将 ((a,b)) 之间红边变为蓝边,蓝边变为红边。给定 (k-1) 条线段,问能否进行若干次操作,使得当 (i) 是奇数,第 (i) 条线段

    2024年02月05日
    浏览(29)
  • LeetCode447. Number of Boomerangs

    You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters). Return the number of boomerangs. Example 1: Input: points = [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs

    2024年02月02日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包