268. Missing Number
2018-03-05 22:36
134 查看
Given an array containing n distinct numbers taken from 0, 1, 2, …,
n, find the one that is missing from the array.
Example 1
Input: [3,0,1] Output: 2
Example 2
Input: [9,6,4,2,3,5,7,0,1] Output: 8
Note: Your algorithm should run in linear runtime complexity. Could
you implement it using only constant extra space complexity?
n, find the one that is missing from the array.
Example 1
Input: [3,0,1] Output: 2
Example 2
Input: [9,6,4,2,3,5,7,0,1] Output: 8
Note: Your algorithm should run in linear runtime complexity. Could
you implement it using only constant extra space complexity?
//直接把1到n加起来,减去数组全部元素之和。 public int missingNumber(int[] nums) { int len=nums.length; int total=(1+len)*len/2; int sum=0; for(int i=0;i!=nums.length;i++){ sum+=nums[i]; } int res=total-sum; return res; }
相关文章推荐
- 268. Missing Number
- LeetCode 268. Missing Number - 求和、异或
- leetcode 268. Missing Number 解题报告
- 2018.03.15 leetcode 打卡 #268. Missing Number
- LeetCode - 268. Missing Number
- Leetcode-268. Missing Number
- 268. Missing Number(C语言)
- 268. Missing Number
- leetcode268. Missing Number
- 268. Missing Number
- 268. Missing Number
- 2018.03.15 leetcode 打卡 #268. Missing Number
- [LeetCode]268. Missing Number
- <leetcode>268. Missing Number
- 268. Missing Number
- Leetcode-268. Missing Number
- Leetcode# 268. Missing Number(异或)
- LeetCode 268. Missing Number
- LeetCode:268. Missing Number
- [LeetCode] 268. Missing Number