leetcode — single-number
/**
* 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的更多相关文章
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode: Single Number I && II
I title: Given an array of integers, every element appears twice except for one. Find that single on ...
- [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 ...
随机推荐
- 我的 FPGA 学习历程(09)—— 时序逻辑入门
讲到这篇时,组合逻辑就告一段落了,下面是一些总结: 描述组合逻辑时,always 语句中的敏感信号列表中需要列出全部的可能影响输出的变量 描述组合逻辑时,always 语句中的赋值总是使用阻塞赋值符号 ...
- 通过源码理解HashMap的并发问题
最近在学习有关于Java的基础知识,在学习到HashMap的相关知识的时候,了解了HashMap的并发中会出现的问题,在此记录,加深理解(这篇文章是基于Java1.7的,主要是为了更加直观,更新版本的 ...
- MyBatis3系列__05查询补充&resultMap与resultType区别
1.查询补充 当你查询一条记录并且是简单查询时,情况相对简单,可以参考以下的例子: public Employee getEmpById(Integer id); 对应的xml文件中: <sel ...
- Java Concurrency in Practice——读书笔记
Thread Safety线程安全 线程安全编码的核心,就是管理对状态(state)的访问,尤其是对(共享shared.可变mutable)状态的访问. shared:指可以被多个线程访问的变量 mu ...
- [Ubuntu]pkg-config和ldconfig
转载自->这里 我们知道,linux编译源码包基本步骤无非是:configure,make,make install三部曲:configure过程中可能会遇到无法找到某些头文件和动态库:原因有两 ...
- BZOJ2681 : 玩游戏2
首先若存在多个连通块,那么答案显然是$+\infty$. 否则以$m$为根,每棵子树的根节点都最多只能放一个金币,且这些子树之间互不干扰. 对于一棵父亲为$m$的子树,最优方案下一定可以将子树剖分成若 ...
- 最简单的原生js和jquery插件封装
最近在开发过程中用别人的插件有问题,所以研究了一下,怎么封装自己的插件. 如果是制作jquery插件的话.就将下面的extend方法换成 $.extend 方法,其他都一样. 总结一下实现原理: 将 ...
- 用户注册之后,通过网易邮箱服务器(smtp.163.com)发送电子邮箱到注册者邮箱的的确认通知短信.(可根据需求自行调整)
Member 是数据实体,穿过来的也就是当前注册用户的信息. 存储的数据一定要有邮箱信息 private void SendAuthCodeToMember(Member member) ...
- 我在B站投稿啦、、、
我在B站投稿啦....欢迎评论交流... https://www.bilibili.com/video/av31539882/ 怎样激活Win10系统修改windows系统账户的名称-mp4 外链: ...
- 查找datatable 中的重复记录(只查询一个字段)
StringBuilder str = new StringBuilder(); var res = new ResParameter() { code = ResponseCode.exceptio ...