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 用异或的办法:
class Solution {
public:
int missingNumber(vector<int>& nums) {
int len = nums.size();
if(len == )
return ;
int x = ;
for (int i=;i<=len;i++)
x ^= i;
for (int i=;i<len;i++)
x ^= nums[i];
return x;
}
};
												

【easy】268. Missing Number的更多相关文章

  1. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  2. 【LeetCode】268. Missing Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...

  3. 【easy】202. Happy Number

    happy number Write an algorithm to determine if a number is "happy". A happy number is a n ...

  4. 【easy】263. Ugly Number 判断丑数

    class Solution { public: bool isUgly(int num) { ) return false; ) return true; && num % == ) ...

  5. 【leetcode】1228.Missing Number In Arithmetic Progression

    题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...

  6. 181. Flip Bits【easy】

    181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n  ...

  7. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  8. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  9. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

随机推荐

  1. CodeForces Round #555 Div.3

    A. Reachable Numbers 代码: #include <bits/stdc++.h> using namespace std; ; int N; set<int> ...

  2. lazyMan

    class Lazyman { constructor() { this.tasks = []; this.init(); } init() { const task = () => { con ...

  3. JS学习笔记:(二)回流和重绘

    在搞清楚回流和重绘的概念之前,我们要清除浏览器的渲染过程. 解析生成DOM Tree(此时包含所有节点,包括display:none); 根据CSS Object Module(CCSSOM)计算节点 ...

  4. 【BZOJ4028】[HEOI2015]公约数数列(分块)

    [BZOJ4028][HEOI2015]公约数数列(分块) 题面 BZOJ 洛谷 题解 看一道题目就不会做系列 首先\(gcd\)最多只会有\(log\)种取值,所以我们可以暴力枚举出所有可能的\(g ...

  5. 函数内部还是不要使用 strtok()

    今天在调试程序的时候,遇到一个奇怪的事情,一开始担心是代码存在内存溢出引起的,花了半个小时没找到原因. 在吃饭的时候,突然想起可能是 strtok() 引起的,查找调用的函数,果然发现在函数中使用了  ...

  6. mac下安装maven

    在mac下 使用 brew安装,brew install maven 查看maven版本 mvn -version 打开Terminal,输入以下命令,设置Maven classpath 添加下列两行 ...

  7. Vue(四)组件

    组件的复用 这里的工程和上一节的一样 先建立一个组件 MyButton.vue <template> <button @click="count++">Yo ...

  8. 阿里百川SDK初始化失败 错误码是203

    由idea换到Androidstudio 了,结果报这个错,之前好好的啊!!! 设置问题:

  9. Android activity创建三部曲

    1.新建类继承Activity或其子类,现在一般继承AppCompatActivity public class TestActivity extends AppCompatActivity { @O ...

  10. Axure PR的使用

    1759139 王越 Axure RP是美国Axure Software Solution公司旗舰产品,是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家能够快速创建应用软件或W ...