136 Single Number(找唯一数Medium)
题目意思:一个int数组,有一个数只出现一次,其他数均出现两次,找到这个唯一数
知识普及:~:非运算,单目运算符1为0,0为1;
&:与运算,都为1则为1,否则为0
|:或运算,全为0则为0,否则为1
^:异或运算,相同为0,不同为1
思路:将数组中元素进行异或运算,则只剩下0和唯一数,异或得到的是唯一数
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res=;
for(int i=;i<nums.size();++i){
res^=nums[i];
}
return res;
}
};
时间复杂度:O(n)
136 Single Number(找唯一数Medium)的更多相关文章
- 137 Single Number II(找唯一数Medium)
题目意思:一个int数组,有一个数只出现一次,其他数均出现三次,找到这个唯一数 思路: 1.将所有数用2进制表示,计算每一位的数字和 1*3*n1+0*3*n2+c 唯一数对应位的数字(0或者1 ...
- [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 ...
- [LeetCode] 260. Single Number III 单独数 III
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- LeetCode 136. Single Number C++ 结题报告
136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...
- 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] 137. Single Number II 单独数 II
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- LeetCode 136. Single Number(只出现一次的数字)
LeetCode 136. Single Number(只出现一次的数字)
- 136. Single Number - LeetCode
Question 136. Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int single ...
随机推荐
- Velocity常用方法
1.字符串替换 replace#if($!{name} != '')#set($tempName = $!{name})#set($tempName = $tempName.Replace('abc' ...
- 《傲慢与偏见》(Pride and Prejudice)
<傲慢与偏见>(Pride and Prejudice)改编自英国作家简·奥斯汀的同名小说,1940年上映.讲述了19世纪初期英国的一个普通的中产家庭中五姐妹的爱情与择偶故事.片中因为男主 ...
- Jinja2模版语言自定义filter的使用
Jinja2模版语言,自带有一些filter,能够在前端的模版中控制数据按照相应的方式显示.比如以下两种filter,分别能在前端控制数字的近似精度显示和根据字符串长度补齐: round(value, ...
- POJ 3978 Primes(求范围素数个数)
POJ 3978 Primes(求范围素数个数) id=3978">http://poj.org/problem? id=3978 题意: 给你一个区间范围A和B,要你求出[A,B]内 ...
- win7 32/64bit VS2010 OpenCV 2.4.9 环境配置
作者 : 卿笃军 因为近期人脸识别须要用到OpenCV,让我開始了OpenCV学习. OpenCV 2.4.9下载地址:http://sourceforge.net/projects/opencvli ...
- linux 下 apt命令集详解
apt命令用法 packagename指代为软件包的名称 apt-get update 在修改/etc/apt/sources.list或/etc/apt/preferences之後运行该命令.此外您 ...
- [Android] Service和IntentService中显示Toast的区别
1. 表象 Service中可以正常显示Toast,IntentService中不能正常显示Toast,在2.3系统上,不显示toast,在4.3系统上,toast显示,但是不会消失. 2. ...
- yii 验证码那点事儿
今天要使用yii验证码, 不过, 这个验证码是整站通用的, 也就是说, 有个表单的提交是使用ajax方式来提交, 整站, 不管在哪个地方, 都能点出来此窗口, 来提交信息 关于yii验证码, fram ...
- Python之路,Day4 - Python基础4 (new版)
Python之路,Day4 - Python基础4 (new版) 本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...
- C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...