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. JasperReports® Library | Jaspersoft Community

    JasperReport报表导出踩坑实录 - 小卖铺的老爷爷 - 博客园https://www.cnblogs.com/laoyeye/p/7707149.html jasperreport_百度百科 ...

  2. dict、defaultdict 和 OrderedDict 比较

    一.dict.defaultdict 和 OrderedDict 常见的方法比较 dict.defaultdict 和 OrderedDict 常见的方法比较   dict defaultdict O ...

  3. window nginx 基础命令

    在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍.(说明:打开cmd窗口) 1.启动: C:\serv ...

  4. element vue 表格编辑

    https://xuliangzhan.github.io/vue-element-extends/#/editable/click1

  5. vue 源代码创建tabs

    <ul class="tabs"> <li class="li-tab" v-for="(item,index) in tabsPa ...

  6. 关系型数据库 VS 非关系型数据库

    一.关系型数据库? 1.概念 关系型数据库是指采用了关系模型来组织数据的数据库.简单来说,关系模式就是二维表格模型. 主要代表:SQL Server,Oracle,Mysql,PostgreSQL. ...

  7. JQ高级

    一.选择器 css语法选择器 $('css3 选择器位‘) 索引匹配 $('div:eq(0)') $('div').eq(0) 内容 $('div:contains(标签文本内容)') // 注:采 ...

  8. 输入法无法切换 win10

    https://jingyan.baidu.com/article/e2284b2b6ea3f8e2e6118d38.html https://jingyan.baidu.com/article/ce ...

  9. Day057--django

    1. http协议 请求的格式(request ---浏览器向服务器发送的消息) 请求方式: URL HTTP/1.1\r\n K1:V1\r\n K2:V2\r\n \r\n 请求正文/请求体(ge ...

  10. jemter+ant+jenkins进行集成测试

    一下为我学习的一些笔记: 一.安装配置ant 安装地址:http://ant.apache.org/ 1.下载ant一路傻瓜式安装 2.配置ant环境变量:path下配置ant的bin路径 3.将jm ...