需求:给出一个int型数组,包含不重复的数字0, 1, 2, ..., n;找出缺失的数字;

如果输入是[0, 1, 2] 返回 3

输入数组 nums = [0, 1, 2, 4] ;应该返回 3

输入nums = [2, 0] ;应该返回 1

输入nums = [1, 0];应该返回 2

方法1:先排序,再线性寻找

元素不一定按顺序排列;

先对数组进行排序,再按顺序寻找

import java.util.Arrays;

public class Solution {
    public int missingNumber(int[] nums) {
        Arrays.sort(nums);  // 先排序
        int index;
        for (index = 0; index < nums.length; index ++) {
            if (index!= nums[index]) {
                return index;
            }
        }
        return index;
    }
}

这个方法比较容易想到,但缺点是速度太慢

方法2:异或法

输入的数组中没有重复的元素,可以利用异或的特点进行处理

异或:不同为1,相同为0;

任何数与0异或都不变,例如:a^0 = a ;

多个数之间的异或满足互换性,a^b^c = a^c^b = a^(b^c)

1.假设输入的数组是[0, 1, 3],应该返回2

可以指定一个数组[0, 1, 2, 3],这个数组包含缺失的那个数字x

这个指定的数组其实是满足预设条件的数组

x并不存在,只是占个位置;把它们的元素按位异或,即

0, 1, x, 3

0, 1, 2, 3

0^1^2^3^0^1^3 = 0^0^1^1^3^3^2 = 0^2 = 2   得到结果“2”

2.假设输入数组是[2, 0],用数组[0, 1, 2]做如下运算:

0^0^1^2^2 = 1  得到结果 “1”

3.假设输入数组是[0, 1, 2, 3],指定一个数组[0, 1, 2, 3]用上面的方法计算

0^0^1^1^2^2^3^3 = 0  结果错误;实际应该返回4

我们用来计算的数组,也可以看做是输入数组的下标,再加1;这里应该用[0, 1, 2, 3, 4]来计算

0, 1, 2, 3, x

0, 1, 2, 3, 4          结果返回4

Java代码

public int missingNumber(int[] nums) {
    if(nums == null || nums.length == 0) {
        return 0;
    }
    int result = 0;
    for(int i = 0; i < nums.length; i++) {
        result ^= nums[i];
        result ^= i;
    }
    return result ^ nums.length;
}

代码中实现了前面说明的异或运算

Missing number - 寻找缺失的那个数字的更多相关文章

  1. lintcode 中等题:find the missing number 寻找缺失的数

    题目 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 样例 N = 4 且序列为 [0, 1, 3] 时,缺失的数为2. 注意 可以改变序 ...

  2. Bestcoder BestCoder Round #28 A Missing number(查找缺失的合法数字)

    Problem Description There is a permutation without two numbers in it, and now you know what numbers ...

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

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

  4. 268. Missing Number序列中遗失的数字

    [抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  5. [LintCode] Find the Missing Number 寻找丢失的数字

    Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...

  6. [LeetCode] Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  7. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  8. Missing Number, First Missing Positive

    268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...

  9. LeetCode算法题-Missing Number(Java实现-四种解法)

    这是悦乐书的第200次更新,第209篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第65题(顺位题号是268).给定一个包含n个不同数字的数组,取自0,1,2,...,n ...

随机推荐

  1. 资深小白带你走进OS Memory

    图片来源:http://www.tomshardware.com/ 序言: Memory(内存)是一台计算机组成的重要部分,也是最基础的一部分.其它基础组件有主板.CPU.磁盘.显卡(可独立可集成)等 ...

  2. 实验:Oracle数据泵导出导入之序列问题

    今天同事提出了一个问题: 使用数据泵expdp导出1个schema,有个表主键是触发器自增的id,导入测试库测试时,发现表里的数据比自增序列的值要大.导致插入数据报错. 最终结论是: 由于数据库先进行 ...

  3. php注册登录源代码

    php注册登录源代码 链接数据库<?php$conn=mysql_connect('localhost','root','');mysql_select_db('ht',$conn);mysql ...

  4. [附录]Discuz X2.5 模板目录结构注释说明

    /template/default/common  公共模板目录全局加载 block_forumtree.htm  DIY论坛树形列表模块 block_thread.htm  DIY帖子模块调用文件 ...

  5. DNS全局负载均衡(GSLB)基本原理

    原理 DNS全局负载均衡通过智能DNS解析来实现,通常在不同的地区设立多个数据中心,每个数据中心又使用多个运营商的线路.目前很多DNS服务商都提供了智能DNS服务,智能DNS通常是利用各运营商分省IP ...

  6. require 增量更新与版本管理

    使用require.js 加载JS文件时,当JS文件有更新,可以通过更改全局版本号( urlArgs : 'v=1'),告诉浏览器加载新的文件. 但该方法虽然使用方便,但美中不足的是有些不需要更新的文 ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)--S ...

  8. 使用JPA和Hibernate进行批量处理的最佳方式

    Tips 原文作者:Vlad Mihalcea 原文地址:The best way to do batch processing with JPA and Hibernate 在本文中,你将了解什么是 ...

  9. [转] 传说中的WCF

    这个解决方案中包含两个项目,一个叫Server,另一个叫Client,天生一对. 1.启动VS 2010,推荐用2010以上版本(2012 RC版也行),因为越高版本越好用,最好Express的,不要 ...

  10. python 标准库 -- shutil

    shutil shutil.move(src,dst) shutil.move('/tmp/20170223/new','/tmp/20170223/test') # 移动文件, 重命名等 shuti ...