/**
* Source : https://oj.leetcode.com/problems/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?
*
*/
public class SingleNumber { /**
* 数组中每个元素都有相同的两个,只有其中一个元素是只有一个的,找出这个元素
* 1. 对数组排序,将arr[i] 和arr[i-1]、arr[i+1]进行比较,如果又都不相等,那么就是要找的元素
* 2. 使用hash表,循环数组,判断hash表中是否有这个元素,如果有则删除hash表的元素,否则将当前元素加入hash表,最后hash表中剩下的是要找的元素
* 3. 使用异或运算,xor,一个数和自身异或运算为0,x^x = 0, x^0 = x
*
* @param arr
* @return
*/
public int singleNumber (int[] arr) {
int res = 0;
for (int i = 0; i < arr.length; i++) {
res ^= arr[i];
}
return res;
} public static void main(String[] args) {
SingleNumber singleNumber = new SingleNumber();
System.out.println(singleNumber.singleNumber(new int[]{1,1,2,2,3}) + " == 3");
}
}

leetcode — single-number的更多相关文章

  1. [LeetCode] Single Number III 单独的数字之三

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

  2. [LeetCode] Single Number II 单独的数字之二

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

  3. [LeetCode] Single Number 单独的数字

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

  4. LeetCode Single Number I / II / III

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

  5. LeetCode:Single Number II

    题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...

  6. LeetCode Single Number III

    原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...

  7. [leetcode]Single Number II @ Python

    原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...

  8. LeetCode——Single Number II(找出数组中只出现一次的数2)

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

  9. LeetCode: Single Number I && II

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

  10. [LeetCode] Single Number III ( a New Questions Added today)

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

随机推荐

  1. 机器学习——KMeans

    导入类库 from sklearn.cluster import KMeans from sklearn.datasets import make_blobs import numpy as np i ...

  2. 创建python虚拟环境如果速度很慢

    conda create -n jjenv python=3.6如果我们这样子创建的话下载速度很慢,那就可以用如下方式,相当于改了下载源. conda create -n jjenv python=3 ...

  3. iOS 开发中单元格cell高度自适应

    高度自适应分下面两种情况 1.用代码自定义的cell 用代码自定义的cell,cell高度自定义需要我们手动的去计算每个cell的字符串高度.然后返回对应的高度即可. 2.用XIB 或者 StoreB ...

  4. 微信小程序统计分析

    在微信公众平台社区看到一个不错的东西,小博统计:https://www.wxappdev.com/:用于微信小程序统计分析.

  5. Gradle 学习二

    按照本指南,您将创建一个简单的Gradle项目,调用一些基本的Gradle命令,并了解Gradle如何管理项目 1.初始化项目创建项目目录 ❯ mkdir basic-demo ❯ cd basic- ...

  6. let和const

    ES6新增了let取代var,let主要有以下特点. 1 只在代码块内有效,代码块外不能使用let声明的变量.let很适合声明循环体的变量. 它可以解决一些闭包的问题存在的问题比如: var a = ...

  7. 使用自建Git服务器管理私有项目 Centos 7.3 + Git 2.11.0 + gitosis (实测 笔记)

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso GIT服务器IP:192.168.1 ...

  8. Consider defining a bean named 'entityManagerFactory' in your configuration解决办法

    错误信息: *************************** APPLICATION FAILED TO START *************************** Descriptio ...

  9. CPU运行原理

    问题: CPU位宽表示什么意思? 下面这个是 https://www.bilibili.com/video/av9667986?from=search&seid=336127932106862 ...

  10. ora-01033 oracle initialization or

    这次出现这个问题是源于错删了 DBF文件. 解决方案如下: 1.打开SQL Plus 最后把你删掉的那个文件的表空间删掉就好了