我也来刷LeetCode——1、Single Number

  这道题目的意思大概是这样:

  给我一个整型数组,里面的元素都出现两次,但是有一个元素只出现一次,你要把这个只出现一次的元素给找出来。并且要求算法的时间复杂度为线性,即O(N)。

  一开始我思考了很久,始终没有找到方法。若是不限定元素类型为整型,那么根本不可能在线性时间内找到这个只出现一次的元素。所以我想突破点应该在于元素类型为整型。

  为什么整型就可以做到在线性时间内得出结果呢?我点开了该题目所属的标签,【Bit Manipulation】——位操作。好像突然间抓住了什么,位操作是个很神奇的东西,它的背后隐藏着布尔代数的哲学。记得在刚开始学编程的时候,交换a、b两个数可以用三次异或完成,当时始终想不通为什么。

  所以这道题目的突破口已经找到了,常见的位操作有“掩码”,和 1 进行 & 操作,结果不变,和 0 进行 & 操作,该位置为 0 。而这道题要用的位操作是“异或”,“异或”有两个特点:

  1、一个数与任何一个数异或两次,结果为原来的数。

  2、一个数与 0 进行异或,结果为原来的数。

啊!这么看来这道题的答案就出来了!

 1 class Solution {
 2 public:
 3     int singleNumber(vector<int>& nums) {
 4         int result = 0;
 5         for (auto first = nums.cbegin(); first != nums.cend(); first++) {
 6             result ^= *first;
 7         }
 8         return result;
 9     }
10 };
时间: 2024-12-31 03:30:04

我也来刷LeetCode——1、Single Number的相关文章

【LeetCode】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】Single Number (2 solutions)

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? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

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 137 Single Number II(只出现一次的数字 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 im

[LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int bits = sizeof(int)*8; 5 int result=0; 6 for(int i=1; i<=bits; i++) 7 { 8 int w=0; 9 int t=1; 10 11 for(int j=0; j<n; j++) 12 w += (A[j]>>(i-1))&t; 13 result+= (w%3)<

LeetCode之Single Number以及拓展

Problem 1:一个数组中有一个数字a只出现一次,其他数字都出现了两次.请找出这个只出现一次的数字? 考察知识点:异或运算 思路:比如数字 b^b = 0     a^0 = a 因此,可以将数组中的所有数字进行异或,而最终异或的结果即为所求只出现一次的数字a. 代码: 1 def SingleNumber1(Array): 2 ret = 0 3 for i in Array: 4 ret ^= i 5 return ret ================================

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 137 Single Number II(仅仅出现一次的数字 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

[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int i,j; 5 for(i=0; i<n; i++) 6 { 7 for(j=i+1; j<n; j++) 8 { 9 if(A[j]==A[i]) 10 { 11 int temp = A[i+1]; 12 A[i+1] = A[j]; 13 A[j] = temp; 14 i++; 15 break; 16 } 17 } 18 if(j==n) 19

[LeetCode] 260. Single Number III 单独数 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. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is