Missing Number ——LeetCode
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?
题目大意:给定一个数组,长度为n,包含从0~n中的n个数,问哪个数不存在?
思路:加减就可以了。
public int missingNumber(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
int res = 0;
for(int i=0;i<nums.length;i++){
res+=nums[i];
res-=i+1;
}
return -res;
}
Missing Number ——LeetCode的更多相关文章
- Missing Number @leetcode
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 【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 ...
- [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 ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
- Missing number
Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...
- 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) [ ...
随机推荐
- jsp带参转链接
1.jsp:forward page <jsp:forward page="show.jsp"> <jsp:param name="data" ...
- HTML5 FileReader读取Blob对象API详解
使用FileReader对象,web应用程序可以异步的读取存储在用户计算机上的文件(或者原始数据缓冲)内容,可以使用File对象或者Blob对象来指定所要读取的文件或数据.其中File对象可以是来自用 ...
- mysql数据库优化日志(更)-howyue
1)记一次首页查询优化 优化前: 优化后: 主要优化: 1.select查询只查询需要字段: 2.where条件字段添加索引:
- .NET 菜单如何链接到指定的框架
这2天我看了网络上很多关于这方面的资料,很多都是抄人家的,要不就是没图说个jiba,要不就是没有说到重点,浪费大家的时间,今天我把我的心得给大家分享下,希望对大家有所帮助. 一.首先,你需要简历一个框 ...
- 用Ueditor存入数据库带HTML标签的文本,从数据库取出来后,anjular用ng-bind-html处理带HTML标签的文本
ng.module('index-filters', []) .filter('trustHtml', function ($sce) { return function (input) { retu ...
- Index Full Scan vs Index Fast Full Scan-1103
[Oracle] Index Full Scan vs Index Fast Full Scan作者:汪海 (Wanghai) 日期:14-Aug-2005 出处:http://spaces.msn. ...
- DGV属性
1.控件的SelectedCells.Count属性可以判断用户是否已经选择数据,如果大于0说明有选择的数据. 2.SelectedCells[N].Value的属性可以获取某一行数据中某列的数据,其 ...
- innerHTML/outerHTML; innerText/outerText; textContent
innerHTML v.s. outerHTML Element.innerHTML Reference: https://developer.mozilla.org/en-US/docs/Web/A ...
- Git 基础再学习之:git checkout -- file
首先明白一下基本概念和用法,这段话是从前在看廖雪峰的git教程的时候摘到OneNote的 准备工作: 新建了一个learngit文件夹,在bash中cd进入文件夹,用以下命令创建一个仓库. $ git ...
- 关于CenttOS的防火墙问题
Fix “Unit iptables.service failed to load: No such file or directory” Error In CentOS7 最近在升级CentOS7遇 ...