LeetCode(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.
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?
分析
给定包含n个元素的序列其元素为n+1元素序列{0,1,2,...,n}中的n个,找出缺失元素。
方法一:排序法 时间O(nlogn) 空间O(1)
现将序列元素排序,然后一次遍历i从0到n−1,若nums[i]!=i,则i便是缺失元素,若循环正常结束,则缺失元素为n;
方法二:
时间O(n) 空间O(1)
由题意,大小为n的数组里的所有数都是0−n之间的数,作为等差数列,如果没有缺失的时候它的和是能O(1)计算出来的,所以我们遍历一遍记录最大、最小和数组和,用期望数组和减去实际数组和,就是缺失的整数。
AC代码
class Solution {
public:
int missingNumber(vector<int>& nums) {
if (nums.empty())
return 0;
sort(nums.begin(), nums.end());
for (unsigned i = 0; i < nums.size(); ++i)
{
if (nums[i] != i)
return i;
}//for
return nums.size();
}
};
LeetCode(268) Missing Number的更多相关文章
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(202) Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- 【LeetCode OJ 268】Missing Number
题目链接:https://leetcode.com/problems/missing-number/ 题目:Given an array containing n distinct numbers t ...
- 位运算(4)——Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
- LeetCode(136) Single Number
题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
随机推荐
- nginx一个简单的反向代理设置
location /aaaaa/ { proxy_pass http://localhost:8080/aaaaa/; } 经过配置,现在访问 http://localhost/aaaaa/ 就会 ...
- 字节码技术---------动态代理,lombok插件底层原理。类加载器
字节码技术应用场景 AOP技术.Lombok去除重复代码插件.动态修改class文件等 字节技术优势 Java字节码增强指的是在Java字节码生成之后,对其进行修改,增强其功能,这种方式相当于对应用 ...
- DHTMLX 使用汇总
1.dhtmlxGrid 底部总出现滚动条 ------------------------------------------ 发现使用DHTMLXGRID时 GRID 底边总有 滚动条 测试 ...
- python+selenium之多表单切换
在Web应用中经常会遇到fram/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于fram/iframe表单内嵌套页面上的元素无法直接定位.这是需要通过swit ...
- [windows]清除访问共享的用户和密码信息
方法一: 操作步骤:进入cmd命令界面-->输入:net use(查看列表)-->输入:net use * /delete(清空列表)-->输入:y 回车确认即可. [查看已记录的登 ...
- UVA 147 Dollars 刀了(完全背包,精度问题)
题意:一样是求钱的转换方案数,但是这次单位下降到分,但给的是元为单位的,所以是浮点的,但是固定有两位小数. 思路:数据都放大100倍来计算,去除精度问题,转成整型时要注意精度.即使给的是0.02,乘以 ...
- BZOJ 3712: [PA2014]Fiolki 倍增+想法
3712: [PA2014]Fiolki Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 437 Solved: 115[Submit][Status ...
- RYU的GUI安装
1. RYU安装 Ubuntu14.04 LTS 1.sudo apt-get install git python-pip libxml2-dev libxslt1-dev python2.7-de ...
- ThinkPHP笔记——开启debug调试模式
debug+trace模式可以查看开发过程中TP的错误信息,可以更好地帮助开发者debug.但是debug模式的开启还不是简单的在配置文件中中设置就可以的,经过查资料摸索,找到一种有效的方法. 首先在 ...
- 使用nginx搭建一个简单的负载均衡
在windows系统上使用IIS模拟出两个不同服务器的站点: 然后再NGINX使用轮询机制配置两个服务器以及虚拟服务器的端口: 需要注意的是,配置虚拟代理域名的话需要找到windowsC盘下的host ...