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 ...
随机推荐
- Net Core WebApi几种版本控制对比
Net Core WebApi几种版本控制对比 一.版本控制的好处: (1)有助于及时推出功能, 而不会破坏现有系统. (2)它还可以帮助为选定的客户提供额外的功能. API 版本控制可以采用不同的方 ...
- js页面可视区域懒加载
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java_面向对象的 static 和 abstract
static:表示静态的 static:可以用来修饰属性.方法.代码块(或初始化块).内部类. 一.static修饰属性(类变量): public class TestStatic { //stati ...
- IE盒子模型和W3C盒子模型
IE盒模型出现在ie5.5以下的版本当中,ie6以上就实行W3C盒模型. box-sizing有两个属性,border-box和content-box. border-box对应传统的盒子模型,即ie ...
- SQL数据库基础二
- idea2018.1.2 激活
https://www.cnblogs.com/ycjcham/p/8724451.html
- 利用任务计划自动删除指定日期的SQLServer备份文件
利用任务计划自动删除指定日期的SQLServer备份文件 命令FORFILES [/P pathname] [/M searchmask] [/S] [/C command] [/D ...
- 香港城市大学:全球首创3D打印微型机器人技术 有望作治疗癌症用途
香港城市大学(香港城大)的研究团队开发出了全球首创以磁力控制的3D打印微型机器人,该微型机器人技术能做到在生物体内精准运载细胞到指定的位置.新研发的微型机器人有望应用在治疗癌症的靶向治疗,并为细胞层面 ...
- mysql数据库操作手册
1 存储过程的写法 以下是一个带有入参的存储过程模板, #删除方案-存储过程 CREATE PROCEDURE procPersonAppointRecallPlanByPlanUuidDelet ...
- python_107_ __metaclass__ 元类
类默认是由 type 类实例化产生,type类中如何实现的创建类?类又是如何创建对象? 答:类中有一个属性 __metaclass__,其用来表示该类由 谁 来实例化创建,所以,我们可以为 __met ...