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 ...
随机推荐
- Redis之PHP操作
一.Redis连接与认证 //连接参数:ip.端口.连接超时时间,连接成功返回true,否则返回false $ret = $redis->connect('127.0.0.1', 6379, 3 ...
- Java 容器之Hashset 详解
Java 容器之Hashset 详解.http://blog.csdn.net/nvd11/article/details/27716511
- 初读 c# IL中间语言
对一段c#编写的代码,有一些疑问,想通过IL中间语言看看,编译后是怎么处理的.代码如下: static StringBuilder sb = new StringBuilder(); ; ; /// ...
- hidden symbol `pthread_atfork'
gcc交叉编译时发生这种错误 /.. .../voice_demo: hidden symbol `pthread_atfork' in /opt/gcc-linaro-aarch64-linux-g ...
- spring boot整合log4j2
POM配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
- canvas实现将文字变成颗粒
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- windows下安装Virtualenvwrapper
windows下安装Virtualenvwrapper 我们可以使用Virtualenvwrapper来方便地管理python虚拟环境,但是在windows上安装的时候.....直接 install ...
- Struts2 学习之小白开始
Struts2 基础知识学习总结 Struts2 概述:Struts2 是一个用来开发 MVC 应用程序的框架,他提供了 Web 应用程序开发过程中的一些常见问题的解决方案,比如对于用户输入信息合法性 ...
- 移动web开发之rem的使用
为什么要使用rem 移动端设备尺寸五花八门,单纯使用px这个单位无法轻易适配,rem就可以为我们解决这个问题! 如何使用rem 1rem默认等于16px,这是因为页面的默认字体大小就是16px.r 代 ...
- #Python3.6.2(32位) pip安装 和 pygame 环境配置
#首先确认电脑已经安装python ,可通过在命令行下 执行 python --version确认. 1. 到 https://pypi.python.org/pypi/setuptools/ 下载 ...