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. static 内部类

    一般情况下是不可以用static修饰类的.如果一定要用static修饰类的话,通常static修饰的是匿名内部类. 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static ...

  2. Kerbose

    http://blog.csdn.net/wulantian/article/details/42418231

  3. DJANGO中filter_horizontal和raw_id_fields的作用

    在作多项选择的操作方便性,及单项选择太多时,会有好一点的体验... class UserAuthorAdmin(admin.ModelAdmin): # raw_id_fields = ('group ...

  4. DVB系统中PCR的生成和PCR校正

    http://blog.csdn.net/chenliangming/article/details/3616720 引自<广播电视信息>2008年1月 从数字电视前端系统功能上来讲,传统 ...

  5. ERDAS IMAGINE 9.2安装破解方法

    Install the application. Copy the license.dat and ERDAS.exe to C:\Program Files\Leica Geosystems\Sha ...

  6. imports,using,和include之间的区别

    Imports, Using基本一样,有两个作用 1.将后面命名空间中所有的名字导入到当前命名空间 2.为后面的名字取一个当前命名空间可以访问的别名. 比如StreamWriter这个类在System ...

  7. [译]好程序员的五声“呐喊”

    通常编程情况下,会导致软件项目变坏的一些列反应 原文:The five shouts of good programmers 在任何一天,在这个世界上都有软件项目正在失败,这很常见.常见到当软件产品按 ...

  8. 关于TableView中出现deallocated问题

    Message sent to deallocated instance 关于的ios 开发中 deallocated问题,相信大家遇到了不少了: 关于怎么查找解决这个问题,特别是当问题在tableV ...

  9. poj 2488 A Knight's Journey( dfs )

    题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...

  10. struts2 package元素配置

    package 元素的所有属性及对应功能: Attribute Required Description name yes key to for other packages to reference ...