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 ...
随机推荐
- Coursera 机器学习笔记(一)
主要是第一二周内容 机器学习概要 机器学习是什么? 生活在信息时代的我们,其实时时刻刻都离不开机器学习算法.比如日常使用的搜索引擎就涉及到很多学习算法. Arthur Samuel 给出第一个定义.他 ...
- windows embedded compact 2013 正版免费下载
不知道wince2013是不是真的免费了,不过可以试一下! 下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=39268 你仍然 ...
- javaWEB之Servlet
Servlet 1. 什么是Servlet * Servlet是JavaWeb三大组件之一(Servlet.Filter.Listener) * Servlet是用来处理客户端请求的动态资源 * ...
- Unity3D常用 API 之 Invoke 函数调用
1.金钱副本细节完善 1.1.宝箱自动掉落 给宝箱预制体添加刚体组件即可. 1.2.实现按键宝箱批量掉落 ①将实例化生成宝箱的代码单独封装成一个函数: ②使用 for 循环,批量生成宝箱. 按一次z键 ...
- MySql三大范式与数据库设计和表创建常用语句
[数据库设计的三大范式] 1.第一范式(1NF First Normal Fromate):数据表中的每一列(字段),必须是不可拆分的最小单元.也就是确保每一列的原子性. 例如: userInfo: ...
- 在C#中初遇Socket - 2
后期项目实战:多人在线聊天室 源码位置:https://git.oschina.net/z13qu/BlogProjects 前言 第一篇主要对Socket有个基本认识,实现初始化,发送.接受消息:本 ...
- linux下安装telnet
1:yum install telnet-server 2:编辑设置 /etc/xinetd.d/telnet ,将disable= yes设置成disable= no 3:service xine ...
- linux下vim 查找命令
在命令模式下输入/word 这个是查找文件中“word”这个单词,是从文件上面到下面查找?word 这个是查找文件中“word”这个单词,是从文件下上面到面查找
- 关于cookie与session的理解
服务器端并不能捕获客户端的浏览器关闭事件,因此你关闭浏览器以后,服务器端那个Session还是存在的,要超时以后才被回收,启动一个新的浏览器会启动一个新的session,所以他不认你了session永 ...
- java怎么发http请求
package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...