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的更多相关文章

  1. LeetCode 268. Missing Number (缺失的数字)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  2. [LeetCode] 268. Missing Number 缺失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  3. Java实现 LeetCode 268 缺失数字

    268. 缺失数字 给定一个包含 0, 1, 2, -, n 中 n 个数的序列,找出 0 - n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [ ...

  4. Java [Leetcode 268]Missing Number

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

  5. 33. leetcode 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  6. Leetcode 268.缺失数字 By Python

    给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2 ...

  7. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  8. 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 ...

  9. leetcode 268 Missing Number(异或运算的应用)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

随机推荐

  1. JDBC学习笔记(8)——数据库连接池(dbcp&C3P0)

    JDBC数据库连接池的必要性 一.在使用开发基于数据库的web程序时,传统的模式基本是按一下步骤: 1)在主程序(如servlet/beans)中建立数据库连接 2)进行sql操作 3)断开数据库连接 ...

  2. JSF 2 listbox example

    In JSF, <h:selectOneListbox /> tag is used to render a single select listbox – HTML select ele ...

  3. 咏南DATASNAP中间件提供免费使用了

    咏南DATASNAP中间件提供免费使用了. 百度网盘分享: 链接: http://pan.baidu.com/s/1c06Ivp2 密码: dhhm

  4. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor (链表)

    题目链接:http://codeforces.com/contest/670/problem/E 给你n长度的括号字符,m个操作,光标初始位置是p,'D'操作表示删除当前光标所在的字符对应的括号字符以 ...

  5. VS 2012单元测试和测试资源管理器

    时间:2012-08-27 17:29 来源:张善友 博客园 字体:[大 中 小] 现在您可以在您的项目当中自行选择 NUnit 或是 xUnit 之类的套件. 而且这次的测试资源管理器也完全以”信息 ...

  6. mysql CASE WHEN的基础和多种用法

    CASE计算条件列表并返回多个可能结果表达式之一. CASE 具有两种格式: 简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果. CASE 搜索函数计算一组布尔表达式以确定结果. 两 ...

  7. Kerberos和NTLM - SQL Server

    当我们使用Windows Authentication去连接SQL Server的时候,SQL Server可能会使用Kerberos或者是NTLM来进行认证,有时间就会因为认证失败的缘故造成各种登录 ...

  8. hibernate-mapping的各种属性配置

    先给出一份常见的持久化类配置文件大概熟悉一下 <strong><spanstyle="font-size: 18px;"><hibernate-map ...

  9. 脚本命令高级Bash脚本编程指南(31):数学计算命令

    题记:写这篇博客要主是加深自己对脚本命令的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 高等Bash脚本编程指南(31):数学盘算命令 成于坚持,败于止步 操作数字 factor ...

  10. Chrome插件开发 尝试1

    1.新建文件夹 如图:整个项目的结构 2.新建一个名为manifest.json的文件,编码模式为utf-8,(可以先建好txt文件然后再将文件后缀txt改为json) 3.用记事本写入代码如下:(m ...