需求:给出一个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. laravel中的多对多关系详解

    数据表之间是纵横交叉.相互关联的,laravel的一对一,一对多比较好理解,官网介绍滴很详细了,在此我就不赘述啦,重点我记下多对多的关系 一种常见的关联关系是多对多,即表A的某条记录通过中间表C与表B ...

  2. PHP文件操作,多行句子的读取,file()函数,file_get_contents()函数,file_put_contents()函数,is_file,统计网站pv (访问量),文件的复制 copy,文件重命名 rename,删除文件 unlink

    php中添加utf-8: header("Content-type:text/html;charset='UTF-8'"); 文件操作步骤: 1.在同一目录下建立一个file.tx ...

  3. 【WPF】用三角形网格构建三维图形

    虽然WPF只能支持部分三维模型,不过从应用功能开发的角度看,也已经够用了(非游戏开发).WPF 的三维图形,说得简单一点,也就两种而已. 1.把二维对象放到三维空间中,这个应该较为好办,像 Image ...

  4. bootstrap 架构知识点

    .col-md-pull-2 向右相对定位偏移量 .col-md-push-2 向左相对定位偏移量 .pull-left 左浮动 .pull-right 右浮动     改变大小写 通过这几个类可以改 ...

  5. Java NIO学习笔记一 Java NIO概述

    Java NIO概述 Java NIO(新的IO)是Java的替代IO API(来自Java 1.4),这意味着替代标准的 java IO和java Networking API.Java NIO提供 ...

  6. 浅谈java内存分配和回收策略

    一.导论 java技术体系中所提到的内存自动化管理归根结底就是内存的分配与回收两个问题,之前已经和大家谈过java回收的相关知识,今天来和大家聊聊java对象的在内存中的分配.通俗的讲,对象的内存分配 ...

  7. top的用法

    top命令可以用来方便地观察当前系统中运行的进程的信息,并可以在运行过程中执行改变进程的优先级.更改排序规则.导出状态信息等操作,非常方便. 1.主要选项 -d:后接秒数,状态更新的秒数,默认5秒-b ...

  8. 10分钟就能学会的.NET Core配置

    .NET Core为我们提供了一套用于配置的API,它为程序提供了运行时从文件.命令行参数.环境变量等读取配置的方法.配置都是键值对的形式,并且支持嵌套,.NET Core还内建了从配置反序列化为PO ...

  9. Gist - ES6 Iterator

    Introduction Iterator is one of the most common design modes in daily development. Let's explore the ...

  10. iOS 输入限制之 InputKit

    前言 最近接手了两个 O2O 的老项目,其中的 Bug 也不言而喻,单看项目中的布局就有 n 种不同的方式,有用纯代码的,有用 Masonry 的,有用 VFL 的,也有用 Xib 的,更有用代码约束 ...