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>::size_type i = 0; i < nums.size(); ++i){
 6             ans ^= nums[i];
 7         }
 8         for (int i = 0; i <= nums.size(); ++i){
 9             ans ^= i;
10         }
11         return ans;
12     }
13 };
时间: 2024-08-25 10:04:31

Leetcode 268 Missing Number 位运算的相关文章

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

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. 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 c

[LeetCode]268 Missing Number

很简单,提供两种解法. (1)等差数列先求得总和,然后减去给定的队列,剩下的值就是缺失值. (2)先排个序,逐个比较,不等的直接return 第一种的代码: class Solution { public: int missingNumber(vector<int>& nums) { int n = nums.size(); int expect = (n+1) * n / 2; for(int i = 0; i < n; i ++) expect -= nums[i]; ret

LeetCode:Missing Number - 缺失的数字

1.题目名称 Missing Number (缺失的数字) 2.题目地址 https://leetcode.com/problems/missing-number 3.题目内容 英文:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n  find the one that is missing from the array. 中文:给出一个包含了n个不同数字的数组,从0开始一直到n,找出缺失的数字.如果数

&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 可以用数学方法直接做,

&lt;LeetCode OJ&gt;Missing Number【268】

268. Missing Number My Submissions Question 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 num