【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 ...
随机推荐
- CodeForces Round #555 Div.3
A. Reachable Numbers 代码: #include <bits/stdc++.h> using namespace std; ; int N; set<int> ...
- lazyMan
class Lazyman { constructor() { this.tasks = []; this.init(); } init() { const task = () => { con ...
- JS学习笔记:(二)回流和重绘
在搞清楚回流和重绘的概念之前,我们要清除浏览器的渲染过程. 解析生成DOM Tree(此时包含所有节点,包括display:none); 根据CSS Object Module(CCSSOM)计算节点 ...
- 【BZOJ4028】[HEOI2015]公约数数列(分块)
[BZOJ4028][HEOI2015]公约数数列(分块) 题面 BZOJ 洛谷 题解 看一道题目就不会做系列 首先\(gcd\)最多只会有\(log\)种取值,所以我们可以暴力枚举出所有可能的\(g ...
- 函数内部还是不要使用 strtok()
今天在调试程序的时候,遇到一个奇怪的事情,一开始担心是代码存在内存溢出引起的,花了半个小时没找到原因. 在吃饭的时候,突然想起可能是 strtok() 引起的,查找调用的函数,果然发现在函数中使用了 ...
- mac下安装maven
在mac下 使用 brew安装,brew install maven 查看maven版本 mvn -version 打开Terminal,输入以下命令,设置Maven classpath 添加下列两行 ...
- Vue(四)组件
组件的复用 这里的工程和上一节的一样 先建立一个组件 MyButton.vue <template> <button @click="count++">Yo ...
- 阿里百川SDK初始化失败 错误码是203
由idea换到Androidstudio 了,结果报这个错,之前好好的啊!!! 设置问题:
- Android activity创建三部曲
1.新建类继承Activity或其子类,现在一般继承AppCompatActivity public class TestActivity extends AppCompatActivity { @O ...
- Axure PR的使用
1759139 王越 Axure RP是美国Axure Software Solution公司旗舰产品,是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家能够快速创建应用软件或W ...