leetCode/src/main/java/com/leetcode/array/SingleNumber.java

54 lines
1.4 KiB
Java
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}