[leetcode268]Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
, find the one that is missing from the array.
Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
解法考虑两种:数学解法和数组解法,数学解法利用连续n个数的序列特点,求和后求差计算,O(n)时间复杂度
class Solution {
public int missingNumber(int[] nums) {
int sum = (0 + nums.length) * (nums.length + 1) / 2;
for (int i = 0; i < nums.length; i++){
sum = sum - nums[i];
}
return sum;
} // public int missingNumber(int[] nums) {
// boolean[] bit = new boolean[nums.length+1];
// for (int i = 0; i < nums.length; i++) {
// bit[nums[i]] = true;
// }
// int i = 0;
// while(bit[i] == true) {
// i++;
// }
// return i;
// }
}
[leetcode268]Missing Number的更多相关文章
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
- LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...
- Missing number
Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- hdu 5166 Missing number
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutatio ...
- Missing Number, First Missing Positive
268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...
- HDU 5166 Missing number 简单数论
Missing number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) [ ...
- Missing number in array
Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missin ...
- PAT 1144 The Missing Number
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
随机推荐
- IntelliJ IDEA mybatis-generator的使用
STEP 1. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件 pop.xml <dependency> <groupId& ...
- C# 消息队列 RabbitMQ
1.引言 RabbitMQ——Rabbit Message Queue的简写,但不能仅仅理解其为消息队列,消息代理更合适. RabbitMQ 是一个由 Erlang 语言开发的AMQP(高级消息队列协 ...
- Tomcat配置https协议访问
Tomcat9配置https协议访问: https://blog.csdn.net/weixin_42273374/article/details/81010203 配置Tomcat使用https协议 ...
- 2018-2019-2 《网络对抗技术》Exp0 Kali安装
2018-2019-2 <网络对抗技术>Exp0 Kali安装 ---20165110 一.实验要求 1.下载 2.安装Kali 3.连接网络 4.共享文件 5.更新软件源 二.实验步骤 ...
- 【Spring学习】SpringMVC demo搭建
前言:今天会通过IDEA建立一个SpringMVC的demo项目,在其中会涉及到一些基础模块和相关知识,然后结合这个具体的知识点,理解清楚SpringMVC的框架原理(以图的形式展示),顺藤摸瓜分析源 ...
- checkpoint防火墙SmartDashboard登录出错
SmartDashboard登录是报错:fingerprint不匹配 原因:主备机切换导致 解决:选择凌晨不影响业务的时间拔掉原备机的电源线.
- 【redis 学习系列】API的理解与使用(二)
3.哈希 几乎所有的语言都支持了哈希(hash)类型.在Redis中,哈希类型是指键值本身又是一个键值对结构,形如:value = {{field, value} ... {field, value} ...
- 2. 2A03简介
2A03简介 1.CPU 1.1 内部寄存器 1.累加寄存器A(Accumulator):8位寄存器,用于同算术逻辑单元(ALU)共同完成各种算术逻辑运算,它既为ALU提供原始操作数又担任存放ALU运 ...
- centos7 使用kubeadm 快速部署 kubernetes 国内源
前言 搭建kubernetes时看文档以及资料走了很多弯路,so 整理了最后成功安装的过程已做记录.网上的搭建文章总是少一些步骤,想本人这样的小白总是部署不成功(^_^). 准备两台或两台以上的虚拟机 ...
- Python集合框架