【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 ...
随机推荐
- A - Petya and Strings
Problem description Little Petya loves presents. His mum bought him two strings of the same size for ...
- E - Perfect Number
Problem description We consider a positive integer perfect, if and only if the sum of its digits is ...
- 2星|《10W+走心文案是怎样炼成的》:标题党。实际是台湾创意总监的一些人生感悟和两三个很一般的创意文案
10W+走心文案是怎样炼成的 作者是台湾人,曾在台湾奥美担任创意总监,做过一些广告.本书是他的一些经验介绍. 总体来说是标题党,作者的广告基本是电视广告,跟文案也有关系,估计播放量也很容易过10W+, ...
- Mac下php连接mysql数据库失败解决办法
通过phpmyadmin连接mysql成功,但是通过php连接数据库失败,执行如下php语句 ? 1 @mysql_connect("localhost","root&q ...
- HDU_1907_基础博弈nim游戏
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- monad - the Category hierachy
reading the "The Typeclassopedia" by Brent Yorgey in Monad.Reader#13 ,and found that " ...
- 【转】在VMware中为Linux系统安装VM-Tools的详解教程
在VMware中为Linux系统安装VM-Tools的详解教程 如果大家打算在VMware虚拟机中安装Linux的话,那么在完成Linux的安装后,如果没有安装Vm-Tools的话,一部分功能将得不到 ...
- Oracle经验积累
Oracle 常用Funtion ----行转列 开始---- select u_id, wmsys.wm_concat(goods) goods_sum from shopping group by ...
- python 直接存入Excel表格
def write_excels(self, document): outwb = openpyxl.Workbook() outws = outwb.create_sheet(index=0) fo ...
- appium分层自动化的封装
1.创建一个case包,start_app的python文件 #coding=utf-8from appium import webdriverfrom util.read_init import R ...