一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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:

+ The order of the result is not important. So in the above example, [5, 3] is also correct.

+ Your algorithm should run in linear runtime complexity.

+ Could you implement it using only constant space complexity?

(二)解题

题目大意:在一个数组中有两个数只出现过一次,其他的数都出现两次,找出这两个数

解题思路:对比【一天一道LeetCode】#137. Single Number,如果有两个只出现一次的数,那么用异或运算可以找出这两个数的异或值

从这个异或值下手,这两个数不同,那么就可以找出其中不同的位,取一个不同的位,如第M位。

将整个数组分为两类,一类为第M位为1,一类为第M类为0,这样在每一组数中只有一个只出现一次的数,很容易就用异或运算来解决了。

具体思路见代码:

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        vector<int> ret;
        int size = nums.size();
        if(size == 0) return ret;
        int temp = nums[0];
        for(int i = 1 ; i < size ;i++) temp^=nums[i];//求全部异或后的最终值
        unsigned int bit;
        for(int i = 0 ; i < 32 ; i++) {//找到temp中第一个为1的位
            bit= 1<<i;
            if((temp&bit)==bit) break;
        }
        int temp1 =0 , temp2 =0;//temp1为第一个只出现一次的数,temp2为第二个
        for(int i = 0 ; i < size ;i++){
            if((nums[i]&bit)==0) {//分组,第M位上为0的组
                temp1^=nums[i];
            }
            else{
                temp2^=nums[i];//第M位上为1的组
            }
        }
        ret.push_back(temp1);
        ret.push_back(temp2);
        return ret;
    }
};

相关题目:

【一天一道LeetCode】#137. Single Number II

【一天一道LeetCode】#137. Single Number

【一天一道LeetCode】#260. Single Number III的更多相关文章

  1. LeetCode 260. Single Number III(只出现一次的数字 III)

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

  2. [LeetCode] 260. Single Number III 单独数 III

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

  3. [LeetCode#260]Single Number III

    Problem: Given an array of numbers nums, in which exactly two elements appear only once and all the ...

  4. Java [Leetcode 260]Single Number III

    题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...

  5. LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数

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

  6. Leetcode 260 Single Number III 亦或

    在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&am ...

  7. [LeetCode] 260. Single Number III(位操作)

    传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...

  8. leetcode 136 Single Number, 260 Single Number III

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

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

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

  10. 【刷题-LeeetCode】260. Single Number III

    Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...

随机推荐

  1. SpringBoot学习之基础篇

    在前面的博文中,已经演示过springboot与Mybatis集成的实例,本篇再来探讨一下SpringBoot的基础.  一.关于SpringBoot SpringBoot可以基于Spring轻松创建 ...

  2. SQL优化实用方法

    SQL优化:避免索引失效 1.不使用NULL 任何在where子句中使用is null或is not null的语句优化器是不允许使用索引的.因为只有该字段中有null值,即使创建了索引其实也是没有用 ...

  3. box-sizing position

    box-sizing 属性 用于更改用于计算元素宽度和高度的默认的 CSS 盒子模型.可以使用此属性来模拟不正确支持CSS盒子模型规范的浏览器的行为. /* 关键字 值 */ box-sizing: ...

  4. SQL使用总结-like,MAX,MIN

    1. 时间索引不容许使用like 对时间索引适应like,会时间索引变成字符串操作,成为遍历动作,失去索引价值. 错误写法: EXPLAIN  SELECT AVG(data_value) AS av ...

  5. 点(x1, y1)关于点(x0, y0)逆时针旋转a度后的坐标求解

    问题描述: 求点(x1, y1)关于点(x0, y0)逆时针旋转a度后的坐标 思路: 1.首先可以将问题简化,先算点(x1, y1)关于源点逆时针旋转a度后的坐标,求出之后加上x0,y0即可. 2.关 ...

  6. Linux下常用设置文件和文件夹读写权限操作

    1.查看权限 ls -l xxx.xxx (xxx.xxx是文件名)   2.常见权限 -rw------- (600) 只有所有者才有读和写的权限 -rw-r--r-- (644) 只有所有者才有读 ...

  7. python显示中文出错以及抛出UnicodeDecodeError的处理办法

    程序开头粘贴如下代码即可: # -*- coding:utf-8 -*- import sys default_encoding = 'utf-8' if sys.getdefaultencoding ...

  8. IP地址段遍历

    #region 搜索ftp服务器地址 /// <summary> /// 搜索ftp服务器 /// </summary> public void SearchFtpServer ...

  9. AIX 命令

    1,[ctrl]+h 删除命令 2, set -o emacs后: [ctrl]+p 看上条命令 [ctrl]+n 看下条命令 两次[esc] 自动补全 3, set -o vi 后,可以按照vi编辑 ...

  10. React 系列教程 1:实现 Animate.css 官网效果

    前言 这是 React 系列教程的第一篇,我们将用 React 实现 Animate.css 官网的效果.对于 Animate.css 官网效果是一个非常简单的例子,原代码使用 jQuery 编写,就 ...