137. Single Number II
Posted Carl_Hugo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了137. Single Number II相关的知识,希望对你有一定的参考价值。
https://leetcode.com/problems/single-number-ii/description/
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
解题思路:
使用HashMap存每个元素出现的次数
import java.util.Map.Entry;
public class Solution
public int singleNumber(int[] nums)
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
int result=0;
for(int i:nums)
if(!map.containsKey(i))
map.put(i, 1);
else
map.put(i, map.get(i)+1);
Set<Entry<Integer,Integer>> entrySet = map.entrySet();
for(Entry es:entrySet)
int count = (Integer)es.getValue();
if(count!=3)
result = (Integer)es.getKey();
break;
return result;
以上是关于137. Single Number II的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III