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.

当一个数组中的数都是出现两次,只有一个是单个时,找这个数可以用异或^,把所有数异或起来就是,因为异或自己本身得0,0异或任何数得本身。

这个题可以把数组和[0-n]组成一个数组,就可以用上边的方法了。

public class Solution {
    public int missingNumber(int[] nums) {
        int n = nums.length;
        int res = 0;
        for(int i = 0;i<n;i++)
        {
            res ^= (nums[i] ^ i);
        }
        res ^= n;
        return res;
    }
}
时间: 2024-10-09 12:18:43

268. Missing Number的相关文章

&amp;lt;LeetCode OJ&amp;gt; 268. Missing Number

268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium 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.

LeetCode 136. Single Number &amp; 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

【LeetCode】268. Missing Number

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 i

268. Missing Number (binary)

0到n的sum减去已经存在的就是missing number 1 //Old 2 class Solution { 3 public int missingNumber(int[] nums) { 4 int max = nums.length; 5 List<Integer> A = new ArrayList<Integer>(); 6 for(int i = 0; i <= max; i++) { 7 A.add(i); 8 } 9 for(int j = 0; j &

Leetcode 268 Missing Number 位运算

题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组,这样就和singe number 一样. 1 class Solution { 2 public: 3 int missingNumber(std::vector<int>& nums) { 4 int ans = 0; 5 for (std::vector<int>::siz

LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告

1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Not

LeetCode 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 usi

leetcode 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 usi

[Array]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 usi