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 ...
随机推荐
- Hibernate之QBC查询与本地SQL查询
1. QBC查询: QBC 查询就是通过使用Hibernate提供的QueryByCriteria API 来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口 ...
- 报表服务框架:WEB前端UI
1.Highcharts 2.ECharts 3.ichartjs 参考: http://v1.hcharts.cn/index.php http://echarts.baidu.com/ http: ...
- 解决Failed to execute goal org.apache.maven.plugins
1.Maven构建失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin: 2.3 . 2 :compile ...
- Spring Data JPA教程, 第八部分:Adding Functionality to a Repository (未翻译)
The previous part of my tutorial described how you can paginate query results with Spring Data JPA. ...
- mybatis杂记
mybatis学习官网: 1.如果项目中使用maven管理,又引用 了mybatis框架, 下面是mybatis官网给出的 mybatis在maven中央仓库的坐标原文 详情见连接:https://c ...
- Ubuntu12.04 使用中遇到的问题
这个随笔回记录使用Ubuntu遇到的一些问题 不定期进行整理和分类 1.Question: ubuntu 无法检测包或者源码包 Description:Ubuntu软件中心打开时报错 无法检 ...
- Unity3D之游戏暂停制作方法记录
在游戏开发中我们一般都需要涉及到一个功能:游戏暂停,但是这里指的暂停仅仅是核心模块的暂停,并不是整个游戏都暂停,比如一些UI和UI上的动画与特效是不能被暂停的,整个游戏都暂停了玩家该如何继续游戏呢. ...
- DataGridView 添加ComboBox
http://www.wapsolo.com/Personal/personal_view_75.aspx DataGridView 添加ComboBox 第一: 先在窗体设计时拖一个ComBoBox ...
- 《C语言编写 学生成绩管理系统》
/* (程序头部凝视開始) * 程序的版权和版本号声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名: 学生成绩管理 ...
- iOS开发笔记系列-基础7(C语言特性)
Objective-C是C语言的扩展,因此,也具备很多C语言的基本特性,这里只罗列部分. 块(Blocks) 块是对C语言的一种扩展,它并未作为标准ANSI C所定义的部分,而是Apple添加到语言中 ...