leetcode || 136、Single Number

problem:

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?

Hide Tags

Hash Table Bit
Manipulation

题意:一组数,只有一个数出现一次,其他数都出现两次,找出这个数

thinking:

(1)考察位运算,C/C++的异或运算符为  ^

0^a=a;

a^a=0;

a^b=b^a;

(2)这道题的解法就出来了:n个数的异或结果就是待求数

code:

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

    }
};

本题扩展,参考http://www.cnblogs.com/changchengxiao/p/3413294.html

1.一个数组中有两个元素只出现一次,其他所有元素都出现两次,求这两个只出现一次的元素

[解题思路]

将数组所有元素都进行异或得到一个不为0的结果,根据这个结果中的不为0的某一位将数组分成两组

将两组中的元素进行异或,如两个数组的异或值都不为0,则得到最后结果

2.一个数组中有一个元素只出现1次,其他所有元素都出现k次,求这个只出现1次的元素

[解题思路]

当k为偶数时,同lss

当k为奇数时,将数组中每个元素的每一位相加mod k,得到结果即位出现1次的元素,时间复杂度O(nlen),空间复杂度为O(1)

时间: 2024-10-06 15:45:10

leetcode || 136、Single Number的相关文章

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

The question: 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? My analysis: Thi

leetcode || 137、Single Number II

problem: 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? Hide Tags Bit Manipulation 题

我也来刷LeetCode——1、Single Number

这道题目的意思大概是这样: 给我一个整型数组,里面的元素都出现两次,但是有一个元素只出现一次,你要把这个只出现一次的元素给找出来.并且要求算法的时间复杂度为线性,即O(N). 一开始我思考了很久,始终没有找到方法.若是不限定元素类型为整型,那么根本不可能在线性时间内找到这个只出现一次的元素.所以我想突破点应该在于元素类型为整型. 为什么整型就可以做到在线性时间内得出结果呢?我点开了该题目所属的标签,[Bit Manipulation]——位操作.好像突然间抓住了什么,位操作是个很神奇的东西,它的

Leetcode 位运算 Single Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Single Number Total Accepted: 20063 Total Submissions: 44658 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear

【一天一道LeetCode】#260. Single Number III

一天一道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 a

LeetCode OJ 之 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. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The or

LeetCode 136、137:Single Number I &amp; II

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? Single Number II Given an arr

leetcode第136题-Single Number

题目要求:给出一个数组,只有一个数字出现一次,其他的都出现两次,找出那出现一次的数字,要求用线性的时间解出题目! 分析:因为题目要求的是用线性时间,所以类似于那种暴力解决的方法会超时,如下面这种: int singleNumber2(int *nums,int n) { int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(nums[i]==nums[j]) break; else continue; } if(j==n) return num

LeetCode 笔记26 Single Number II

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? 没辙,智商碾压题.楼主没遇到之前就只会这种做法. public int si