美丽下标对的数目(Lc2748)——计数

给你一个下标从 0 开始的整数数组 nums 。如果下标对 ij 满足 0 ≤ i < j < nums.length ,如果 nums[i] 的 第一个数字 和 nums[j] 的 最后一个数字 互质 ,则认为 nums[i] 和 nums[j] 是一组 美丽下标对 。

返回 nums 中 美丽下标对 的总数目。

对于两个整数 x 和 y ,如果不存在大于 1 的整数可以整除它们,则认为 x 和 y 互质 。换而言之,如果 gcd(x, y) == 1 ,则认为 x 和 y 互质,其中 gcd(x, y) 是 x 和 y 的 最大公因数 。

示例 1:

输入:nums = [2,5,1,4]
输出:5
解释:nums 中共有 5 组美丽下标对:
i = 0 和 j = 1 :nums[0] 的第一个数字是 2 ,nums[1] 的最后一个数字是 5 。2 和 5 互质,因此 gcd(2,5) == 1 。
i = 0 和 j = 2 :nums[0] 的第一个数字是 2 ,nums[2] 的最后一个数字是 1 。2 和 1 互质,因此 gcd(2,1) == 1 。
i = 1 和 j = 2 :nums[1] 的第一个数字是 5 ,nums[2] 的最后一个数字是 1 。5 和 1 互质,因此 gcd(5,1) == 1 。
i = 1 和 j = 3 :nums[1] 的第一个数字是 5 ,nums[3] 的最后一个数字是 4 。5 和 4 互质,因此 gcd(5,4) == 1 。
i = 2 和 j = 3 :nums[2] 的第一个数字是 1 ,nums[3] 的最后一个数字是 4 。1 和 4 互质,因此 gcd(1,4) == 1 。
因此,返回 5 。

示例 2:

输入:nums = [11,21,12]
输出:2
解释:共有 2 组美丽下标对:
i = 0 和 j = 1 :nums[0] 的第一个数字是 1 ,nums[1] 的最后一个数字是 1 。gcd(1,1) == 1 。
i = 0 和 j = 2 :nums[0] 的第一个数字是 1 ,nums[2] 的最后一个数字是 2 。gcd(1,2) == 1 。
因此,返回 2 。

提示:

  • 2 <= nums.length <= 100
  • 1 <= nums[i] <= 9999
  • nums[i] % 10 != 0

问题简要描述:返回美丽下标对的总数目 

细节阐述:

  1. 数组 cnt 来记录每个数字的第一个数字出现的次数

Java

class Solution {
    public int countBeautifulPairs(int[] nums) {
        int[] cnt = new int[10];
        int ans = 0;
        for (int x : nums) {
            for (int i = 0; i < 10; i++) {
                if (cnt[i] > 0 && gcd(i, x % 10) == 1) {
                    ans += cnt[i];
                }
            }
            while (x > 9) {
                x /= 10;
            }
            cnt[x]++;
        }
        return ans;
    }

    int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
}

 Python3

class Solution:
    def countBeautifulPairs(self, nums: List[int]) -> int:
        cnt = [0] * 10
        ans = 0
        for x in nums:
            for i in range(10):
                if cnt[i] > 0 and gcd(i, x % 10) == 1:
                    ans += cnt[i]
            cnt[int(str(x)[0])] += 1
        return ans

 TypeScript

function countBeautifulPairs(nums: number[]): number {
    let cnt: number[] = Array(10).fill(0);
    let ans = 0;
    let gcd = (a: number, b: number): number => {
        return b == 0 ? a : gcd(b, a % b);
    };
    for (let x of nums) {
        for (let i = 0; i < 10; i++) {
            if (cnt[i] > 0 && gcd(i, x % 10) == 1) {
                ans += cnt[i];
            }
        }
        while (x > 9) {
            x = Math.floor(x / 10);
        }
        cnt[x]++;
    }
    return ans;
};

C++

class Solution {
public:
    int countBeautifulPairs(vector<int>& nums) {
		int cnt[10]{};
		int ans = 0;
		for (auto x : nums) {
			for (int i = 0; i < 10; i++){
				if (cnt[i] > 0 && gcd(i, x % 10) == 1) {
					ans += cnt[i];
				}
			}
			while (x > 9) {
				x /= 10;
			}
			cnt[x]++;
		}
		return ans;       
    }
};

Go

func countBeautifulPairs(nums []int) int {
	cnt := [10]int{}
	ans := 0
	for _, x := range nums {
		for i := 0; i < 10; i++ {
			if cnt[i] > 0 && gcd(i, x%10) == 1 {
				ans += cnt[i]
			}
		}
		for x > 9 {
			x /= 10
		}
		cnt[x]++
	}
	return ans
}

func gcd(a int, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值