Array Math Bit Manipulation

Description:

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,

Given nums = [0, 1, 3] return 2.

这题真是一言难尽....刚开始不太理解,数字要从0开始往后记,我本来写的Binary Search有问题...参考了Discuss里重写了一遍

看到Discuss里很多用XOR写的,以前没有涉及过这个领域,感觉好神奇...

my Solution:

public class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
int left = 0, right = nums.length, mid= (left + right)/2;
while(left < right){
mid = (left + right) / 2;
if(nums[mid] > mid) right = mid;
else left = mid + 1;
}
return left;
}
}

XOR Solution:

// a^b^b = a, nums[index] = index
public int missingNumber(int[] nums) { int xor = 0, i = 0;
for (i = 0; i < nums.length; i++) {
xor = xor ^ i ^ nums[i];
} return xor ^ i;
}

看到最快的是用数学稍微转换下思路的方法,数学好的人真厉害...其实也不是特别厉害的数学....可是我怎么就没想起来呢...

public class Solution {
public int missingNumber(int[] nums) { int n = nums.length;
int sum = (n*(n+1)) /2; int numSum =0;
for(int i =0;i<nums.length;i++){
numSum += nums[i];
} int missingNumber = sum - numSum;
return missingNumber;
}
}

LeetCode & Q268-Missing Number-Easy的更多相关文章

  1. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  2. LeetCode 268. Missing Number (缺失的数字)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  3. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  4. [LeetCode] 268. Missing Number 缺失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  5. 33. leetcode 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  6. LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告

    1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...

  7. leetcode 268 Missing Number(异或运算的应用)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  8. Leetcode 268 Missing Number 位运算

    题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...

  9. [leetcode] 263. Ugly Number (easy)

    只要存在一种因数分解后,其因子是2,3,5中的一种或多种,就算是ugly数字. 思路: 以2/3/5作为除数除后,最后结果等于1的就是ugly数字 Runtime: 4 ms, faster than ...

  10. LeetCode 268. Missing Number缺失数字 (C++/Java)

    题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...

随机推荐

  1. redis缓存的应用详解

    在现在的很多项目,基本上都需要引入缓存机制,那么缓存到底是什么呢? 缓存  也就是数据交互的缓冲区  Cache 在java-web项目中实现缓存,也就是需要首先把数据库需要用到的数据备份一份作为副本 ...

  2. 关于在Editplus中设置内容提示比如syso的快捷输出的方法

    在Editplus中默认的内容提示是很少的,比如我们最常用的syso快捷输出就没有,那么怎么来设置呢? 首先打开工具-首选项: 然后打开文件类型及语法-在文件类型中打开Java,如图: 然后打开 我们 ...

  3. win7开通共享步骤

    win7开通共享步骤 2017-10-09    11:12:09 个人原创博客,允许转载,转载请注明作者及出处,否则追究法律责任 1,开通来宾账户 2,为来宾账户创建一个空密码 右键我的电脑,管理, ...

  4. 分享一个命令行计算器-bc

    分享一个命令行计算器-bc 假如你在一个图形桌面环境中需要一个计算器时,你可能只需要一路进行点击便可以找到一个计算器.例如,Fedora 工作站中就已经包含了一个名为 Calculator 的工具.它 ...

  5. 前端touch事件方向的判断

    移动端touch事件判断滑屏手势的方向 方法一 当开始一个touchstart事件的时候,获取此刻手指的横坐标startX和纵坐标startY: 当触发touchmove事件时,在获取此时手指的横坐标 ...

  6. TPYBoard v102 DIY照相机(视频和制作流程)

    前段时间的帖子,利用TPYBoard v102做的DIY照相机,周末实物终于做出来了,加了两个按键模块和一个5110,做的有点糙啊----望大家勿怪,哈哈哈.拍出来图片还算清晰,串口摄像头模块用的30 ...

  7. 搭建hadoop伪分布式环境

    伪分布式就是只有一台机器,既是namenode又是datanode.一台阿里云服务器(centos)即可完成. Java环境 首先需要安装Java环境,下载jdk的安装包,解压到/usr/java/, ...

  8. 由浅入深理解----java反射技术

    java反射机制详解 java反射机制是在运行状态下,对任意一个类可以获取该类的属性和方法,对任意一个对象可以调用其属性和方法.这种动态的获取信息和调用对象的方法的功能称为java的反射机制 clas ...

  9. pip遇见的format问题

    这是pip升级以后的问题. DEPRECATION: The default format will switch to columns in the future. You can use –for ...

  10. linux小白成长之路3————更新yum源

    [内容指引] 进入目录:cd 查看目录下的内容:ls 重命名备份:mv 从网络下载:wget yum更新:yum update 第一次运行yum安装软件前,建议更新yum. 1.进入yum源目录 命令 ...