[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 ...
随机推荐
- PowerDesigner中name和code取消自动关联
PowerDesigner中,如果修改了某个字段的name,其code也跟着修改,如果想取消,可以如下操作 解决方法如下: 1.选择Tools->GeneralOptions...菜单,出现Ge ...
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
转载地址:http://blog.csdn.net/mad1989/article/details/9147661 ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当 ...
- eclipse中启动tomcat报错 java.lang.ClassNotFoundException
之前启动还好好的,某次启动tomcat就莫名其妙的报了这个java.lang.ClassNotFoundException的错. 检查maven依赖包,发现这个类是存在的. 然后一通clean操作 ...
- Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)
题目链接 这个题取模的时候挺坑的!!! 题意:div(x , b) / mod(x , b) = k( 1 <= k <= a).求x的和 分析: 我们知道mod(x % b)的取值范围为 ...
- Java中的private、protected、public和default的区别
(1)对于public修饰符,它具有最大的访问权限,可以访问任何一个在CLASSPATH下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. (2)对于protec ...
- 函数lock_rec_enqueue_waiting
type_mode基础上 加上 LOCK_WAIT 表示等待状态 /****************************************************************** ...
- [转载]initwithcoder和 initwithframe
大前提是UIViewController有一个UIView.同时,需要厘清两个概念,创建一个类和实例化一个类.在XCode中创建一个类和实例化一个类很容易区分,但是在IB(Interface Buil ...
- 15.Object-C--浅谈Foundation框架OC数组NSArray与NSMutableArray
昨天总结了一下NSString与NSMutableString,今天我在这里总结一下NSArray与NSMutableArray. NSArray数组是:不可变数组. nil 是数组元素结束的标记.O ...
- 【JavaScript学习笔记】鼠标样式
style="cursor:hand" 手形 style="cursor:crosshair" 十字形 style="cursor ...
- js屏弊错误
<SCRIPT language=javascript> <!-- window.onerror=function(){return true;} // --> </SC ...