[LeetCode] Missing Number (A New Questions Added Today)
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?
这道题的话其实为了简单一点是需要用到一些数学知识的。我们已知的是这里面只有一个missing的数。
如果给你一个连续的整数列,计算总的sum的公式=[总个数*(总个数+1)]/2。
那么简化一点,先计算出应该有的sum然后减去现在的sum,得到的差就应该是那个我们miss的数了。
注意一点,我们的数列是从0开始,但是计算本应的sum的时候依然应该是[nums.length*(nums.length+1)]/2来计算,因为我们要加上一位作为被miss的数的位置。所以结果就是总个数还是nums.length不变。
代码如下。~
public class Solution {
public int missingNumber(int[] nums) {
int sum=0;
for(int i=0;i<nums.length;i++){
sum=sum+nums[i];
}
return (nums.length*(nums.length+1))/2-sum;
}
}
[LeetCode] Missing Number (A New Questions Added Today)的更多相关文章
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- (leetcode)Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] Ugly Number (A New Question Added Today)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- LeetCode——Missing Number
Description: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one t ...
- LeetCode Missing Number (简单题)
题意: 给一个含有n个整数的数组,数组中的元素应该是0-n.现在缺了其中某1个,找出缺少的那个整数? 思路: 0-n的总和是可以直接计算的,而缺少的那个就是sum减去数组的和. int missing ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- [LintCode] Find the Missing Number 寻找丢失的数字
Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...
随机推荐
- CentOS 快速安装pip
python的很多组件都必须依靠pip来安装,比如elasticsearch驱动.postgres驱动 Python2.7以后的版本自带pip,centos6.5之前yum自带的python为2.6, ...
- TOMCAT服务器不写端口号、不写项目名访问项目、虚拟目录配置
一.不写端口. 这个问题都被问烂了,因为TOMCAT默认的访问端口为8080,而TCP/IP协议默认80端口访问,大家之所以看到别的网站都不写端口号是因为人家用的的80端口访问的,而80端口因为的TC ...
- php pod
//PDO:数据访问抽象层 //dsn:数据源: //带有事务功能: $dsn = "mysql:host=localhost;dbname=mydb"; ——建立数据源 //造p ...
- Redstone 云观象台 服务器部署 - Nginx配置文件
以下信息仅针对Redstone的Ngxin配置文件进行更新. web服务器Nginx配置文件结构如下: /etc/nginx/nginx.conf # For more information on ...
- POJ 1322 Chocolate(母函数)
题目链接:http://poj.org/problem?id=1322 题意: 思路: double C[N][N]; void init() { C[0][0]=1; int i,j; for(i= ...
- Using Git subtrees to split a repository
https://lostechies.com/johnteague/2014/04/04/using-git-subtrees-to-split-a-repository/ We are in a p ...
- 阿里云yum源
wget -O /etc/yum.repos.d/CentOS-Base-aliyun.repo http://mirrors.aliyun.com/repo/Centos-6.repo 参考:htt ...
- JS里面匿名函数的调用 & 变量作用域的实验
参考 http://www.educity.cn/wenda/54753.html 已实验验证结果正确. 1.下列哪些正确?(B.C) A.function(){ alert("Here!& ...
- NHibernate 二级缓冲
session.CreateCriteria(typeof(SysModuleFields)).SetCacheable(true).List<SysModuleFields>(); se ...
- R语言数据读入函数read.table
1.read.table:可以读TXT也可以读CSV (1)file:文件名 (2)header:是否包含表头 (3)sep:分隔符,如果不设定默认是空格 (4)dec:标志小数点符号,有些国家的小数 ...