[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笔记---列表
---恢复内容开始--- 1.列表的格式:用方括号([])来表示的,逗号来分隔元素,下标从0开始 2.根据下标来查看列表中元素,当索引为-1时,显示的是最后一个元素 print(a[0])-----1 ...
- SourceTree提交不了,报git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags origin master:master
刚下载好的Soucetree,拉好项目代码却提交不了,害的我百度了好一小会,下面我把我自己最终的解决方案介绍给大家,希望对你们有用. 首先打开 下载好的git 输入命令 ssh-keygen -t ...
- dotnet不是内部或外部的命令,也不是可运行的程序或批处理文件
该问题是由于电脑环境变量配置错误所导致.最初在网上查找的方法,是在系统环境变量path中添加以下语句: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\ ...
- 树的遍历——c#实现
树作为一种重要的非线性数据结构,以分支关系定义其层次结构,在客观世界中应用广泛.通过对树遍历,将树进行线性化处理,即遍历的结果是将非线性结构的树种节点排列成一个线性序列.其中,最常见的遍历方式包括先序 ...
- Could not HEAD 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.2.0/gradle-3.2.0.pom'.
- python--第二十一/二天总结
Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. 1 2 3 ...
- [SSM项目]Eclipse 搭建marven-web项目 hello world!
配置的种种 (仅第一次)eclipse配置好tomcat.jdk.marven: 建立项目:建立mvn project-选择mvn-web 消除警告和错误: 解决错误1-项目propriety-Jav ...
- Odoo domain 中的 like, ilike, =like, =ilike
Odoo domain 中的 like, ilike, =like, =ilike 举例说明[转] Odoo domain 中的 like, ilike, =like, =ilike Odoo d ...
- python基础 (初识函数&函数进阶)
函数基础部分 .什么是函数? 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率. 2.定义函数 定义:def 关键词开头,空格之后接函数名 ...
- [精华][推荐]CAS SSO 单点登录框架学习 环境搭建
1.了解单点登录 SSO 主要特点是: SSO 应用之间使用 Web 协议(如 HTTPS) ,并且只有一个登录入口. SSO 的体系中有下面三种角色: 1) User(多个) 2) Web 应用( ...