【easy】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 用异或的办法:
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的更多相关文章
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
- 【easy】202. Happy Number
happy number Write an algorithm to determine if a number is "happy". A happy number is a n ...
- 【easy】263. Ugly Number 判断丑数
class Solution { public: bool isUgly(int num) { ) return false; ) return true; && num % == ) ...
- 【leetcode】1228.Missing Number In Arithmetic Progression
题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...
- 181. Flip Bits【easy】
181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n ...
- 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 ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
随机推荐
- visual studio中各文件的输出路径
dll或exe输出路径一般在 配置属性->链接器->常规->输出文件 中 若该路径与 配置属性->常规 中的输出目录+目标文件名+目标文件扩展名不一致,可能会有提示,建议保持一 ...
- mysql 在线加索引 锁表
mysql在线修改表结构大数据表的风险与解决办法归纳 - 王滔 - 博客园 http://www.cnblogs.com/wangtao_20/p/3504395.html MySQL 加索引 加字段 ...
- [转帖]创建文件或修改文件时间 touch
Linux命令(五)创建文件或修改文件时间 touch https://www.cnblogs.com/ay-a/p/7900274.html touch -t .x86_64.rpm 记得 wind ...
- Python网络爬虫精要
目的 学习如何从互联网上获取数据.数据科学必须掌握的技能之一. 本文所用到的第三方库如下: requests, parsel, selenium requests负责向网页发送HTTP请求并得到响应, ...
- npm install报错
npm ERR! C:\Users\xxxxxx\AppData\Roaming\npm-cache\_logs\2019-02-27T07_48_47_283Z-debug.log " ...
- ClientValidationEnabled
ClientValidationEnabled 去掉这个的时候就无效了,前端验证 <add key="ClientValidationEnabled" value=" ...
- java的clone()方法
什么是"clone"? 在实际编程过程中,我们常常要遇到这种情况:有一个对象A,在某一时刻A中已经包含了一些有效值,此时可能 会需要一个和A完全相同新对象B,并且此后对B任何改动都 ...
- Android面试题集合
https://www.jianshu.com/p/718aa3c1a70b https://www.jianshu.com/p/2dd855aa1938 https://www.jianshu.co ...
- Elasticsearch 目录总结
一:Elasticsearch (及工具插件)安装相关: 二:Elasticsearch 数据新增相关: 三:Elasticsearch 数据删除相关: 四:Elasticsearch 数据更新相关: ...
- Python2和Python3安装教程
当同时安装Python2和Python3后,如何兼容并切换使用详解(比如pip使用) python成了2.7而py成了3.7,呵呵! 当同时安装Python2和Python3后,如何兼容并切换使用详解 ...