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 ...
随机推荐
- 命令行更新node和npm
Windows系统下: 查看版本的命令和Ubuntu下一样. 不同的是Windows下不能使用"n"包管理器来对NodeJS进行管理,在这里我们使用一种叫"gnvm&qu ...
- Redis进阶实践之十八 使用管道模式加速Redis查询
一.引言 学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中批量的增加数据,肯定是大批量的,为了这主题,我从新找起了解决方案.目前 ...
- Myeclipse配置tomcat,以及简单的Myeclipse的配置
第一次总结技术相关的东西,那就坚持下去每天写一点.[有错欢迎指正,本人小白,轻虐] 首先,(以我的为例子吧)需要准备原材料,当然了也需要根据自己的机器底层架构选择了---Myeclipse(2015) ...
- Windows下GO的开发环境配置
本文主要内容如下几点: 下载安装GO 配置多个工作区,第一个默认放第三方包,其他的放项目代码 包管理器godep的安装使用 安装过程中的一些坑(墙) vscode中使用go 1. 下载并安装go 官网 ...
- CentOS 远程桌面相关服务安装笔记
# CentOS 7安装图形界面 sudo yum groupinstall "GNOME Desktop" "Graphical Administration Tool ...
- 【Unity与23种设计模式】解释器模式(Interpreter)
GoF中定义: "定义一个程序设计语言所需要的语句,并提供解释来解析(执行)该语言." 传统上,执行程序代码通常通过两种方式 第一种:编译程序 第二种:解释器 常见的使用解释器的程 ...
- VLOOKUP函数常用套路大全
今天和大家来说说VLOOKUP的那些事儿,深入了解一下VLOOKUP函数的各种用法,看看这位大众情人还藏着多少不为人知的秘密.函数的语法为:VLOOKUP(要找谁,在哪儿找,返回第几列的内容,精确找还 ...
- 使用.NET Core快速开发一个较正规的命令行应用程序
程序员的世界,命令行工具一直是"体验非常友好"的工具,也能自动化完成很多事情,同时还能结合shell来进行某项任务的批处理(脚本).在.NET Core中,命令行应用程序是基础,但 ...
- angular的$scope的使用
1. 可以在scope中直接使用 // 监听日期变化 $scope.$watch('vaFilter.startEffectiveDate', function(newDate, oldDate, s ...
- 第六届蓝桥杯B组java最后一题
10.压缩变换(程序设计) 小明最近在研究压缩算法. 他知道,压缩的时候如果能够使得数值很小,就能通过熵编码得到较高的压缩比. 然而,要使数值很小是一个挑战. 最近,小明需要压缩一些正整数的序列,这些 ...