136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one. (Easy)

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

分析:

第一问属于技巧题,做过就会,没做过很难想。考虑异或操作,相同数异或之后为0, 0与一个数异或还是这个数本身,且异或操作满足交换律和结合律。

所以这个题将所有的数异或起来,就能得到那个落单的数。

代码:

 class Solution {
public:
int singleNumber(vector<int>& nums) {
int result = ;
for (int i = ; i < nums.size(); ++i) {
result ^= nums[i];
}
return result;
}
};

137. Single Number II

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

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

分析:

除了“单身狗”以外其他元素均出现了三次,考虑上一题中的异或的思想(不进位加法,两个1之后就变成0),推广到三个数,就是有一位1如果出现三遍,就应该抵消为0;

所以可以考虑建立一个32个元素数组表示int的各个位置,然后把每个数的每一位对应加进去,mod 3后的结果恢复成一个数即为结果。

代码:

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

260. Single Number III

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. (Medium)

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?

分析:

有两个单身狗,其他都是出现两次。考虑怎么把这个问题转化为single number1的问题。

利用1中的思路,先把所有的数异或,最后可以得到一个数。可以知道这个数(xorResult)是那两个单身狗异或的结果,但是我们无法从这个数恢复两个数本身。

但是我们可以xorResult中第一个1出现的位置,这个位置是1说明之前两个数在这一位不同(一个0,一个1);

于是我们可以把原数组中的元素这一位是0还是1分为两个数组,这两个数组便都是single number1的问题,最后把两个结果添加到vector返回。

代码:

 class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int xorResult = ;
for (int i = ; i < nums.size(); ++i) {
xorResult ^= nums[i];
}
int lastBitofOne = xorResult - (xorResult & (xorResult - ) );
int result1 = , result2 = ;
for (int i = ; i < nums.size(); ++i) {
if ( (nums[i] & lastBitofOne) == ) {
result1 ^= nums[i];
}
else {
result2 ^= nums[i];
}
}
vector<int> result{result1, result2};
return result;
}
};

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III的更多相关文章

  1. 执行tsung时报"Maximum number of concurrent users in a single VM reached

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ovcer.blog.51cto.com/1145188/1581326 [roo ...

  2. 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II

    好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...

  3. LC 375. Guess Number Higher or Lower II

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  4. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  5. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  6. 'Invalid update: invalid number of rows in section xx. The number of rows contained in an existing section after the update (xxx)...

    'Invalid update: invalid number of rows in section 5.  The number of rows contained in an existing s ...

  7. 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)

    We define the smallest positive real number as the number which is explicitly greater than zero and ...

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

    题目描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: ...

  9. [LeetCode] Guess Number Higher or Lower II 猜数字大小之二

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

随机推荐

  1. 如何解决mysql服务器load高

    .登录主机 # ssh hostname .确定是否是mysql导致 # top .查看是哪些sql正在慢查询 # mysql -h hostname -P port -u username # sh ...

  2. 【codeforces 499C】Crazy Town

    [题目链接]:http://codeforces.com/problemset/problem/499/C [题意] 一个平面,被n条直线分成若干个块; 你在其中的某一块,然后你想要要到的终点在另外一 ...

  3. pip报错ImportError: cannot import name main

    编辑pip sudo gedit /usr/bin/pip 修改pip文件: 源文件 from pip import main if __name__ == '__main__': sys.exit( ...

  4. css 实现单行以及多行文本溢出显示省略号

    如果实现单行文本的溢出显示省略号大家都知道用text-overflow:ellipsis属性来,当然还需要加上宽度width属性. 实现方法: ``` overflow: hidden; text-o ...

  5. 矩阵快速幂2 3*n铺方格

    #include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...

  6. python scikit-learn计算tf-idf词语权重

       python的scikit-learn包下有计算tf-idf的api,研究了下做个笔记 1 安装scikit-learn包 sudo pip install scikit-learn 2 中文分 ...

  7. LA3882 And Then There Was One

    And Then There Was One https://vjudge.net/problem/UVALive-3882 题目大意:n个数编号1..n排成一圈,第一次删除m,后来每k个删除一个(下 ...

  8. img标签中的onerror事件

    img标签中有一个onerror事件.是当我引用的src属性获取不到图片或者网络错误导致无法正常显示我src属性的图片时,显示的提示错误图片或者是可以代替的万能图片. 用法: <img src= ...

  9. spring cloud深入学习(二)-----服务注册中心spring cloud eureka

    服务治理 主要用来实现各个微服务实例的自动化注册与发现,为啥需要这玩意呢?在一开始比如A系统调用B服务,可能通过手工维护B服务的实例,并且还得采用负载均衡等方式,这些全部都得需要手工维护,等后面系统越 ...

  10. E浏览器常见的9个css Bug以及解决办法

    我们在浏览网页的时候经常看见这样的现象:某个网页在IE6浏览器中打开很正常,但是在IE8里面打开可能完全变形了.或者也有可能出现完全相反的现象.这让Web程序员及设计师往往为了其CSS在各个IE版本下 ...