目录
一、概念
计数
二、模板
统计出现一次的数字和:
class Solution {
public int sumOfUnique(int[] nums) {
// 计数数组
int[] count = new int[101];
// 统计次数
for(int num : nums) {
count[num] ++;
}
int sum = 0;
// 遍历
for(int i = 1; i < count.length; i ++) {
if(count[i] == 1) sum += i;
}
return sum;
}
}
三、例题
题:1748. 唯一元素的和
给你一个整数数组 nums 。数组中唯一元素是那些只出现 恰好一次 的元素。
请你返回 nums 中唯一元素的 和 。
示例 1:
输入:nums = [1,2,3,2]
输出:4
解释:唯一元素为 [1,3] ,和为 4 。
示例 2:
输入:nums = [1,1,1,1,1]
输出:0
解释:没有唯一元素,和为 0 。
示例 3 :
输入:nums = [1,2,3,4,5]
输出:15
解释:唯一元素为 [1,2,3,4,5] ,和为 15 。
提示:
1 <= nums.length <= 100
1 <= nums[i] <= 100
解:
解题思路:计数数组的使用
AC代码:
class Solution {
public int sumOfUnique(int[] nums) {
// 计数数组
int[] count = new int[101];
for(int num : nums) {
count[num] ++;
}
int sum = 0;
// 1 <= num <= 100
for(int i = 1; i < count.length; i ++) {
if(count[i] == 1) sum += i;
}
return sum;
}
}
另解:Map
AC代码:
class Solution {
public int sumOfUnique(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
int sum = 0;
for(int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
int temp = map.get(num);
if(temp == 1) {
sum += num;
}
if(temp == 2) { // 出现两次,把第一次删掉
sum -= num;
}
}
return sum;
}
}
题:387. 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
示例:
s = "leetcode"
返回 0
s = "loveleetcode"
返回 2
提示:你可以假定该字符串只包含小写字母。
解:
解题思路:计数模板
AC代码:
class Solution {
public int firstUniqChar(String s) {
int[] dic = new int[26]; // 统计26个字母的次数
for(char c : s.toCharArray()) {
dic[c - 'a'] ++;
}
for(int i = 0; i < s.length(); i ++) {
if(dic[s.charAt(i) - 'a'] == 1){
return i;
}
}
return -1;
}
}
题:1941. 检查是否所有字符出现次数相同
给你一个字符串 s ,如果 s 是一个 好 字符串,请你返回 true ,否则请返回 false 。
如果 s 中出现过的 所有 字符的出现次数 相同 ,那么我们称字符串 s 是 好 字符串。
示例 1:
输入:s = "abacbc"
输出:true
解释:s 中出现过的字符为 'a','b' 和 'c' 。s 中所有字符均出现 2 次。
示例 2:
输入:s = "aaabb"
输出:false
解释:s 中出现过的字符为 'a' 和 'b' 。
'a' 出现了 3 次,'b' 出现了 2 次,两者出现次数不同。
提示:
1 <= s.length <= 1000
s 只包含小写英文字母。
解:
解题思路:计数模板
AC代码:
class Solution {
public boolean areOccurrencesEqual(String s) {
int[] count = new int[26];
for(char c : s.toCharArray()) {
count[c - 'a'] ++;
}
// 词频统计
for(int i = 1; i < s.length(); i ++) {
if(count[s.charAt(i)-'a'] != count[s.charAt(i - 1)-'a']) return false;
}
return true;
}
}
题:448. 找到所有数组中消失的数字
给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内。请你找出所有在 [1, n] 范围内但没有出现在 nums 中的数字,并以数组的形式返回结果。
示例 1:
输入:nums = [4,3,2,7,8,2,3,1]
输出:[5,6]
示例 2:
输入:nums = [1,1]
输出:[2]
提示:
n == nums.length
1 <= n <= 105
1 <= nums[i] <= n
进阶:你能在不使用额外空间且时间复杂度为 O(n) 的情况下解决这个问题吗? 你可以假定返回的数组不算在额外空间内。
解:
解题思路:计数模板
AC代码:
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int[] count = new int[nums.length + 1];
for(int num : nums) count[num] ++;
List<Integer> res = new ArrayList<>();
for(int i = 1; i < count.length; i ++) {
if(count[i] == 0) res.add(i);
}
return res;
}
}
解题思路:原地修改
- 首先含 n 个整数的数组,nums[i] 在区间 [1, n] 内
- 我们只需要将出现过的数字做标记(取负数、取模不变性)
- 遍历数组,将未标记的下标添加返回
AC代码:
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();
// 数组标记
int len = nums.length;
for(int num : nums) {
int index = (num - 1) % len;
nums[index] += len;
}
// 数组遍历
for(int i = 0; i < nums.length; i ++) {
if(nums[i] <= len) res.add(i + 1);
}
return res;
}
}