leetcode刷题打卡(一)
从这篇文章开始,我会记录每天刷leetcode的题目作为打卡记录在该博客上,也希望大家也可以跟随博主一起走进算法的世界,感受算法的魅力。
出现的题目可以根据题目的题号在leetcode找到相关题目的的信息。
- 只出现一次的数字 II
- 题目
给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。
- 我的思路
记录每个数字出现的次数,若出现的次数为1则该数字为正确答案
- 代码
class Solution {
public int singleNumber(int[] nums) {
int res=0;
Map<Integer,Integer> hashMap=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++){
hashMap.put(nums[i],hashMap.getOrDefault(nums[i],0)+1);
}
for(Map.Entry<Integer,Integer> entry:hashMap.entrySet()){
int key=entry.getKey();
int value=entry.getValue();
if(value==1){
res=key;
}
}
return res;
}
}
- 提交结果

3万+



被折叠的 条评论
为什么被折叠?



