当前位置:网站首页>Sword finger offer 53 - ii Missing numbers from 0 to n-1

Sword finger offer 53 - ii Missing numbers from 0 to n-1

2022-07-05 05:29:00 ThE wAlkIng D

Title Description

![ Insert picture description here ](https://img-blog.csdnimg.cn/47a5ebd2365c4f788ce77d9cdd5ae0e7.png
)

Problem analysis

Use the formula of arithmetic sequence to find n Xiang He , Then find the sum of the array , Subtract the two

Code instance

class Solution {
    
    public int missingNumber(int[] nums) {
    
        int n = nums.length;
        int a = n * (n + 1) / 2;
        int sum = 0;
        for(int i = 0;i < nums.length;i++){
    
            sum += nums[i];
        }
        return (a - sum);
    }
}
原网站

版权声明
本文为[ThE wAlkIng D]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050525449002.html