LeetCode 136 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?

分析

请參照上一题:LeetCode 260 Single Number III(仅仅出现一次的数字3)(*)

另一道与之相应的题:LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

代码

class Solution {
public:
 unsigned int FindFirstBigIs1(int num) {
    int indexBit = 0;
    while (((num & 1) == 0) && (indexBit < 8 * sizeof(int))) {
        num = num >> 1;
        ++indexBit;
    }
    return indexBit;
}

int singleNumber(vector<int>& nums) {
    if (nums.size() <= 0) return NULL;

    int resultExclusiveOR = 0;
    for (int i = 0; i < nums.size(); ++i)
        resultExclusiveOR ^= nums[i];

    unsigned int indexOf1 = FindFirstBigIs1(resultExclusiveOR);

    int singleNum = 0;
    for (int j = 0; j < nums.size(); ++j) {
            singleNum ^= nums[j];
    }
    return singleNum;
}
};
时间: 2024-10-14 05:59:51

LeetCode 136 Single Number(仅仅出现一次的数字)的相关文章

LeetCode 136 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 i

LeetCode 136. Single Number &amp; 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

LeetCode - 136. Single Number - ( c++ ) - 解题报告

1.题目大意 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? 给定一个数组的整数,数组中的每个元素都出现了两次.例外地,有一个元素只出现

leetcode 136 Single Number bBt Option

Linked Url:https://leetcode.com/problems/single-number/ Given a non-empty 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 withou

[LeetCode] 136.Single Number Java

题目:Given an array of integers, every element appears twice except for one. Find that single one. 题意及分析:一个数组中,有一个数只出现了一次,其他的出现了两次.要求给出只出现一次的数.这道题,最简单的方法是用一个hashtable来储存,然后得出结果,但是效率比较低.也可以用位运算求解.直接使用异或运算,因为对于异或运算有:n^0=n,n^n=0,所以相同的数异或为0,最后得出的结果就为出现一次的数

LeetCode: 136 Single Number(easy)

题目: 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? 代码: 1 class Solution { 2 public: 3 int s

leetcode 136. Single Number &amp;&amp; 137. Single Number II

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? 异或运算应用: 异或运算满足交换律和结合律, 通过交换元素(如插入排序等)可以使得相同元

Java for LeetCode 136 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? 解题思路: 按位异或运算,出现两次的都归零了,剩下的就是出现一次的了,JAVA实现如下

LeetCode 136. Single Number C++

Given a non-empty 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? Example 1: Input: [2,2,1] Output: