欢迎光临散文网 会员登陆 & 注册

LeetCode 1390. Four Divisors

2023-04-22 16:35 作者:您是打尖儿还是住店呢  | 我要投稿

Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.

 

Example 1:

Input: nums = [21,4,7]

Output: 32

Explanation: 

21 has 4 divisors: 1, 3, 7, 21 

4 has 3 divisors: 1, 2, 4 

7 has 2 divisors: 1, 7 

The answer is the sum of divisors of 21 only.

Example 2:

Input: nums = [21,21]

Output: 64

Example 3:

Input: nums = [1,2,3,4,5]

Output: 0

 

Constraints:

  • 1 <= nums.length <= 104

  • 1 <= nums[i] <= 105

主要就是计算有4个divisor的数字和,那么我们用set去储存,遍历从1到这个数的平方根,

如果能除尽,那么2个因子都放到set中,最后判断set的大小为4,则返回每个数字的和,

如果不是4,返回0;

下面是代码:

Runtime: 126 ms, faster than 11.96% of Java online submissions for Four Divisors.

Memory Usage: 43.2 MB, less than 31.58% of Java online submissions for Four Divisors.


LeetCode 1390. Four Divisors的评论 (共 条)

分享到微博请遵守国家法律