[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 ...
随机推荐
- python的apidoc使用
一.apidoc的安装 npm install apidoc -g -g参数表示全局安装,这样在哪儿都能使用. 二.apidoc在python接口代码中的使用 def index(): "& ...
- UniRX简述
UniRX:是一个Unit3D的编程框架,专注于解决异步逻辑,使得异步逻辑的实现更加简单优雅. 例如:实现“只处理第一次鼠标点击事件”: Observable.EveryUpdate() .Where ...
- PhoenixFD插件流体模拟——UI布局【Output】详解
Liquid Output 流体输出 本文主要讲解Output折叠栏中的内容.原文地址:https://docs.chaosgroup.com/display/PHX3MAX/Liquid+Outp ...
- PhoenixFD插件流体模拟——UI布局【Dynamics】详解
流体动力学 本文主要讲解Dynamics折叠栏中的内容.原文地址:https://docs.chaosgroup.com/display/PHX3MAX/Liquid+Dynamics 主要内容 Ov ...
- python--第十六天总结(bootstrap)
一. 实现原理 网格布局是通过容器的大小,平均分为12份(可以修改),再调整内外边距,和表格布局有点类似但是也存在区别. 实现步骤如下: (1) 数据行.row 必须包含在容器.container 中 ...
- java批量生成用例脚本-保留字关键词的用例脚本生成
20171012新补丁包程序,程序整理了全部的关键字和保留字,支持 字段a = "字段b" 与 字段a = 字段b,并做了代码重构.对补丁包进行关键字专项测试,验证关键字保留字作为 ...
- 高性能mysql 事务笔记
事务的四大特性原子性.一致性.隔离性.持久性, 事务隔离的四大隔离级别: READ UNCOMMITTED(未提交读), 在 read uncommitted级别,事务中的修改,及时没有提交,对其他事 ...
- 题目--统计一行文本的单词个数(PTA预习题)
PTA预习题——统计一行文本的单词个数 7-1 统计一行文本的单词个数 (15 分) 本题目要求编写程序统计一行字符中单词的个数.所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以 ...
- Codeforces Round #552 (Div. 3) B题
题目链接:http://codeforces.com/contest/1154/problem/B 题目大意:给出n个数,每个数都可以加上或减去这个一个数D,求对这n个数操作之后当所有数都相等时,D的 ...
- C# DataTable抽取Distinct数据(不重复数据)[z]
DataTable dataTable; DataView dataView = dataTable.DefaultView; DataTable dataTableDisti ...