3与5的最大公约数,3和5之间的最大公约数

首页 > 教育 > 作者:YD1662023-04-17 01:15:12

题目:

给你一个由正整数组成的数组 nums 。

数字序列的 最大公约数 定义为序列中所有整数的共有约数中的最大整数。

例如,序列 [4,6,16] 的最大公约数是 2 。
数组的一个 子序列 本质是一个序列,可以通过删除数组中的某些元素(或者不删除)得到。

例如,[2,5,10] 是 [1,2,1,2,4,1,5,10] 的一个子序列。
计算并返回 nums 的所有 非空 子序列中 不同 最大公约数的 数目 。

示例 1:



3与5的最大公约数,3和5之间的最大公约数(1)

image.png



输入:nums = [6,10,3]
输出:5
解释:上图显示了所有的非空子序列与各自的最大公约数。
不同的最大公约数为 6 、10 、3 、2 和 1 。

示例 2:
输入:nums = [5,15,40,5,6]
输出:7

java代码:

class Solution { public int countDifferentSubsequenceGCDs(int[] nums) { int maxVal = Arrays.stream(nums).max().getAsInt(); boolean[] occured = new boolean[maxVal 1]; for (int num : nums) { occured[num] = true; } int ans = 0; for (int i = 1; i <= maxVal; i ) { int subGcd = 0; for (int j = i; j <= maxVal; j = i) { if (occured[j]) { if (subGcd == 0) { subGcd = j; } else { subGcd = gcd(subGcd, j); } if (subGcd == i) { ans ; break; } } } } return ans; } public int gcd(int num1, int num2) { while (num2 != 0) { int temp = num1; num1 = num2; num2 = temp % num2; } return num1; } }

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.