/**
* 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. CF715B. Complete The Graph

    CF715B. Complete The Graph 题意: 给一张 n 个点,m 条边的无向图,要求设定一些边的边权 使得所有边权都是正整数,最终 S 到 T 的最短路为 L 1 ≤ n ≤ 100 ...

  2. Chrome开发者工具面板

    Chrome开发者工具面板 面板上包含了Elements面板.Console面板.Sources面板.Network面板.Timeline面板.Profiles面板.Application面板.Sec ...

  3. URI is not registered ( Setting | Project Settings | Schemas and DTDs )

    URI is not registered ( Setting | Project Settings | Schemas and DTDs ) 在idea中,当初手动第一次写spring配置文件的时候 ...

  4. go-mod 入门

    Q群有人问go mod 问题,自己也忘了些.顺便再整理下. GO111MODULE可以设置为三个字符串值之一:off,on或auto(默认值). off 则go命令从不使用新模块支持.它查找vendo ...

  5. [Lyft Level 5 Challenge 2018 - Elimination Round][Codeforces 1033D. Divisors]

    题目链接:1033D - Divisors 题目大意:给定\(n\)个数\(a_i\),每个数的约数个数为3到5个,求\(\prod_{i=1}^{n}a_i\)的约数个数.其中\(1 \leq n ...

  6. Springboot关于脚本脚本启动的项目:

    #!/bin/bash if [ -f ~/.bash_profile ];then  . ~/.bash_profilefi JAVA_HOME=/usr/local/usr_software/jd ...

  7. 关于使用freemarker导出文档的使用

    7.FreeMarker导出word文件,模板:template.ftl/** * 为word加载数据插值 * * @throws IOException */ public void exportW ...

  8. [Swift]LeetCode97. 交错字符串 | Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  9. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  10. 页面的div中有滚动条,js实现刷新页面后回到记录时滚动条的位置

    当div中绑定数据,给它一个属性overflow-y: scroll,添加长度大小,使其能够出现滚动条:每次刷新的时候滚动条总是会出现在最上方,这使我很头疼,经过查阅网上资料,返现两种方法可行.如下: ...