题目链接: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的更多相关文章

  1. 【LeetCode OJ 136】Single Number

    题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...

  2. 【LeetCode算法-9】Palindrome Number

    LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...

  3. 【LeetCode OJ 016】3Sum Closest

    题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...

  4. LeetCode(268) Missing Number

    题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...

  5. 【LeetCode OJ 232】Implement Queue using Stacks

    题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...

  6. 【LeetCode OJ 34】Search for a Range

    题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...

  7. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  8. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  9. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

随机推荐

  1. [SGU 199] Beautiful People

    [SGU 199] Beautiful People The most prestigious sports club in one city has exactly N members. Each ...

  2. 题解报告:hdu 4764 Stone(巴什博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 Problem Description Tang and Jiang are good frie ...

  3. 遍历WPF DataGrid单元格

    using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; ...

  4. SQL Server跨库跨服务器访问实现

    我们经常会遇到一个数据库要访问另一个数据库,或者一台服务器要访问另一台服务器里面的数据库. 那么这个如何实现的呢? 相信看完这篇文章你就懂了! 同一台服务器跨库访问实现 1. 首先创建两个数据库Cro ...

  5. 浅谈ByteBuffer转换成byte[]时遇到的问题

    有些时候我们要把ByteBuffer转换成byte[]来使用.于是很多时候会用以下代码来转换: ByteBuffer buf; .....(一些往buffer写数据的操作) byte[] bs= ne ...

  6. js基础标签用法

    js是脚本语言,开始标签<script type="text/javascript">.......结束标签</script>.script通常放在< ...

  7. 【SQL】DUAL表

    DUAL表是Oracle系统中对所有用户可用的一个实际存在的1行1列的表,这个表不能用来存储信息,在实际应用中仅用来执行SELECT语句.可以使用DUAL表来查询系统的信息. --dual是1行1列的 ...

  8. 据说现在很缺设计师,阿里云的LOGO是用键盘打出来的……

    今天,阿里云发布了自己的新LOGO,官方的解读是:“[]”这个呢其实是代码中常用的一个符号,代表着计算:中间的“—”则代表流动的数据.在官网等平台上,阿里云新LOGO是动态的,中间的“—”会匀速移动, ...

  9. 【sqli-labs】 less32 GET- Bypass custom filter adding slashes to dangrous chars (GET型转义了'/"字符的宽字节注入)

    转义函数,针对以下字符,这样就无法闭合引号,导致无法注入 ' --> \' " --> \" \ --> \\ 但是,当MySQL的客户端字符集为gbk时,就可能 ...

  10. turn.js中文API 写一个翻页效果的参数详细解释

    $('.flipbook').turn({     width: 922,     height: 600,     elevation: 50,     gradients: true,     a ...