268. Missing Number

来源:互联网 发布:人工智能视觉识别 编辑:程序博客网 时间:2024/06/07 23:04

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.

a^b^b = a

public class Solution {    public int missingNumber(int[] nums) {        int res = 0;        if (nums == null || nums.length == 0) return res;        for (int i = 0; i < nums.length; i++) {            res = res ^ i ^ nums[i];        }        return res ^ nums.length;    }}
0 0
原创粉丝点击