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 ...
随机推荐
- 模板C++ 03图论算法 2最短路之全源最短路(Floyd)
3.2最短路之全源最短路(Floyd) 这个算法用于求所有点对的最短距离.比调用n次SPFA的优点在于代码简单,时间复杂度为O(n^3).[无法计算含有负环的图] 依次扫描每一点(k),并以该点作为中 ...
- 10分钟弄懂javascript数组
建议阅读时间 : 10分钟 主要内容:javascript数组的基本概念.属性.方法 新建数组: var arr01 = ["a","b","c&qu ...
- javaScript 设计模式系列之二:适配器模式
介绍 适配器模式将一个类的接口转接成用户所期待的,有助于避免大规模改写现有客户代码. In software engineering, the adapter pattern is a softwar ...
- pb传输优化浅谈
在正式切入今天要谈的优化之前,先碎碎念一些自己过去这几年的经历.很久没有登录过博客园了,今天也是偶然兴起打开上来看一下,翻看了下自己的随笔,最后一篇原创文章发布时间是2015年的4月,今天是2017年 ...
- Ajax&jQuery教案总结
Ajax&jQuery教程总结 目录 第一章 Ajax入门 6 第1讲 传统表单提交存在的问题 6 课程内容 6 1. 问题的引入 6 2. 问题的解决 6 参考进度(0.5课时) 7 第2讲 ...
- 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:JsonBlog' did not find a matching property.
这个问题困扰很久了,逛了很多论坛,终于得以解决 我的控制台错误如下: 五月 , :: 下午 org.apache.catalina.startup.VersionLoggerListener log ...
- 【麦克风阵列增强】Delay and sum beamforming
作者:桂. 时间:2017-06-03 15:40:33 链接:http://www.cnblogs.com/xingshansi/p/6937576.html 前言 本文主要记录麦克风阵列的几个基 ...
- ecshop支付方式含线下自提
用户展示页面模板所在:如ecshop/theme/default/flow.dwt 后台管理展示页面模板所在:如admin/templates/payment_list.htm ecshop 支付接口 ...
- 2.配置Spring+SpringMvc+Mybatis(分库or读写分离)--Intellij IDAE 2016.3.5
建立好maven多模块项目后,开始使用ssm传统的框架: 1.打开总工程下的pom.xml文件:添加如下代码: <!--全局的所有版本号定义--> <properties> & ...
- V9发布内容时保留框架<iframe></iframe>
有些时候,发布文章内容的时候需要用到<iframe></iframe>框架站外内容最近在发布内容时就遇到这个问题,<iframe></iframe>给转 ...