Single Number 解答

时间:2022-09-23 00:35:32

Question

Given an array of integers, every element appears twice except for one. Find that single one.

Solution 1 -- Set

We can use a hash set to record each integer's appearing time. Time complexity O(n), space cost O(n)

 public class Solution {
public int singleNumber(int[] nums) {
Set<Integer> counts = new HashSet<Integer>();
int length = nums.length, result = nums[0];
for (int i = 0; i < length; i++) {
int tmp = nums[i];
if (counts.contains(tmp))
counts.remove(tmp);
else
counts.add(tmp);
}
for (int tmp : counts)
result = tmp;
return result;
}
}

Solution 2 -- Bit Manipulation

The key to solve this problem is bit manipulation. XOR will return 1 only on two different bits. So if two numbers are the same, XOR will return 0. Finally only one number left.

 public int singleNumber(int[] A) {
int x = 0;
for (int a : A) {
x = x ^ a;
}
return x;
}

Single Number 解答的更多相关文章

  1. LeetCode 136&period; Single Number C&plus;&plus; 结题报告

    136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...

  2. 异或巧用:Single Number

    异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...

  3. Single Number 普通解及最小空间解(理解异或)

    原题目 Given a non-empty array of integers, every element appears twice except for one. Find that singl ...

  4. &lbrack;LeetCode&rsqb; Single Number III 单独的数字之三

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  5. &lbrack;LeetCode&rsqb; Single Number II 单独的数字之二

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  6. &lbrack;LeetCode&rsqb; Single Number 单独的数字

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  7. LeetCode Single Number I &sol; II &sol; III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  8. LeetCode&mdash&semi;&mdash&semi;Single Number II(找出数组中只出现一次的数2)

    问题: Given an array of integers, every element appears three times except for one. Find that single o ...

  9. &lbrack;LintCode&rsqb; Single Number 单独的数字

    Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in ...

随机推荐

  1. if -else 条件语句原理

    #!/usr/bin/python # coding utf-8 name = '?' if name == 'python': print('欢迎BOSS') else: print('输入错误')

  2. java程序打包成jar文件,使用到第三方jar包

    1.右击工程选择Export—>选择JAR file—>选择NEXT,如下图所示 2.选择需要打包的工程,并且选择存放目录,我这放在 E:\jartest 目录下,然后点击NEXT,如下图 ...

  3. mybatis3&period;3 &plus; struts2&period;3&period;24 &plus; mysql5&period;1&period;22开发环境搭建及相关说明

    一.新建Web工程,并在lib目录下添加jar包 主要jar包:struts2相关包,mybatis3.3相关包,mysql-connector-java-5.1.22-bin.jar, gson-2 ...

  4. JS&lpar;三&rpar;

    1.检查用户名中是否含有特殊字符: <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  5. Objective-C 数组、可变数组

    数组的使用方式 下面是数组:类型NSArray #import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int ...

  6. DDD分层架构之领域实体(基础篇)

    DDD分层架构之领域实体(基础篇) 上一篇,我介绍了自己在DDD分层架构方面的一些感想,本文开始介绍领域层的实体,代码主要参考自<领域驱动设计C#2008实现>,另外参考了网上找到的一些示 ...

  7. 〖Linux〗Linux的smb地址转换Windows格式(两者互转)

    因为个人常用办公PC是Linux,打开文件共享什么的是 smb:// 的,而不是Windows的 \\ 需要复制文件路径给别人的时候,发smb://给一个使用Windows办公的用户不算很得体的方法 ...

  8. Intersection(Check)

    Intersection http://poj.org/problem?id=1410 Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  9. 可能是最通俗易懂的 Java 位操作运算讲解

    https://blog.csdn.net/briblue/article/details/70296326

  10. 我的Android进阶之旅------>Android中MediaButtonReceiver广播监听器的机制分析

    今天看公司的一段关于MediaButtonReceiver的代码看的比较混乱,幸好看了下面的这篇文章,才能茅塞顿开的理解好代码.在此转载下来,以备以后理解,希望都到这篇文章的人也能够有所帮助. 本文转 ...