Single Number

Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium

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) {
int res = ;
int nums_size = nums.size();
for(int i=;i<nums_size;i++){
res ^= nums[i];
}
return res;
}
};
 

Single Number II

Total Accepted: 69333 Total Submissions: 191725 Difficulty: Medium

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?

统计各个二进制位中1的个数,该方法适用于这种类型的题目:数组中的所有数都出现了K次,只有一个数只出现了一次

const int BITS = sizeof(int) * ;

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

Single Number III

Total Accepted: 18186 Total Submissions: 44516 Difficulty: Medium

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity

所有的结果异或之后剩下一个非零值,这个值就是只出现一次的两个数的异或结果,找到这个数中第一个bit=1的数位,用1左移这么多个数位做为数组的分割器。

const int BITS = sizeof(int)*;
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int nums_size = nums.size();
int n = ;
for(int i=;i<nums_size;i++){
n ^= nums[i];
}
int seprator = ;
for(int i=;i<BITS;i++){
if((n>>i) & ){
seprator = <<i;
break;
}
}
vector<int> res;
int res1=,res2=;
for(int i=;i<nums_size;i++){
if( (seprator & nums[i])){
res1 ^= nums[i];
}else{
res2 ^= nums[i];
}
}
res.push_back(res1);
res.push_back(res2);
return res;
}
};

Single Number,Single Number II的更多相关文章

  1. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  2. 4.Single Number && Single Number (II)

    Single Number: 1. Given an array of integers, every element appears twice except for one. Find that ...

  3. Single Number i and ii

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  4. [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示

    有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...

  5. leetcode@ [136/137] Single Number & Single Number II

    https://leetcode.com/problems/single-number/ Given an array of integers, every element appears twice ...

  6. [Leetcode]Single Number && Single Number II

    Given an array of integers, every element appears twice except for one. Find that single one. 非常简单的一 ...

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

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

  8. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

  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. eclipse中配置免安装tomcat7

    参看如下链接:http://hi.baidu.com/gpy11/item/744c13e14614c9b52e140b25

  2. MVC再次学习1

    1.0 匿名函数和匿名类: internal delegate int AddDel(int a, int b); //泛型委托 delegate T Demo<T>(int a,int ...

  3. js实现页面图片加载进度条

    //html <div id="loading" class="loading"> <div class="load"&g ...

  4. angular 中 directive中的多个指令

    <div ng-controller="ctrl1"> <superman weight length speed>superman</superma ...

  5. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  6. information_schema.routines 学习

    information_schema.routines 用户查看mysql中的routine信息 1.information_schema.routines 表中的常用列: 1.

  7. android sdk 如何重新生成debug.keystore

    1)首先你要确定你安装的JDK位置,Windows->Preferences->Java->Installed JREs,你可以看到是Jre的location,再在dos cmd模式 ...

  8. logstash 处理nginx 错误日志

    2016/08/30 14:52:02 [error] 11325#0: *346 open() "/var/www/zjzc-web-frontEnd/%27%22%2f%3E%3C%2f ...

  9. 深入浅出Node.js (2) - 模块机制

    2.1 CommonJS规范 2.1.1 CommonJS的出发点 2.1.2 CommonJS的模块规范 2.2 Node的模块实现 2.2.1 优先从缓存加载 2.2.2 路径分析和文件定位 2. ...

  10. 一个C#多线程的工作队列

    多线程添加元素到队列中,队列根据绑定 的事件进行自动处理,可以设置WorkSequential属性来实现对队列处理的单线程(严格顺序处理)或者多线程处理(循序出队,但是 多线程处理,不保证对队列元素的 ...