268. Missing Number
2016-07-12 19:43
197 查看
Given an array containing n distinct numbers taken from
For example,
Given nums =
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
Credits:
Special thanks to
@jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
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?
Credits:
Special thanks to
@jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
class Solution { public: int missingNumber(vector<int>& nums) { double sum=(nums.size())*(nums.size()+1)/2; for(int i=0;i<nums.size();++i) { sum-=nums[i]; } return sum; } };
相关文章推荐
- Centos环境下Redis的安装
- 一些关于API层面的优化的VR渲染优化方法
- 最大公约数和最小公约数
- Java编程思想第四版第十二章学习——通过异常处理错误(1)
- php程序员应具有的7种能力
- leetCode_Flatten Binary Tree to Linked List
- hdu 1019Least Common Multiple(最小公倍数)
- JavaScript中获取表单信息并添加在表格中
- 小学生进位问题
- include标签布局以及自定义标题
- 第2章 一切都是对象
- android TextView、EditText对部分内容设置颜色、字体、超链接、图片
- HDU-1849-Rabbit and Grass【Nim博弈】
- 怎么做能尽量使得自己的孩子不成为熊孩子?
- C# DataTable构造自定义数据的一个使用问题
- Spring 小示例
- CCNU ACM 2016夏季集训·day2比赛
- 欢迎使用CSDN-markdown编辑器
- 【C++】 求两string日期间隔天数
- Mysql 5.7修改密码