136. Single Number 求数组中单一的数字
来源:互联网 发布:拼多多衣服比淘宝便宜 编辑:程序博客网 时间:2024/05/29 02:56
给定的整数的数组,除了其中一个元素出现一次,剩余每个元素出现两次。找出单独出现的这个元素。
你应该有一个线性时间复杂度的算法。你能实现它,而无需使用额外的内存?
we use bitwise XOR to solve this problem :first , we have to know the bitwise XOR in java1、0 ^ N = N2、N ^ N = 0So..... if N is the single numberN1 ^ N1 ^ N2 ^ N2 ^..............^ Nx ^ Nx ^ N= (N1^N1) ^ (N2^N2) ^..............^ (Nx^Nx) ^ N= 0 ^ 0 ^ ..........^ 0 ^ N= N
public int singleNumber(int[] nums) { int ans =0; int len = nums.length; for(int i=0;i!=len;i++) ans ^= nums[i]; return ans; }
用异或的解法,牛逼
其他解法
public class Solution { public int singleNumber(int[] nums) { int count=1; int a=0; Map<Integer,Integer> map=new HashMap(); for(int num:nums){ if(!map.containsKey(num)) map.put(num,count); else map.put(num,count+1); } Set<Integer> ks = map.keySet(); Iterator<Integer> it = ks.iterator(); while (it.hasNext()) { Integer key = it.next(); if(map.get(key)==1) a=key; } return a; }}
阅读全文
0 0
- 136. Single Number 求数组中单一的数字
- Single Number --- 找出数组中出现一次的数字
- Single Number III & 数组中只出现一次的数字
- 136. Single Number (查找数组中只出现一次的数字)
- single-number、single-number2,数组中只出现一次的数字
- 【leetcode】数组中找出只出现一次的数字(Single Number)
- 【LeetCode】- Single Number(从数组中找出只出现一次的数字)
- Leetcode:136.Single Number 找到数组里面的非重复数字,其他数字也最多重复两次
- [leetcode] 【数组】 136. Single Number
- 位运算-Single Number II(给定一个数组,除了一个数字出现一次,其他都出现三次,求出现一次的数)
- 找出数组中只出现一次的数(single number )
- leetcode_136 Single Number-找数组中唯一的单身数
- 136.Single Number (数组中只出现一次的数)
- [LeetCode]136. Single Number(找出数组中单独出现的数)
- 找到单独的数字 Single Number
- 136. Single Number--取出非重复数字
- LeetCode 136. Single Number(单个数字)
- 求数组中出现一次的数字
- Unable to load configuration.
- Sublime Text3 简体中文汉化
- asp.net 获取GridView中a标签并进行操作
- CSS基础(一)入门
- linux-watch
- 136. Single Number 求数组中单一的数字
- Ajax主要用来完成哪些工作?
- 访问rabbitmq web管理页面undefined: There is no template at js/tmpl/login.ejs
- 设置Xcode调试模式,运行成功后等待用户交互后再启动
- HTML与CSS篇----清除浮动塌陷
- python时间序列平稳性定义及代码相关
- WebService技术
- Java设计模式 工场模式 从共享单车说起
- iBATIS update 特殊字符处理