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通过代理访问互联网平台提供的WebService接口的一种方法
背景 通常有两点原因需要通过代理访问互联网平台的提供的WebService接口: 1. 在公司企业内网访问外部互联网平台发布的接口,公司要求通过代理访问外网. 2. 频繁访问平台接口,IP被平台封了, ...
- Android Studio 封装的类的继承
有个封装好的Firebase.java文件,放到项目中直接使用就可以,这个需要继承一个AbstractFirebase类,在广告代码中,可以等到加广告的时候来加这个文件. 这个地方的继承,因为是ads ...
- intellij idea快捷键字典
最近在重装系统,在安装python IDE时候依然安装了sublime Text3和intellij Idea(冏,别问为什么没安装pycharm,0-0 逃).首先是已然将之前一直使用的sublim ...
- WordPress怎么给分类目录排序
WordPress默认的分类目录是按照字母的顺序排序的, 我们可以安装一款插件Category Order就可以自定义分类目录的顺序了,这款插件很小,安装后即可在后台的左侧菜单中看见Category ...
- Spring OAuth2 GitHub 自定义登录信息
# 原因 最近在做一款管理金钱的网站进行自娱自乐,发现没有安全控制岂不是大家都知道我的工资了(一脸黑线)? 最近公司也在搞 Spring OAuth2,当时我没有时间(其实那时候不想搞)就没做,现在回 ...
- nodejs中的require,exports使用说明
模块是一门语言编写大项目的基石,因此,了解如何组织.编写.编译.加载模块很重要.这里主要谈谈Node中的模块加载. 1.Node中的模块,主要使用require来加载模块,文件 require(&qu ...
- git 使用方式
一.常用操作命令 1.初始化操作 git config --global user.name '<name>' # 设置提交者名称 git config --global user.ema ...
- Django基础(二):环境配置
前戏 WEB框架简介 具体介绍Django之前,必须先介绍WEB框架等概念. web框架: 别人已经设定好的一个web网站模板,你学习它的规则,然后“填空”或“修改”成你自己需要的样子. 一般web框 ...
- 关于IM的一些思考与实践
上一篇简单的实现了一个聊天网页,但这个太简单,消息全广播,没有用户认证和已读未读处理,主要的意义是走通了websocket-sharp做服务端的可能性.那么一个完整的IM还需要实现哪些部分? 一.发消 ...
- 关于MD5+salt盐加密
MD5+salt 最近浏览浏览一些帖子时,发现曾经引以为傲的md5加密算法,虽然是无法解密的算法,但是现在可以通过FELHELP(谷歌浏览器插件)或者一些字典可以套出来,.但是当md5+salt值时, ...