leetcode------Single Number(2)

标题: Single Number
通过率: 46.0% 
难度:    中等 

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?

第一次刷题目拿到题目后有些懵。线性时间复杂度O(n),那么不能用循环遍历了,实在是不会。

想到的方法就是存入map,然后进行比较,最后输出value为1的map的key。

然后去网上查资料。发现这个题目原来是考察“位”操作的,解释如下:

如果一个序列是A={1,2,3,4,3,1,3,2}也许还不明白为什么用位操作那么我整理成如下方式

序列变成B={1,1,2,2,3,3,4}后是不是有点思路?其实我当时也没有。然后看大神说就是“异或“

针对容易的序列B进行分析,在位运算中:

1、0^【自身】=【自身】

2、a^b=b^a

那么a^b^c^a=a^a^b^c这就是为什么我把序列A整理成序列B,有了条件1,条件2,我们发现B中的1^1=0,2^2=0……以此类推最后只有0^4输出后刚好是4,同理即使乱序序列我们不管他们是否一样或者不同,都仅仅是个中间件而已相同的早晚会被抵消,最后只剩下单独的那个。

最后Java代码如下:

1 public class Solution {
2     public int singleNumber(int[] A) {
3         int xor=0;
4         for(int i=0;i<A.length;i++){
5             xor=xor^A[i];
6         }
7         return xor;
8     }
9 }

第一次成功,以上代码应该是效率最高的。我认为

时间: 2024-09-30 19:05:18

leetcode------Single Number(2)的相关文章

LeetCode——Single Number(II)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm shou

LeetCode&mdash;&mdash;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 II [137]

[题目] 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? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

[LeetCode] Distinct Subsequences(DP)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

[LeetCode] Decode Ways(DP)

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded

hdu 5623 KK&#39;s Number(dp)

问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗10?4??)个数,每次KK都会先拿数.每次可以拿任意多个数,直到NN个数被拿完.每次获得的得分为取的数中的最小值,KK和对手的策略都是尽可能使得自己的得分减去对手的得分更大.在这样的情况下,最终KK的得分减去对手的得分会是多少? 输入描述 第一行一个数T\left( 1\leq T\leq 10\right)T(1≤T≤10),表示数据组

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,

【leetcode】Candy(python)

题目要求比其高的邻居要比本身的奖励多,那么最少也要多一个,所有我们可以找到所有的凹点,凹点如下三种情形. 找到所有的凹点后,我们就可以从凹点处开始向左右两个方向依次查找递增序列,其中每个高的都要比相邻的矮的多一个,比如1,2,5,4.我们找到凹点为1 和4,那么从1开始向左没有其他点,我们向右,依次得到2 比1高,2的糖果应该是1的基础上加1,为2, 5比2高,5的糖果是在2的基础上加1,为3.令一个凹点4, 向左,5比4高,5的糖果应该是在4的基础上加 1,为2,这时我们发现冲突了,从凹点1

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

LeetCode: Single Number [136]

[题目] 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? [题意] 给定一个整数数组,其中除了一个数以外,其他数都是成对出现的,找出这