Single Number(JAVA)
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 Solution {
public int singleNumber(int[] A) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
if(A.length == 0 || A == null){
return 0;
}
for(int i=0;i<A.length;i++){
if(map.containsKey(A[i])){
int value = map.get(A[i]);
map.put(A[i], value+1);
}else {
map.put(A[i], 1);
}
}
for(int i=0;i<A.length;i++){
int value = map.get(A[i]);
if(value == 1){
return A[i];
}
}
return 0;
}
}
解法2
异或:相同为0 不同为1
0和a数异或都是a
a和a异或就是0
由于每个数有两个 所以总会相见一次 所以还是0
public class singleNumber { /**
* @param args
*/
public static void main(String[] args) {
int a[]={1,-1,3,3,4,1,5,5,-1,6};
System.out.println(singleNumber(a)); } // 异或
public static int singleNumber(int[] A) {
int res = 0;
for (int i : A) {
res ^= i;
}
return res;
} }
屌爆
Single Number(JAVA)的更多相关文章
- leetcode 136. Single Number ----- java
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- Single Number III leetcode java
问题描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- Single Number leetcode java
问题描述: Given an array of integers, every element appears twice 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 III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- Single Number i and ii
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- 异或巧用:Single Number
异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...
随机推荐
- ongl三种符号的使用
1.#符号 访问非根对象属性,由于Struts2中值栈被视为根对象,所以访问其他非根对象时,需要加#前缀.实际上,#相当于ActionContext.getContext(): 用于过滤和投影(pro ...
- (转) Functions
Functions Functions allow to structure programs in segments of code to perform individual tasks. In ...
- SQL基础-->层次化查询(START BY ... CONNECT BY PRIOR)[转]
--====================================================== --SQL基础-->层次化查询(START BY ... CONNECT BY ...
- bootstrap data- jquery .data
jquery官网对.data函数描述是:在匹配元素上存储任意相关数据 或 返回匹配的元素集合中的第一个元素的给定名称的数据存储的值. 存储键值(key/value): $("body&quo ...
- ES6新特性-------数组、Math和扩展操作符(续)
三.Array Array对象增加了一些新的静态方法,Array原型上也增加了一些新方法. 1.Array.from 从类数组和可遍历对象中创建Array的实例 类数组对象包括:函数中的argumen ...
- IClone地形编辑器结合T4M插件在Unity3D使用
通过IClone编辑器打造的地形,经过T4M插件的转化后,资源占用极少,比在Unity中自己刷出来的地形再通过T4M转化的要小的的多多了,非常适合用在手机上. IClone地形编辑器下载地址: 如果对 ...
- Android 圆形按钮实现
项目中用到的圆形按钮,做个半天,用sharp形式实现,样式代码如下: <Button android:id="@+id/btn_5" android:layout_width ...
- Linux dirname、basename(转)
首先使用 --help 参数查看一下.basename命令参数很少,很容易掌握. $ basename --help 用法示例: $ basename /usr/bin/sort 输出&q ...
- C#中List<T>用法
所属命名空间:System.Collections.Generic public classList<T> :IList<T>,ICollection<T>,IEn ...
- PB C/S轉B/S ODBC方式連接數據庫
PB C/S轉B/S ODBC方式連接數據庫,DSN需要建為系統而不是使用者DSN,否則連不上數據庫.