LeetCode 268
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?
/*************************************************************************
> File Name: LeetCode268.c
> Author: Juntaran
> Mail: Jacinthmail@gmail.com
> Created Time: Tue 10 May 2016 06:19:13 PM CST
************************************************************************/ /************************************************************************* 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? ************************************************************************/ #include <stdio.h> /* 通用方法 */
int missingNumber( int* nums, int numsSize )
{
int sum = ( + numsSize) * (numsSize+) / ;
int ret = ;
int i;
for( i=; i<numsSize; i++ )
{
ret += nums[i];
}
ret = sum - ret;
return ret;
} /* 如果数据是从0递增的话可以使用以下方法 */
/* 测试用例包含不是纯从0递增数组,所以此方法LeetCode不通过 */
int missingNumber2( int* nums, int numsSize )
{
int i; for( i=; i<numsSize; i++ )
{
if( i != nums[i] )
{
return i;
}
}
return numsSize;
} int main()
{
int nums[] = { , , };
int numsSize = ; int ret = missingNumber2( nums, numsSize );
printf("%d\n", ret); return ;
}
LeetCode 268的更多相关文章
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Java实现 LeetCode 268 缺失数字
268. 缺失数字 给定一个包含 0, 1, 2, -, n 中 n 个数的序列,找出 0 - n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [ ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- 33. leetcode 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Leetcode 268.缺失数字 By Python
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2 ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告
1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...
- leetcode 268 Missing Number(异或运算的应用)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
随机推荐
- google proto buffer安装和简单示例
1.安装 下载google proto buff. 解压下载的包,并且阅读README.txt,根据里面的指引进行安装. $ ./configure $ make $ make check $ mak ...
- 【WPF】【火车站点信息查询】
全文涉及到的是C#和XAML 如果这两门语言并非你喜欢的语言,那可以关闭本网页了 本文介绍的是什么? 一个火车站点信息查询软件 本文涉及到的WPF基本知识 Task async await WebCl ...
- Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)
相关资料: http://blog.csdn.net/earbao/article/details/46514313 结果注意: 1.Use IdContext.IdGlobal 这两个单元2.不能使 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- Protobuf一键生成代码bat文件
最近在摆弄Unity的Socket,需要用到Protobuf,一般都会有多个协议文件,所以研究了下bat的批处理,下面给出批处理文件代码: @echo off ::协议文件路径, 最后不要跟“\”符号 ...
- matlab eps中文乱码的解决方法
直接存成eps总是乱码 最优解决方法是matlab print 保存成jpg,之后用adobe acrobat pro 打开jpg文件另存为eps
- word2010 ctrl v not work
终于解决了word 2010中ctrl v 不能用的问题. 0 word ctrl c 可以用,右键粘贴可以正常使用,快捷键ctrl v不能用. 1 在excel中ctrl c 和ctrl v,可以正 ...
- opennebula 自定义安装目录
/bin//mkinstalldirs /usr/local/lib /bin//mkinstalldirs /usr/local/include /bin//mkinstalldirs /usr/l ...
- eclipse中异常的快捷键
选中你要try的代码,alt+shift+z 就会弹出一个菜单,里面有个try 选项
- Ectouch修改虚拟销售数量的方法
1.参考:http://zhidao.baidu.com/link?url=5OEkRlKqtRcmnO6iyW2pq-gw1aj-1S6QdImmBkQZHHt6tcvT50aIf_1nibP3T6 ...