Missing number - 寻找缺失的那个数字
需求:给出一个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 - 寻找缺失的那个数字的更多相关文章
- lintcode 中等题:find the missing number 寻找缺失的数
题目 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 样例 N = 4 且序列为 [0, 1, 3] 时,缺失的数为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 ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 268. Missing Number序列中遗失的数字
[抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- [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 ...
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- Missing number
Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...
- Missing Number, First Missing Positive
268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...
- LeetCode算法题-Missing Number(Java实现-四种解法)
这是悦乐书的第200次更新,第209篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第65题(顺位题号是268).给定一个包含n个不同数字的数组,取自0,1,2,...,n ...
随机推荐
- Java文件流之练习
1 )将"今年是反法西斯胜利70周年,举国欢庆,所以要放假啦" 字符串 使用文件字符输出流 写入到oldhappy.txt文件中,复写10000行, 要求换行 在文件的开头写入当前 ...
- MyBB 18 SQL Injection Vulnerability
<?php error_reporting(0); ?> <form method="post" action=""> Input a ...
- MySQL5.7绿色版(免装版)的初始化和修改密码
1.下载MySQL5.7.18绿色版 1.1下载链接 以下是MySQL5.7.18绿色版的链接(来源oracle官网),打开链接直接下载 https://dev.mysql.com/gt/Downlo ...
- centos文件权限详解
假设回显信息为 ①-②rws③r-x④r-x ⑤1 ⑥root ⑦root ⑧430540 ⑨Dec 20 18:27 ⑩/usr/sbin/passwd ,现在逐一分析其内容. ①. 首字符-,表 ...
- cesium自定义气泡窗口infoWindow后续优化篇
http://www.cnblogs.com/giserhome/p/6248858.html该篇文章实现的自定义气泡窗口是基于修改cesium源代码基础上,这种做法只是援兵之计,凑合应付的,投机取巧 ...
- Redis中的基本数据结构
Redis基础数据结构 基础数据结构 sds简单动态字符串 数据结构 typedef struct sdstr{ int len // 字符串分配的字节 int free // 未使用的字节数 cha ...
- Python入门(2)
一. 基础语法 1.Print print 是 python 里很基本很常见的一个操作,它的操作对象是一个字符串. 直接在 print 后面加一段文字来输出的话,需要给文字加上双引号或者单引号. ...
- excel转html 实现在线预览
首先说一下,本人发布的代码都是经过本人亲测,并且用在实际项目中.如果觉得可以,希望大家点个赞,谢谢大家. 有什么问题,大家评论出来,一起交流.好了,不废话了,下面来说一说这个东西怎么做. 网上也有许多 ...
- 玩转spring boot——websocket
前言 QQ这类即时通讯工具多数是以桌面应用的方式存在.在没有websocket出现之前,如果开发一个网页版的即时通讯应用,则需要定时刷新页面或定时调用ajax请求,这无疑会加大服务器的负载和增加了客户 ...
- Linux 开机引导流程
Linux 开机启动流程 BIOS(Basic Input Output System)是 PC 机启动时加载的第一个软件.其实,它是一组固化到计算机主板上一个芯片上的程序,它保存着计算机最重要的输入 ...