题目链接: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. MessageDigest 加密和解密2

    package com.drawthink.platform.util; import java.security.MessageDigest; import java.security.NoSuch ...

  2. 5.17领扣--Arrays.copyOf()方法

    ?? 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同 ...

  3. SQL Server之存储过程

    存储过程的概念 存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行. 存储过程中可以包含逻辑控制语句和数据操纵语句,它 ...

  4. 学习廖雪峰的Python教程之第一个Python程序

    一.命令行模式和Python交互模式的区分 命令行模式: Python交互模式 二.文本编辑器 1.绝对不能用Word和Windows自带的记事本.Word保存的不是纯文本文件,而记事本会自作聪明地在 ...

  5. 【sqli-labs】 less39 GET -Stacked Query Injection -Intiger based (GET型堆叠查询整型注入)

    http://192.168.136.128/sqli-labs-master/Less-39/?id=1;insert into users(id,username,password) values ...

  6. SQL 分组

  7. Memcached 之内存管理与删除机制

    一.内存的碎片化 如果用c语言直接 malloc,free 来向操作系统申请和释放内存时,在不断的申请和释放过程中,形成了一些很小的内存片断,无法再利用,这种空闲,但无法利用内存的现象称为内存的碎片化 ...

  8. Swiper 3D flow轮播使用方法

    swiper 的3d轮播效果,移动端适用 (1). 如需使用Swiper的3d切换首先加载3D flow插件(js和css). <head> <link rel="styl ...

  9. Doxyfile中插入图片

    下面讲一下如何在doxyfile中插入图片 在查看别人写的文档的过程中,看到可以在doxyfile中插入图片,对此十分的好奇,所以拿出来研究一下 那么这是如何实现的? 根据代码,可以看到如下的注释 @ ...

  10. sql server安装出现的一点小问题