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. Kafka 之 async producer (1)

    问题 很多条消息是怎么打包在一起的? 如果消息是发给很多不同的topic的, async producer如何在按batch发送的同时区分topic的 它是如何用key来做partition的? 是如 ...

  2. linux MySQL安装配置

    执行下面的命令初始化授权表: ./scripts/mysql_install_db --user=mysql

  3. ANDROID_MARS学习笔记_S01_002View、监听器初步

    一.View.监听器介绍 二.在Activity中获取view和设置属性,设置button的监听器 1.activity_main.xml <LinearLayout xmlns:android ...

  4. POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

    C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...

  5. JS中访问对象的属性

    方式一: 对象名.属性名;   方式二: 对象名["属性名"];   ★注意:方式二中,属性名以字符串的形式出现在方括号中,这意味着通过方式二访问属性的话,可以实现“动态访问对象的 ...

  6. 让JAVA代码跑得更快

    本文简单介绍一下在写代码过程中用到的一些让JAVA代码更高效的技巧. 1.   将一些系统资源放在池中(如数据库连接, 线程等) 在standalone的应用中, 数据库连接池可以使用一些开源的连接池 ...

  7. Ubuntu12.04下arm交叉编译环境的建立

    http://blog.csdn.net/heyangya2009/article/details/5424376 备注:ubuntu12.04+Android+Real6410 在主机上用来编译其他 ...

  8. linux 入门教程

    linux入门教程 搜索 Linux入门教程 前言 第一章 关于Linux的历史 第二章 图形界面还是命令窗口 第三章 Linux操作系统的安装 第四章 初步进入linux世界 第五章 Linux系统 ...

  9. bzoj2757

    非常神的数位dp,我调了几乎一天首先和bzoj3131类似,乘积是可以预处理出来的,注意这里乘积有一个表示的技巧因为这里质因数只有2,3,5,7,所以我们可以表示成2^a*3^b*5^c*7^d,也就 ...

  10. bzoj1079: [SCOI2008]着色方案

    dp.以上次染色时用的颜色的数量和每种数量所含有的颜色作状态. #include<cstdio> #include<algorithm> #include<cstring ...