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: 1

Example 2:

Input: [4,1,2,1,2]
Output: 4

巧用位操作0 ^ n = nn ^ n = 0因为题目说明只有一个数出现一次,其余都出现两次,出现两次的数异或以后为0,0与出现一次的数n异或为n
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int s = 0;
        for(int c : nums)
            s  ^= c;
        return s;

    }
};


原文地址:https://www.cnblogs.com/dingxi/p/11571322.html

时间: 2024-10-16 01:47:07

LeetCode 136. Single Number C++的相关文章

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(仅仅出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 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 yo

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实现如下