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 ...
随机推荐
- work5
这一次写的内容是黄金豆小游戏,由于现在偏重写服务器端.对于算法层面其实涉及不多,更多偏于工程上的架构. 总而言之本次作业的服务器核心是用web.py所写,而且为了方便其他用户写客户端,架构非常简单. ...
- Spring入门(9)-AOP初探
Spring入门(9)-AOP初探 0. 目录 什么是面向切面编程 AOP常见术语 AOP实例 参考资料 1. 什么是面向切面编程 Aspect Oriented Programming(AOP),即 ...
- Spring入门(2)-通过构造器注入Bean
Spring入门(2)-通过构造器注入Bean 前一篇文章将了最基本的spring例子,这篇文章中,介绍一下带有参数的构造函数和通过构造器注入对象引用. 0. 目录 带有参数的构造函数 通过构造器注入 ...
- [iOS UI进阶 - 6.1] 核心动画CoreAnimation
A.基本知识 1.概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对 ...
- Linux下c/c++项目代码覆盖率的产生方法
最近做了一系列的单元测试相关的工作,除了各种规范及测试框架以外,讨论比较多的就是关于代码覆盖率的产生,c/c++与其他的一些高级语言或者脚本语言相比较而言,例如 Java..Net和php/pytho ...
- HDU 5776 sum (思维题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 题目让你求是否有区间的和是m的倍数. 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续 ...
- 最大流&最小割 - 专题练习
[例1][hdu5889] - 算法结合(BFS+Dinic) 题意 \(N\)个点\(M\)条路径,每条路径长度为\(1\),敌人从\(M\)节点点要进攻\(1\)节点,敌人总是选择最优路径即最短路 ...
- 如何更改c#项目的App.config文件
动态修改App.Config 和web.Config 首先假设你的应用程序配置文件如下: <?xml version="1.0" encoding="utf-8&q ...
- iOS 通知中心 NSNotificationCenter
iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...
- 让sublime text 3默认新建GBK文件
想让sublime text支持显示或者保存中文,需要安装插件convertToUTF8,具体安装可以问度娘. 安装插件以后,想让保存的html文件支持中文,可以通过ctrl+shift+C来完成.但 ...