LeetCode_136. Single Number
136. Single Number
Given a non-empty 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?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
package leetcode.easy; import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; public class SingleNumber {
@org.junit.Test
public void test() {
int[] nums1 = { 2, 2, 1 };
int[] nums2 = { 4, 1, 2, 1, 2 };
System.out.println(singleNumber1(nums1));
System.out.println(singleNumber1(nums2));
System.out.println(singleNumber2(nums1));
System.out.println(singleNumber2(nums2));
System.out.println(singleNumber3(nums1));
System.out.println(singleNumber3(nums2));
System.out.println(singleNumber4(nums1));
System.out.println(singleNumber4(nums2));
} public int singleNumber1(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
int result = 0;
for (int i = 0; i < nums.length; i++) {
if (!set.contains(nums[i])) {
set.add(nums[i]);
} else {
set.remove(nums[i]);
}
}
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
result = it.next();
}
return result;
} public int singleNumber2(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int result = 0;
for (int i = 0; i < nums.length; i++) {
if (!map.containsKey(nums[i])) {
map.put(nums[i], 1);
} else {
map.remove(nums[i]);
}
}
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
result = it.next();
}
return result;
} public int singleNumber3(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
int sumSet = 0;
int sum = 0;
for (int i = 0; i < nums.length; i++) {
set.add(nums[i]);
sum += nums[i];
}
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
sumSet += it.next();
}
return 2 * sumSet - sum;
} public int singleNumber4(int[] nums) {
int a = 0;
for (int i = 0; i < nums.length; i++) {
a ^= nums[i];
}
return a;
}
}
LeetCode_136. 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(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- [LintCode] Single Number 单独的数字
Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- LeetCode 【Single Number I II III】
Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
随机推荐
- P1330 封锁阳光大学[搜索+染色]
题目来源:洛谷 题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构成的无向图 ...
- 系统间HTTP调用代码封装
痛点 最近接手一个老项目,这个项目几经转手,到我这里时,发现代码的可阅读性实在是很差,对于一个有点代码洁癖的我来说,阅读起来实在是很难受.其中一个痛点,现在就拉出来讲讲.该项目需要与另外一个项目进行业 ...
- 多任务案例--文件夹copy.py
import os import multiprocessing def copy_file(q,file_name,new_folder_name,old_folder_name): with op ...
- 定时器 间隔调用setInterval
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [Dart] Understand Classes and Inheritance in Dart
We will look at how we can create classes and explore some various features. Dart adopts a single-in ...
- PHP流程控制之if语句多种嵌套
王思总同学我们在最开始的故事中讲到了他有两个秘书:一个生活秘书.一个工作秘书. 王思总同学在出行和项目中也是极度有计划性.他给自己的生活秘书和工作秘书分别指派了出差的行程:大理石平台支架 生活上: 先 ...
- 洛谷 P2580 于是他错误的点名开始了 题解
每日一题 day10 打卡 Analysis trie树模板题,只需用到简单的插入和查询就好了 如果想要学trie树,见信息学奥赛一本通·提高篇P82 #include<iostream> ...
- [转]C++ 模板 静态成员 定义(实例化)
提出问题: 如果有这样一个模板: template <typename T> class Test{ public: static std::string info; }; 对于以下若干种 ...
- Luogu2791 幼儿园篮球题【斯特林数,数学】
题目链接:洛谷 我一开始不知道$N,M$有什么用处,懵逼了一会儿,结果才发现是输入数据范围... $$\begin{aligned}\binom{n}{k}Ans&=\sum_{i=0}^k\ ...
- 洛谷P1419寻找段落
题目 单调队列+前缀和 #include <bits/stdc++.h> #define N 101001 using namespace std; int n, s, t; int da ...