[LeetCode] 136. Single Number 单独数
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
由于要求时间复杂度O(n),空间复杂度O(1),所以不能用排序法,也不能使用map。
解法1:用两倍所有非重复元素和减去原数组。
解法2:位操作Bit Operation,使用二进制数位操作中的异或,同为0,异为1。主要考察位操作。
异或运算{\displaystyle A\oplus B}的真值表如下: F表示false,T表示true
| A | B | ⊕ |
|---|---|---|
| F | F | F |
| F | T | T |
| T | F | T |
| T | T | F |
Java:
public class Solution {
public int singleNumber(int[] nums) {
int res = 0;
for (int num : nums) res ^= num;
return res;
}
}
Python:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return 2 * sum(set(nums)) - sum(nums)
Python:
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
for num in nums:
res ^= num return res
Python:
import operator
from functools import reduce class Solution:
"""
:type nums: List[int]
:rtype: int
"""
def singleNumber(self, A):
return reduce(operator.xor, A)
C++:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = 0;
for (auto num : nums) res ^= num;
return res;
}
};
类似题目:
[LeetCode] 137. Single Number II 单独数 II
[LeetCode] 260. Single Number III 单独数 III
All LeetCode Questions List 题目汇总
[LeetCode] 136. Single Number 单独数的更多相关文章
- 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 136. Single Number(只出现一次的数字)
LeetCode 136. Single Number(只出现一次的数字)
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- LeetCode 136. Single Number C++ 结题报告
136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...
- LeetCode 136 Single Number(仅仅出现一次的数字)
翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array ...
- LeetCode 136. Single Number (落单的数)
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode 136 Single Number 数组中除一个数外其他数都出现两次,找出只出现一次的数
Given an array of integers, every element appears twice except for one. Find that single one. class ...
- leetcode 136. Single Number ----- java
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- Java [Leetcode 136]Single Number
题目描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
随机推荐
- AS项目报错 Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
1 修改gradle的缓存目录 这个可以通过android studio的设置中找到gradle,配置另一个非中文目录来缓存. File -> Settings -> Build, Exe ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组 [Pro ...
- Linux学习21-设置定时任务crontab
前言 做自动化测试写的脚本需设置定时任务,在指定的时间去执行,这就需要用到定时任务.之前用jenkins可以在里面设置定时任务,很好用,其实不用jenkins,在linux上也可以用crontab做个 ...
- 最小圆覆盖(洛谷 P1742 增量法)
题意:给定N个点,求最小圆覆盖的圆心喝半径.保留10位小数点. N<1e5: 思路:因为精度要求较高,而且N比较大,所以三分套三分的复杂度耶比较高,而且容易出错. 然是写下增量法吧. 伪代码加深 ...
- [Codeforces 1242C]Sum Balance
Description 题库链接 给你 \(k\) 个盒子,第 \(i\) 个盒子中有 \(n_i\) 个数,第 \(j\) 个数为 \(x_{i,j}\).现在让你进行 \(k\) 次操作,第 \( ...
- linux 查看某个目录下文件的数量
今日思语:时间是个庸医,却自称能包治百病~ 在linux环境下,经常需要查看某个文件目录下的文件数有多少,除了进入当前目录下查看,还可以使用命令: ls -l | grep "^-" ...
- linux /lib64/libc.so.6: version `GLIBC_2.17′ not found
使用root权限安装Glances,需要用到glibc,安装失败后所有命令都不好用了,执行回报“/lib64/libc.so.6: version `GLIBC_2.17′ not found ”的错 ...
- 28-ESP8266 SDK开发基础入门篇--编写wifi模块TCP 客户端程序(官方API版,非RTOS版)
https://www.cnblogs.com/yangfengwu/p/11432795.html 注:这节实现的功能是WIFI模块作为TCP 客户端,连接咱的TCP服务器,然后实现透传 本来想着做 ...
- javascript之随机密码[必包含大写,小写,数字]
js取两个数字之间的随机数: parseInt(Math.random()*(上限-下限+1)+下限) 如:取1-10之间的随机数 parseInt(Math.random()*(10-1+1)+ ...
- javascript根据两点和底角,计算等腰三角形的顶点坐标
参考图: 代码如下: var x1 = 0; var y1 = 100; var x2 = -100; var y2 = 0; var angle = 30; var PI = Math.PI; // ...