【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/single-number/
- Total Accepted: 183838
- Total Submissions: 348610
- Difficulty: Easy
题目描述
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
题目大意
数组中除了有一个数字只出现了一次之外,其余的数字都出现了偶数次。找出这个只出现了一次的叛徒。
解题方法
异或
标准的异或运算!
异或运算是可以交换顺序的运算,也就是说和元素的排列顺序无关,自己异或自己等于0,0异或别人等于别人。故,
we use bitwise XOR to solve this problem :
first , we have to know the bitwise XOR in java
1. 0 ^ N = N
2. N ^ N = 0
So… if N is the single numberN1 ^ N1 ^ N2 ^ N2 … Nx ^ Nx ^ N
= (N1^N1) ^ (N2^N2) … (Nx^Nx) ^ N
= 0 ^ 0 ^ …^ 0 ^ N
= N
即,只要把所有的数字异或一遍,如果出现两次的数字,进行异或之后自动消失,剩余的就是只出现一次的那个数字。
java代码如下。
public class Solution {
public int singleNumber(int[] nums) {
int returnNum=0;
for(int i=0; i<nums.length; i++){
returnNum ^=nums[i];
}
return returnNum;
}
}
AC:1ms
二刷 python。同样使用异或,对于Python2来说可以直接使用reduce函数,就所有数字进行运算的结果。
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return reduce(lambda x, y: x ^ y, nums)
字典
别只记得位运算,忘记了最简单的数字统计啊!可以使用Counter直接求只出现一次的数字即可。
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = collections.Counter(nums)
return count.most_common()[-1][0]
日期
2017 年 1 月 7 日
2018 年 3 月 14 日 – 霍金去世
2018 年 11 月 9 日 —— 睡眠可以
【LeetCode】136. Single Number 解题报告(Java & Python)的更多相关文章
- LeetCode 136 Single Number 解题报告
题目要求 Given a non-empty array of integers, every element appears twice except for one. Find that sing ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 136. Single Number(只出现一次的数字)
LeetCode 136. Single Number(只出现一次的数字)
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- LeetCode 136. Single Number C++ 结题报告
136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...
- [LeetCode] 136. Single Number 单独数
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
随机推荐
- 【机器学习与R语言】13- 如何提高模型的性能?
目录 1.调整模型参数来提高性能 1.1 创建简单的调整模型 2.2 定制调整参数 2.使用元学习来提高性能 2.1 集成学习(元学习)概述 2.2 bagging 2.3 boosting 2.4 ...
- 毕业设计之zabbix---自带模板监控mysql内容
自带模板是不能直接建立连接就可以用的 必须经历一下几步: 建立用户权限: [root@mysql.quan.bbs lib]$mysql -u root -p Enter password: Welc ...
- idea中如何找到重写
Ctrl+O 为了避免写错重写类和快速重写.
- MariaDB——简介
一.MariaDB跟MySQL在绝大多数方面是兼容的,对于开发者来说,几乎感觉不到任何不同.是MySQL的代替品. MariaDB虽然被视为MySQL数据库的替代品,但它在扩展功能.存储引擎以及一些新 ...
- kubectl logs查看日志时出现failed to create fsnotify watcher: too many open files
因为系统默认的 fs.inotify.max_user_instances=128 太小,在查看日志的pod所在节点重新设置此值: 临时设置 sudo sysctl fs.inotify.max_us ...
- 【leetcode】122.Best Time to Buy and Sell Stock II(股票问题)
You are given an integer array prices where prices[i] is the price of a given stock on the ith day. ...
- minSdkVersion、targetSdkVersion、targetApiLevel的区别
在AndroidMenifest.xml中,常常会有下面的语句: <uses-sdk android:minSdkVersion="4" android:targetSdk ...
- zabbix之邮件报警
创建媒介类型 如果用QQ邮箱的话,先设置一下授权码 为用户设置报警 创建一个用户 配置动作 测试
- JS - 字符串转换成数组,数组转换成字符串
1.字符串转换成数组: var arr = "1, 2, 3, 4, 5, 6"; arr.split(","); // ["1",&quo ...
- 2.ElasticSearch集群的搭建
1.创建elasticsearch-cluster文件夹,在内部复制三个elasticsearch服务 2.修改elasticsearch-cluster\node*\config\elasticse ...