【LeetCode OJ 268】Missing Number
题目链接:https://leetcode.com/problems/missing-number/
题目: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,1,2...n,找出数组中遗漏的那个数。
演示样例代码例如以下:
public class Solution
{
public int missingNumber(int[] nums)
{
//首先对数组进行排序
Arrays.sort(nums);
int startData=nums[0];
for(int i=1;i<nums.length;i++)
{
//检查数组是否连续
if((startData+1)==nums[i])
{
startData=nums[i];
}
else
{
return startData+1;
}
}
/**
* 假设数组是连续的
* 起始值不是0。则返回0,否则返回数组末尾数的下一个自然数
*/
if(startData==nums[nums.length-1])
{
if(nums[0]>0)
return 0;
else
return nums[nums.length-1]+1;
}
return 0;
}
}
【LeetCode OJ 268】Missing Number的更多相关文章
- 【LeetCode OJ 136】Single Number
题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...
- 【LeetCode算法-9】Palindrome Number
LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...
- 【LeetCode OJ 016】3Sum Closest
题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...
- LeetCode(268) Missing Number
题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- 【LeetCode OJ 34】Search for a Range
题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
随机推荐
- ruby --Paperclip::NotIdentifiedByImageMagickError
首先,如果遇到这个问题,Paperclip::NotIdentifiedByImageMagickError,先检查下环境变量是否配置了ImagicMagick的路径. cmd下path 查看,首先加 ...
- 取消VS2017窗口置顶
今天打开VS2017,莫名其妙窗口置顶了,百度了一下如何取消窗口置顶,就是Ctrl+Alt+Esc组合键,就可以取消窗口置顶了,至于到底怎么会突然置顶的我也不知道emmm... /********** ...
- SQLServer2008 关于while循环
有这样一个表tbl id code name 11 a aa/bb/cc 22 b ee/rr/tt 需要将name段根据‘/’拆分开来,变成新的数据行 即: id c ...
- [转]line-height1.5和line-height:150%的区别
line-height1.5和line-height:150%的区别 一.区别 区别体现在子元素继承时,如下: 父元素设置line-height:1.5会直接继承给子元素,子元素根据自己的font ...
- ArrayList 源码
1.ArrayList的类关系: 2.属性及方法 2.1 构造 三个构造方法分别对应: 通过传入初始化容器大小构造数组列表 ...
- macOS下登录store或者xcode等应用时提示【this action could not be completed】
sudo mkdir -p /Users/Shared sudo chown root:wheel /Users/Shared sudo chmod -R 1777 /Users/Shared === ...
- 2016年8月17日 内省(1)18_黑马程序员_使用beanUtils操纵javabean
8.内省(1):18_黑马程序员_使用beanUtils操纵javabean 1.导入两个包: 2.调用静态方法. 9.泛型 map.entrySet() :取出map集合的键值对组成一个set集合. ...
- Generics of a Higher Kind
http://adriaanm.github.io/files/higher.pdf https://www.atlassian.com/blog/archives/scala-types-of-a- ...
- 团体程序设计天梯赛-练习集-L1-048. 矩阵A乘以B
L1-048. 矩阵A乘以B 给定两个矩阵A和B,要求你计算它们的乘积矩阵AB.需要注意的是,只有规模匹配的矩阵才可以相乘.即若A有Ra行.Ca列,B有Rb行.Cb列,则只有Ca与Rb相等时,两个矩阵 ...
- kernel学习单
lock (spin_lock, mutex, rw_mutex/spinlock) waitqueue, tasklet, softIRQ, hardIRQ basic struct (atomic ...