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 ...
随机推荐
- MVC3 自定义的错误页
ASP.NET MVC3中如果配置文件出错了,怎么跳转到自定义的错误页,现在参考网上的档案是说 添加 如下配置文件,并且在路径Views/Shared/下添加Error页面,测试下没有用的,请大家看看 ...
- JavaScript 函数(方法)
1 定义 1.1 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 语法: 函数就是包裹在大括号中的代码块,前面使用了关键词 function function 方法名(参数列表){ 代码 ...
- dubbo服务降级(1)
1. 在 dubbo 管理控制台配置服务降级 上图的配置含义是:consumer 调用 com.zhang.HelloService 的方法时,直接返回 null,不发起远程调用. 实际操作是:在 z ...
- 修复使用<code>XmlDocument</code>加载含有DOCTYPE的Xml时,加载后增加“[]”字符的错误
C# LINQ TO XML - Remove “[]” characters from the DTD header http://stackoverflow.com/questions/12358 ...
- Python+selenium之获取请求信息
basicConfig()所捕获的log信息.不过其开启的debug模式只能捕获到客户端像服务器发送的post()请求,而无法获取服务器所返回的应答信息. from random import ran ...
- Java 方法介绍
1.方法(函数)介绍 各种语言都有方法的概念(有的语言称其为函数或过程). 方法用于封装一段特定的逻辑功能.如执行计算或操作. 方法可以在程序中反复被调用,方法可以减少代码重复,便于程序的维护,有利于 ...
- codeforce Gym 100685F Flood (topo排序)
如果直接模拟水向周围流会TLE,因为某些个结点被重复扩展了多次, 科学做法是topo排序,每次只把入度为0的点放入队列,这样就严格保证了每个结点只被扩展一次. #include<bits/std ...
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...
- ecplise——python not configured报错
解决方法:点击window——preferences——PyDey——pythonInterprter 最后成功
- 使用ErrorProvider组件验证文本框输入
实现效果: 知识运用: ErrorProvider组件的BlinkStyle属性 //指示错误图标的闪烁时间 public ErrorBlinkStyle BlinkStyle{ get;set; } ...