leetcode-1390-Four Divisors
问题
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:
1 | Input: nums = [21,4,7] |
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5
分析
这道题的解题比较暴力,直接对数组中的每个数进行判断即可。当时做的时候一直觉得这样做可能是会超时的,没有正确地估计好时间复杂度,这也是记录下本题的一个主要的原因。在判断每个数 num 是否四个除数的时候只需要遍历到 $\sqrt{ num}$ 即可,时间复杂度为 $O(n\sqrt{ num})$,根据最大数量级的时间复杂度越为 $10^4 \sqrt{ 10^5} = 10^6 * \sqrt{10}$ 。是可以AC的。
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/22/leetcode-1390/
License: 知识共享署名-非商业性使用 4.0 国际许可协议