LeetCode & Q268-Missing Number-Easy
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]return2.
这题真是一言难尽....刚开始不太理解,数字要从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的更多相关文章
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 33. leetcode 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 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 ...
- leetcode 268 Missing Number(异或运算的应用)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Leetcode 268 Missing Number 位运算
题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...
- [leetcode] 263. Ugly Number (easy)
只要存在一种因数分解后,其因子是2,3,5中的一种或多种,就算是ugly数字. 思路: 以2/3/5作为除数除后,最后结果等于1的就是ugly数字 Runtime: 4 ms, faster than ...
- 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 ...
随机推荐
- 一道java基础面试题
package test; class A { public A(){ System.out.println("A3"); } { S ...
- PHP Curl会话请求
/** * @param string $url 请求地址 * @param string $type 请求类型 post get * @param string $arr 如果是post 传递的数据 ...
- ThreadLocal用例之周期为一次请求的变量
public class RecordedLocal { private static ThreadLocal<Recorded> local = new ThreadLocal<R ...
- python 数据结构简介
栈(stack) 定义: 数据集合,只能在一端(首尾)进行删除和插入的列表. 特点: 后进先出(LIFO) 典型作用: 括号匹配:左括号进栈,右括号跟左括号对应则出栈,例如:(({{[]}}))匹配 ...
- 创建Maven项目时提示web.xml is missing and <failOnMissingWebXml> is set to true错误解决方案
1. 右键点击Deployment Descriptor 2. 选择Generate Deployment Descriptor Stub P.S.下面顺便提一个小技巧: 创建动态web时先右键项目, ...
- OpenStack Paste.ini详解(二)
接着OpenStack Paste.ini详解(一),接下来就分析request被paste.ini处理的流程 WSGI server接收到URL形式的request时,这些request首先会被Pa ...
- mysql简单操作
1,mysql 唤醒数据库,mysql -uroot -p11221 2,创建一个数据库: CREATE DATABASE mldn CHARACTER SET UTF8; 也可以写成小写的:crea ...
- SublimeText3插件安装及使用
之前写过一篇文章讲了安装PackageControl,这里就不做赘述了,需要的朋友移步到这篇文章:Sublime Text安装package control 安装插件方法:通过preferencs-- ...
- SignalR Self Host+MVC等多端消息推送服务(4)
由于工作太忙,一直没时间更新博客,之前有很多朋友一直问我什么时候将后续的代码发上来,一直没时间,今天就长话短说,不写文章了,直接上demo,里面将正式项目中用到的一些敏感信息修改了,要使用的话下载后自 ...
- 前端的UI设计与交互之设计原则篇
1.亲密性 a)纵向间距示例这三种规格分别为:8px(小号间距).16px(中号间距).24px(大号间距). b)在这三种规格不适用的情况下,可以通过加减『基础间距』的倍数,或者增加元素来拉开信息层 ...