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?

 1 /*************************************************************************
 2     > File Name: LeetCode268.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: Tue 10 May 2016 06:19:13 PM CST
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Missing Number
11
12     Given an array containing n distinct numbers taken from 0, 1, 2, ..., n,
13     find the one that is missing from the array.
14
15     For example,
16     Given nums = [0, 1, 3] return 2.
17
18     Note:
19     Your algorithm should run in linear runtime complexity.
20     Could you implement it using only constant extra space complexity?
21
22  ************************************************************************/
23
24 #include <stdio.h>
25
26 /* 通用方法 */
27 int missingNumber( int* nums, int numsSize )
28 {
29     int sum = ( 0 + numsSize) * (numsSize+1) / 2;
30     int ret = 0;
31     int i;
32     for( i=0; i<numsSize; i++ )
33     {
34         ret += nums[i];
35     }
36     ret = sum - ret;
37     return ret;
38 }
39
40 /* 如果数据是从0递增的话可以使用以下方法 */
41 /* 测试用例包含不是纯从0递增数组,所以此方法LeetCode不通过 */
42 int missingNumber2( int* nums, int numsSize )
43 {
44     int i;
45
46     for( i=0; i<numsSize; i++ )
47     {
48         if( i != nums[i] )
49         {
50             return i;
51         }
52     }
53     return numsSize;
54 }
55
56 int main()
57 {
58     int nums[] = { 0, 1, 3 };
59     int numsSize = 3;
60
61     int ret = missingNumber2( nums, numsSize );
62     printf("%d\n", ret);
63
64     return 0;
65 }
时间: 2024-10-20 11:05:43

LeetCode 268的相关文章

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 usi

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

Leetcode 268 Missing Number 位运算

题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组,这样就和singe number 一样. 1 class Solution { 2 public: 3 int missingNumber(std::vector<int>& nums) { 4 int ans = 0; 5 for (std::vector<int>::siz

[LeetCode]268 Missing Number

很简单,提供两种解法. (1)等差数列先求得总和,然后减去给定的队列,剩下的值就是缺失值. (2)先排个序,逐个比较,不等的直接return 第一种的代码: class Solution { public: int missingNumber(vector<int>& nums) { int n = nums.size(); int expect = (n+1) * n / 2; for(int i = 0; i < n; i ++) expect -= nums[i]; ret

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 relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Not

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 usi

[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. Example 1 Input: [3,0,1] Output: 2 Example 2 Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note:Your algorithm should run in linear runtime c

Leetcode 268.缺失数字 By Python

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2,3,5,7,0,1] 输出: 8 说明: 你的算法应具有线性时间复杂度.你能否仅使用额外常数空间来实现? 思路 因为给定的序列也是从0开始,所以可以进行排序,比较索引和索引对应的值,如果两个不等于说明就确实一个值了,还要注意一个情况是,没出现的数字是n 代码 class Solution(obje

LeetCode:Missing Number - 缺失的数字

1.题目名称 Missing Number (缺失的数字) 2.题目地址 https://leetcode.com/problems/missing-number 3.题目内容 英文:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n  find the one that is missing from the array. 中文:给出一个包含了n个不同数字的数组,从0开始一直到n,找出缺失的数字.如果数