268. Missing Number -- 找出0-n中缺失的一个数
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
(1)
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size(), ans = ;
for(int i = ; i < n; i++)
{
ans ^= nums[i] ^ (i+);
}
return ans;
}
};
异或0-n,异或nums。
异或0还等于原来的数。
(2)
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size(), sum = n*(n+) / ;
for(int i = ; i < n; i++)
{
sum -= nums[i];
}
return sum;
}
};
求和相减。
268. Missing Number -- 找出0-n中缺失的一个数的更多相关文章
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- 268 Missing Number 缺失的数字
给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数.案例 1输入: [3,0,1]输出: 2案例 2输入: [9,6,4,2,3,5,7, ...
- 笔试题&面试题:找出一个数组中第m小的值并输出
题目:找出一个数组中第m小的值并输出. 代码: #include <stdio.h> int findm_min(int a[], int n, int m) //n代表数组长度,m代表找 ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- Entity Framework 6 Recipes 2nd Edition(9-3)译->找出Web API中发生了什么变化
9-3. 找出Web API中发生了什么变化 问题 想通过基于REST的Web API服务对数据库进行插入,删除和修改对象图,而不必为每个实体类编写单独的更新方法. 此外, 用EF6的Code Fri ...
- 找出Java进程中大量消耗CPU
原文:https://github.com/oldratlee/useful-shells useful-shells 把平时有用的手动操作做成脚本,这样可以便捷的使用. show-busy-java ...
- 找出sql脚本中需要创建的表空间名称和数据库用户名
测试的工作中,经常会遇到项目交接或者搭建一个新的测试环境,而创建oracle数据库用户及表空间时,需要提前找出脚本中的 数据库用户名和表空间名,所以自己写了一个python脚本,自动找出sql脚本中的 ...
- 如何在EXCEL中找出第一列中不包含的第二列数据
1.找出第一列中不包含的第二列数据:=IFERROR(VLOOKUP(A:A,B:B,1,0),"无") 2.A列相同,B列相加:=SUMIF(G:G,G1,J:J)
随机推荐
- C++模拟实现JDK中的ArrayList和LinkedList
Java实现ArrayList和LinkedList的方式采用的是数组和链表.以下是用C++代码的模拟: 声明Collection接口: #ifndef COLLECTION_H_ #define C ...
- Java集合面试题
1.Java集合框架是什么?说出一些集合框架的优点? 每种编程语言中都有集合,最初的Java版本包含几种集合类:Vector.Stack.HashTable和Array.随着集合的广泛使用,Java1 ...
- asp.net timer viewstate
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- nodejs httpserver
用nodejs服务器去拿取后台的数据.(我刚开始学的nodejs的时候是一个蒙圈的宝宝 t.t,开始接触的时候,在本地搭建去拿数据.之前菜鸟的我都不知道路由是神马[囧囧]). --> index ...
- 高通平台FastMMI(FFBM模式)简介与进入方法
参考: http://blog.csdn.net/tfslovexizi/article/details/51499979 http://www.voidcn.com/blog/jimbo_lee/a ...
- [转载]LazyWriter(惰性写入器) 进程的作用
Q:What Does the LazyWriter Process Do? The LazyWriter process is a periodic process that checks th ...
- Signlar
后台内部发送到指定客户端 Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<tvHub>().Clie ...
- ElasticSearch5中文分词(IK)
ElasticSearch安装 官网:https://www.elastic.co 1.ElasticSearch安装 1.1.下载安装公共密钥 rpm --import https://artifa ...
- 微信小程序-基础内容组件
icon 图标 示例: <view class="group"> <block wx:for="{{iconSize}}"> <i ...
- 换个新的思路 代替解压jar包 例证:wechat4j 框架中的templateMsg类
很多朋友在写java的程序的时候都喜欢用第三方的jar包和框架,有可能遇到jar包中的内容已经跟不上官方开发者文档的更新,导致部分内容出错了,这个时候可能就要放弃这个jar的使用,但是这个jar中的其 ...