(leetcode)Missing Number
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?
class Solution {
public:
int missingNumber(vector<int>& nums) {
int len = nums.size();
int sum = len*(len+)/;
for(int i = ;i<len;++i)
{
sum -= nums[i];
}
return sum;
}
};
(leetcode)Missing Number的更多相关文章
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] Missing Number (A New Questions Added Today)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode——Missing Number
Description: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one t ...
- LeetCode Missing Number (简单题)
题意: 给一个含有n个整数的数组,数组中的元素应该是0-n.现在缺了其中某1个,找出缺少的那个整数? 思路: 0-n的总和是可以直接计算的,而缺少的那个就是sum减去数组的和. int missing ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- [LintCode] Find the Missing Number 寻找丢失的数字
Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
随机推荐
- BZOJ3325 : [Scoi2013]密码
从以每一位为中心的回文串长度可以用Manacher倒推出$O(n)$对相等和不等关系. 将相等的用并查集维护,不等的连边. 然后输出方案时若还没被染过色,则求一个mex. #include<cs ...
- 【UVa】11270 Tiling Dominoes
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Linux压力测试工具
1 http_load:http://www.oschina.net/p/http_load 命令行输入man http_load 或者 http_load -h可以看到工具的使用方式: 参数说明: ...
- javascript 原型及继承
一般继承方式如下 function people(n){ this.name =n; } function worker(name,num){ people.call(this, name); thi ...
- MongoDB高可用模式部署
首先准备机器,我这里是在公司云平台创建了三台DB server,ip分别是10.199.144.84,10.199.144.89,10.199.144.90. 分别安装mongodb最新稳定版本: w ...
- nVIDIA SDK White Paper ----Vertex Texture Fetch Water
http://blog.csdn.net/soilwork/article/details/713842 nVIDIA SDK White Paper ----Vertex Texture Fetch ...
- Javascript 笔记与总结(2-1)Javascript 与 DOM
浏览器有渲染 html 的功能,把 html 源码在内存里形成一个 DOM 对象,就是文档对象. 浏览器内部有一个 js 的解释器 / 执行器 / 引擎,如 Chrome 的 V8 引擎. 在 htm ...
- Nginx 报错: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) 的解决方法
今天测试域名访问不了,登陆 Linux(Ubuntu)重启Nginx: nginx -s reload 结果报错: nginx: [error] open() : No such file or di ...
- ios-滚动导航条页面
// ViewController.m #import "ViewController.h" #import "ScrollSliderView.h" @int ...
- swift 子类继承父类
// 子类的指定构造方法必须调用父类构造方法,并确保调用发生在子类存储属性初始化之后.而且指定构造方法不能调用同一个类中的其他指定构造方法: // 便利构造方法必须调用同一个类中的其他指定构造方法(可 ...