Posted onEdited onInLeetCodeDisqus: Symbols count in article: 231Reading time ≈1 mins.
O(n) time O(1) space 利用下标从0到n,把所有数异或最后剩下的就是差的数
1 2 3 4 5 6 7 8 9 10 11
classSolution { public: intmissingNumber(vector<int>& nums){ int n = nums.size(); int res = n; for (int i = 0; i < n; ++i) { res ^= (i ^ nums[i]); } return res; } };