commit
fc434551c9
53
src/main/java/com/leetcode/array/SingleNumber.java
Normal file
53
src/main/java/com/leetcode/array/SingleNumber.java
Normal file
@ -0,0 +1,53 @@
|
||||
package com.leetcode.array;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 只出现一次的数字
|
||||
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
|
||||
|
||||
说明:
|
||||
|
||||
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
|
||||
|
||||
示例 1:
|
||||
|
||||
输入: [2,2,1]
|
||||
输出: 1
|
||||
示例 2:
|
||||
|
||||
输入: [4,1,2,1,2]
|
||||
输出: 4
|
||||
|
||||
作者:力扣 (LeetCode)
|
||||
链接:https://leetcode-cn.com/leetbook/read/top-interview-questions/xm0u83/
|
||||
来源:力扣(LeetCode)
|
||||
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|
||||
*/
|
||||
public class SingleNumber {
|
||||
|
||||
|
||||
public int singleNumber(int[] nums) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
Integer counter = map.getOrDefault(nums[i], 0);
|
||||
counter ++;
|
||||
map.put(nums[i], counter);
|
||||
}
|
||||
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
|
||||
if (entry.getValue() == 1) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SingleNumber singleNumber = new SingleNumber();
|
||||
int[] nums = {2,2,1};
|
||||
int res = singleNumber.singleNumber(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user