首页 > 其他分享 >leetcode-645. 错误的集合

leetcode-645. 错误的集合

时间:2023-01-04 00:12:25浏览次数:63  
标签:nums int sum ret pSum 645 len 集合 leetcode

645. 错误的集合 - 力扣(Leetcode)

又用了哈希表,又用了数学计算,看题解有个位运算看不太懂

func findErrorNums(nums []int) []int {
    m := make(map[int]struct{}, len(nums))
    
    pSum := (1+len(nums))*len(nums)/2
    ret := []int{}
    sum := 0

    for _, v := range nums {
        sum+=v
        if _, ok := m[v]; ok {
            ret = append(ret, v)
        }

        m[v] = struct{}{}
    }

    // fmt.Print(sum, pSum)
    ret = append(ret, ret[0] - (sum - pSum))
    return ret
}

标签:nums,int,sum,ret,pSum,645,len,集合,leetcode
From: https://www.cnblogs.com/wudanyang/p/17023752.html

相关文章

  • leetcode-643. 子数组最大平均数 I
    643.子数组最大平均数I-力扣(Leetcode)滑动窗口,判断好边界条件即可funcfindMaxAverage(nums[]int,kint)float64{begin,end:=0,k-1ifend>=len(n......
  • 秋招之路-链表面试题集合(二)
    [图]program2019-07-24前言链表是最基本的数据结构,面试官也常常用链表来考察面试者的基本能力,链表的操作也离不开指针,指针又很容易导致出错。综合多方面的原因,链表题目在面......
  • List集合的排序方式
    本文主要讲述Lsit集合的排序方式:1/**2*集合的排序方式:2种方式3*/4publicclassGenericWork{5publicstaticvoidmain(String[]args){......
  • leetcode-168-easy
    ExcelSheetColumnTitleGivenanintegercolumnNumber,returnitscorrespondingcolumntitleasitappearsinanExcelsheet.Forexample:A->1B->2C-......
  • leetcode-206-easy
    ReverseLinkedListGiventheheadofasinglylinkedlist,reversethelist,andreturnthereversedlist.Example1:Input:head=[1,2,3,4,5]Output:[5,......
  • leetcode-557-easy
    ReverseWordsinaStringIIIGivenastrings,reversetheorderofcharactersineachwordwithinasentencewhilestillpreservingwhitespaceandinitialwo......
  • leetcode-627-easy
    IslandPerimeterYouaregivenrowxcolgridrepresentingamapwheregrid[i][j]=1representslandandgrid[i][j]=0representswater.Gridcellsareconn......
  • leetcode-121-easy
    BestTimetoBuyandSellStockYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitb......
  • leetcode-441-easy
    ArrangingCoinsYouhavencoinsandyouwanttobuildastaircasewiththesecoins.Thestaircaseconsistsofkrowswheretheithrowhasexactlyicoins.Th......
  • leetcode-459-easy
    RepeatedSubstringPatternGivenastrings,checkifitcanbeconstructedbytakingasubstringofitandappendingmultiplecopiesofthesubstringtogether......