https://leetcode.com/problems/single-number/

Given an 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?

class Solution {
public:
int singleNumber(vector<int>& nums) {
if(nums.size() == ) return nums[]; vector<int> bitNums(, ); int res = ;
for(int i=;i<bitNums.size();++i) {
for(int j=;j<nums.size();++j) {
bitNums[i] += (nums[j] >> i) & ;
}
res |= (bitNums[i] % ) << i;
} return res;
}
};

https://leetcode.com/problems/single-number-ii/

Given an array of integers, every element appears three times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

class Solution {
public:
int singleNumber(vector<int>& nums) {
if(nums.size() == ) return nums[]; vector<int> bitNums(, ); int res = ;
for(int i=;i<;++i) {
for(int j=;j<nums.size();++j) {
bitNums[i] += (nums[j] >> i) & ;
cout << ((nums[j] >> i) & ) << " ";
}
cout<<endl;
} for(int i=;i<bitNums.size();++i) {
res += (bitNums[i] % ) << i;
}
return res;
}
};

leetcode@ [136/137] Single Number & Single Number II的更多相关文章

  1. [LeetCode#136, 137]Single Number, Single Number 2

    The question: Single Number Given an array of integers, every element appears twice except for one. ...

  2. leetcode 136 137 Single Number

    题目描述(面试常考题) 借助了异或的思想 class Solution { public: int singleNumber(vector<int>& nums) { ; ; i ...

  3. Leetcode 136 137 260 SingleNumber I II III

    Leetccode 136 SingleNumber I Given an array of integers, every element appears twice except for one. ...

  4. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  5. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  6. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  7. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

  8. LeetCode 136. Single Number(只出现一次的数字)

    LeetCode 136. Single Number(只出现一次的数字)

  9. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

随机推荐

  1. 手机开发类型jquery的框架Zepto(API)

    Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jquery有着类似的api. 如果你会用jquery,那么你也会用zepto. http://www.html-5.cn/M ...

  2. javascript closure

    http://www.jibbering.com/faq/notes/closures/ http://hi.baidu.com/bluedream_119/item/938dcd082b1e1880 ...

  3. 【POJ 3335】 Rotating Scoreboard (多边形的核- - 半平面交应用)

    Rotating Scoreboard Description This year, ACM/ICPC World finals will be held in a hall in form of a ...

  4. MSSQLServer基础02(SQL语句入门(脚本、命令))

    SQL 全名是结构化查询语言(Structured Query Language),是关系数据库管理系统的标准语言 SQL语句是和DBMS“交谈”专用的语句,不同DBMS都认SQL语法. SQL语句中 ...

  5. RabbitMQ安装和配置

    RabbitMQ: MQ:message queue.MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来 ...

  6. python 简单示例说明os.walk和os.path.walk的不同

    import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,fi ...

  7. TextView设置样式的3种方式

    1,直接在<TextView>中设置 <TextView android:id="@+id/tv_badge_view_count" android:layout ...

  8. 1067. Disk Tree(字符串)

    1067 破题啊  写完发现理解错题意了 子目录下会有跟之前重名的 把输入的字符串存下来 排下序 然后依次找跟上面有没有重的 #include <iostream> #include< ...

  9. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  10. poj3666

    一道不错的dp题 就是最小修改代价,使序列变为一个非下降序或非上升(由于数据较弱直接求非下降即可,当然非上升非下降本质是一样的) 观察可得到,修改后得到的数列中的元素最后一定都在原序列中: 由此我们可 ...