LeetCode:268. Missing Number

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:268. Missing Number相关的知识,希望对你有一定的参考价值。

package Math;

import java.util.Arrays;

//Question 268. Missing Number
/*
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n,
find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
Note:
Your algorithm should run in linear runtime complexity.
Could you implement it using only constant extra space complexity?
*/
public class missingNumber268 {
//测试case设计的有问题,就有点不理解题目了,其实根本不存在[3]这种情况,把题目想复杂了
public static int missingNumber(int[] nums) {
int N=nums.length;
int sum=0;
for(int i=0;i<nums.length;i++){
sum+=nums[i];
}
return N*(N+1)/2-sum;
}
//study1 利用a^b^b=a
//[0,1,3] 0^0^0^1^1^2^3^3=2 好吧,好机智
public static int missingNumber2(int[] nums){
int xor=0;
int i=0;
for(;i<nums.length;i++){
xor=xor^i^nums[i];
}
return xor^i;
}
//study2 binary search
public static int missingNumber3(int[] nums){
Arrays.sort(nums);
int left=0;
int right=nums.length-1;
while(left<right){
int mid=(left+right)/2;
if(nums[mid]>mid)
right=mid;
left=mid+1;
}
return left;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums1={};
//测试case设计错误
int[] nums2={3};
int[] nums3={1,3};
int[] nums4={1,3,2,5};
int[] nums5={1,2,3};
int[] nums6={1};

System.out.println(missingNumber(nums1));
System.out.println(missingNumber(nums2));
System.out.println(missingNumber(nums3));
System.out.println(missingNumber(nums4));
System.out.println(missingNumber(nums5));
System.out.println(missingNumber(nums6));
}

}


























































以上是关于LeetCode:268. Missing Number的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode:268. Missing Number

leetcode268 - Missing Number - easy

LeetCode 268. Missing Number

Leetcode 268: Missing Number

Leetcode 268 Missing Number 位运算

leetcode 268. Missing Number