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 ...
随机推荐
- spring boot 事务
spring事务:默认自动提交只读:@Transactional(readOnly = true)读写:@Transactional(),因为等同于@Transactional(readOnly = ...
- 063 Unique Paths II 不同路径 II
这是“不同路径” 的进阶问题:现在考虑网格中有障碍物.那样将会有多少条不同的路径从左上角到右下角?网格中的障碍物和空位置分别用 1 和 0 来表示.例如,如下所示在 3x3 的网格中有一个障碍物.[ ...
- redis和mysql同步 终极解决方案
使用Canal,类似mysql的主从复制,实时更新 具体使用之后更新
- mysql索引方式
/* 所有MySQL列类型可以被索引.根据存储引擎定义每个表的最大索引数和最大索引长度. 所有存储引擎支持每个表至少16个索引,总索引长度至少为256字节.大多数存储引擎有更高的限制. 索引的存储类型 ...
- scrollHelper
(function ($) { var mouseScroll = function (e) { try { var origEvent = e.originalEvent; origEvent.pr ...
- 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party
银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...
- 牛客NOIP提高组(二)题解
心路历程 预计得分:100 + 40 + 30 = 170 实际得分:100 + 30 + 0 = 130 T2有一个部分分的数组没开够RE了. T3好像是思路有点小问题.. 思路没问题,实现的时候一 ...
- GreenDao 数据库升级 连接多个DB文件 或者指定不同的model&dao目录
相信很多人都用过greenDao 今天 我抽空总结下使用的时候一些小东西吧 废话不多说 下边就GreenDao 的使用遇到的问题以及解决方案记录一下吧. 1.greendao 指定不同的生成目录: S ...
- C# 对接腾讯企业邮接口----get/post请求
在无所知之的情况下.来了一个对接接口的任务,没办法,只能根据前端时候的经验硬着头皮上了,随后又整理了一下写的方法,主要包括了部门的创建.更新.删除.查找.然后他们的前提是token的获取 首先HTTP ...
- SQLServer 2012 报表服务部署配置(2)
2.当系统打开"SQL Server安装中心",则说明我们可以开始正常的安装SQL Server 2012,可以通过"计划"."安装".&q ...