Golang sum solution


  • 0
    R
    func missingNumber(nums []int) int {
       sum:= len(nums) * (len(nums)  + 1) / 2
       sm2 := 0
       for i:=0; i < len(nums); i++ {
           sm2+= nums[i]
       }
       return sum - sm2
    }
    

    // n * (n+1)/2 is shortcut for sum of all numbers


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.