LeetCode编程练习

来源:互联网 发布:java视频教程数组 编辑:程序博客网 时间:2024/06/06 17:59

题目:

       Given an array containingn distinct numbers taken from0, 1, 2, ..., n, find the one that is missing from the array.

       For example,
       Given
nums =[0, 1, 3] return2.

       Note:
       Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

    给定一个包含n个不同数字的数组,从0,1,2,...,n,找到数组中缺失的那个。例如,给定nums = [0,1,3],返回2.


思路:

   首先想到的是对这个数组进行遍历,之前有在Hash Table中遇到Sigle Number,类型差不多,它使用到了异或^,判断数值是否相同,若不相同则赋值,但在这里,若不相同则返回这个数。

    但当我输入[0,1]时,输出结果应该为2,但我的输出结果为3,查看后发现,还需要跟遍历值进行对比。

原创粉丝点击