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. Django(五)母版继承、Cookie、视图装饰器等

    大纲 一.内容回顾 补充:默认值 补充:命名空间 二.模板语言 1.母版继承 2.include 3.自定义simple_tag 三.Cookie Cookie 使用总结 四.视图 1.获取用户请求相 ...

  2. jQuery的deferred对象实战应用(附:Exchar动态多条数据展示并在topic展示详细数据)

    解决三个后台请求都成功后先比较数据再处理数据的需求 今天碰到了一个问题,我需要创建一个图表,但是需要请求三个接口才能比较出指标数据,于是就看到了deferred对象 理论的补充在这里:http://w ...

  3. VisualStudio神级插件Resharper技巧基础入门到骨灰玩家使用全教程+Resharper性能优化

    原文地址:https://www.masuit.com/21/resharper 破解地址:https://www.masuit.com/20/resharper 官方文档:https://www.j ...

  4. 【LUOGU???】WD与地图 整体二分 线段树合并

    题目大意 有一个简单有向图.每个点有点权. 有三种操作: 修改点权 删除一条边 询问和某个点在同一个强连通分量中的点的前 \(k\) 大点权和. \(n\leq 100000,m,q\leq 2000 ...

  5. openstack虚拟机内核崩溃问题解决

    openstack对接的kvm虚拟化环境,创建虚拟机后无法进如系统,一直卡在call Trace . 解决办法: 更改配置文件的cpu-model,libvirt_cpu_mode = custom ...

  6. [oracle]查询一个表中数据的插入时间

    select to_char(scn_to_timestamp(ORA_ROWSCN),'yyyy-mm-dd hh24:mi:ss') insert_time from tablename;

  7. php对某个页面设置基础认证登录设置

    记录下. 对某个页面设置认证 代码最开始添加: <?php $user = 'test'; $password = '111111'; // 通过HTTP认证进行验证 if (!isset($_ ...

  8. SpringMVC 文件上传下载

    目录 文件上传 MultipartFile对象 文件下载 上传下载示例 pom.xml增加 创建uploadForm.jsp 创建uploadForm2.jsp 创建userInfo.jsp spri ...

  9. Java集合的总结

    参考博客: http://www.jianshu.com/p/63e76826e852 http://www.cnblogs.com/LittleHann/p/3690187.html https:/ ...

  10. time series analysis

    1 总体介绍 在以下主题中,我们将回顾有助于分析时间序列数据的技术,即遵循非随机顺序的测量序列.与在大多数其他统计数据的上下文中讨论的随机观测样本的分析不同,时间序列的分析基于数据文件中的连续值表示以 ...