国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Leetcode 268. Missing Number

這篇具有很好參考價(jià)值的文章主要介紹了Leetcode 268. Missing Number。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

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.文章來(lái)源地址http://www.zghlxwxcb.cn/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_

到了這里,關(guān)于Leetcode 268. Missing Number的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • LeetCode --- 1971. Find if Path Exists in Graph 解題報(bào)告

    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日
    瀏覽(21)
  • LeetCode --- 1863. Sum of All Subset XOR Totals 解題報(bào)告

    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日
    瀏覽(26)
  • LeetCode --- 1869. Longer Contiguous Segments of Ones than Zeros 解題報(bào)告

    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日
    瀏覽(22)
  • LeetCode --- 1880. Check if Word Equals Summation of Two Words 解題報(bào)告

    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日
    瀏覽(29)
  • LeetCode --- 1790. Check if One String Swap Can Make Strings Equal 解題報(bào)告

    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日
    瀏覽(22)
  • leetcode268. 丟失的數(shù)字

    這題簡(jiǎn)單的有點(diǎn)過(guò)分了吧。。。 一開(kāi)始還納悶會(huì)不會(huì)有重復(fù)的元素,后來(lái)看到[0,n]范圍,那么肯定有n+1個(gè)數(shù)字,然后要在n 個(gè)數(shù)字里面找誰(shuí)沒(méi)有,那肯定沒(méi)有重復(fù)的元素,如果有重復(fù),就不止缺少一個(gè)元素了。 思路: 排序之后,看誰(shuí)跟下標(biāo)不一樣,就返回它之前的那個(gè)值;如

    2024年02月12日
    瀏覽(18)
  • 【Leetcode】268.丟失的數(shù)字

    給定一個(gè)包含 [0, n] 中 n 個(gè)數(shù)的數(shù)組 nums ,找出 [0, n] 這個(gè)范圍內(nèi)沒(méi)有出現(xiàn)在數(shù)組中的那個(gè)數(shù)。 示例1: 示例2: 示例3:

    2024年01月17日
    瀏覽(15)
  • 區(qū)間dp 解題報(bào)告

    **區(qū)間dp:**就是對(duì)于區(qū)間的一種動(dòng)態(tài)規(guī)劃,對(duì)于某個(gè)區(qū)間,它的合并方式可能有很多種,我們需要去枚舉所有的方式,通常是去枚舉區(qū)間的分割點(diǎn),找到最優(yōu)的方式(一般是找最少消耗)。 區(qū)間dp寫法:( 石子合并(弱化版) 問(wèn)題描述:略。 轉(zhuǎn)移方程: F ( i , j ) = m i n i ≤ k

    2024年02月13日
    瀏覽(23)
  • [ARC114D] Moving Pieces on Line 解題報(bào)告

    AT題面 有一個(gè)紅色的數(shù)軸,相鄰兩個(gè)整點(diǎn)之間連有一條邊,所有邊初始為紅色。數(shù)軸上有 (n) 個(gè)棋子,將一個(gè)棋子從 (a) 位置移到 (b) 位置,可以將 ((a,b)) 之間紅邊變?yōu)樗{(lán)邊,藍(lán)邊變?yōu)榧t邊。給定 (k-1) 條線段,問(wèn)能否進(jìn)行若干次操作,使得當(dāng) (i) 是奇數(shù),第 (i) 條線段

    2024年02月05日
    瀏覽(17)
  • 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日
    瀏覽(45)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包