[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 ...
随机推荐
- tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("arg0:0", shape=(), dtype=float32, device=/device:CPU:0)'
tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype flo ...
- tesseract的编译安装
需要安装: <span style="font-family:'Microsoft YaHei';font-size:14px;">apt-get install au ...
- SpringCloud-day04-Eureka高可用集群配置
5.4Eureka高可用集群配置 在高并发的情况下一个注册中心难以满足,因此一般需要集群配置多台. 我们再新建两个module microservice-eureka-server-2002, m ...
- Unity中建立文本保存数据
public void CreateYunYD() { GameToolsManager.Instance.effectType = EFFECTTYPE.YunYD; CreateYunOrWu(& ...
- java百度云推送
实体类: import java.io.Serializable; import javax.persistence.Entity; /** * * @Version : 1.0 * @Author ...
- cat <<EOF
1.cat >file记录的是键盘输入,相当于从键盘创建文件,并且只能创建新文件,不能编辑已有文件.>是数据重导向,会将你输入的文本内容输出到file中. 2.cat <<EO ...
- 编写一个lambda,接受两个int,返回它们的和
void counter(int i, int b) { auto count = [i,b]{ return i + b; }; cout<< count(); } int main() ...
- 100-days: twenty
Title: Apple's 'show time(好戏开幕)' event(发布会) puts the spotlight on subscription services Apple's 'sho ...
- php hook编程机制
说明 hook,中文翻译为钩子,编程中的钩子类似我们现实中的钩子,需要挂在东西的时候 直接挂载到上面即可.程序中也是,需要运行的代码挂载到上面即可. 具体思想就是:在项目代码中, ...
- Linux debug
proc文件系统中可以查看一些正在运行的变量如device-tree sh-3.2# cat /proc/device-tree/ #address-cells fixedregulator@9/ # ...