社区讨论LinuxDo 最新
Leetcode每日一题 —— 2913. 子数组不同元素数目的平方和 I
力扣 LeetCode 2913. 子数组不同元素数目的平方和 I - 力扣(LeetCode) 2913. 子数组不同元素数目的平方和 I - 给你一个下标从 0 开始的整数数组 nums 。 定义 nums 一个子数组的 不同计数 值如下: 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 nums) { int n = nums.size(); HashSet set = new HashSet< (); int ans = 0; for (int i…
内容摘要
作者:#魔法师
板块:#开发调优
力扣 LeetCode
2913. 子数组不同元素数目的平方和 I - 力扣(LeetCode)
2913. 子数组不同元素数目的平方和 I - 给你一个下标从 0 开始的整数数组 nums 。
定义 nums 一个子数组的 不同计数 值如下:
* 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 <= i <= j < nums.length ),那么我们称子数组 nums[i..j] 中不同值的数目为 nums[i..j] 的不同计数。
请你返回 nums 中所有子数组的 不同计数 的...
思路
简单题简单做,剩下的交给明天。
直接模拟即可。
代码
class Solution {
public int sumCounts(List<Integer> nums) {
int n = nums.size();
HashSet<Integer> set = new HashSet<>();
int ans = 0;
for (int i = 0; i < n; i++) {
set.clear();
for (int j = i; j < n; j++) {
set.add(nums.get(j));
int b = set.size();
ans += b * b;
}
}
return ans;
}
}
资讯来源
LinuxDo