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 ...
随机推荐
- 排序与检索【UVa10474】Where is the Marble?
Where is the Marble? DescriptionRaju and Meena love to play with Marbles. They have got a lot of ma ...
- python基础操作_集合_三元运算
#使用操作文件的时候,可以使用with函数#with open('E:\info.txt','a+') as fr#fr这个值可以是任意值# :#for line in fr:'''with open ...
- 基于C#的接口自动化测试(二)
Json多重数据处理方法II:jobj直接向后调用即可 ]["GoodsName"]; 用:分隔的字符串转字典: public static Dictionary<strin ...
- ASP.NET MVC开发学习过程中遇到的细节问题以及注意事项
1.datagrid中JS函数传值问题: columns: { field: 'TypeName', title: '分类名称', width: 120, sortable: true, format ...
- Bash中的特殊变量和位置参量
位置参量:向脚本或函数传递的参数,可以被set命令设置.重置和清空. 1.$$ 当前Shell的PID 2.$- 当前Shell的选项,如果是交互式shell,应该包含字符i,例如$ echo $-h ...
- VirtualBox的快照功能
VirtualBox是非常好用的一个虚拟机软件,无论是性能还是易用性不比商用的Vmware差.VirtualBox最初是Sun公司的产品,由于Sun被Oracle收购,现在也归Oracle了.除了虚拟 ...
- 一劳永逸的解决AFNetworking3.0网络请求问题(面向对象封装大法,block回调)
AFNetworking在iOS网络请求第三方库中占据着半壁江山,前段时间将AFNetworking进行了3.0版本的迁移,运用面向对象的设计将代码进行封装整合,这篇文章主要为还在寻找AFNetwor ...
- cordova.js的坑
最近在使用cordova开发app,一开始只引用了cordova.js,cordova通过function exec(){}来链接本地和H5,之前代码被下载到了本地,在苹果和android上都能实现功 ...
- 增强学习 | AlphaGo背后的秘密
"敢于尝试,才有突破" 2017年5月27日,当今世界排名第一的中国棋手柯洁与AlphaGo 2.0的三局对战落败.该事件标志着最新的人工智能技术在围棋竞技领域超越了人类智能,借此 ...
- Css3视频教程下载
本套教程主要讲解了大量的CSS3新功能,包括: 边框.圆角.背景.渐变.阴影.文本特效.2D/3D转换.过渡.动画.伪类元素的使用等,同时伴随了大量的实例制作,比如CSS3实现红心的制作,火焰字.多彩 ...