【LetMeFly】2748.美丽下标对的数目:模拟
力扣题目链接:https://leetcode.cn/problems/number-of-beautiful-pairs/
给你一个下标从 0 开始的整数数组 nums 。如果下标对 i、j 满足 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 和 k 最大公因数 。
示例 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 和 5 互质,因此 gcd(2,1) == 1 。
i = 1 和 j = 2 :nums[1] 的第一个数字是 5 ,nums[2] 的最后一个数字是 1 。2 和 5 互质,因此 gcd(5,1) == 1 。
i = 1 和 j = 3 :nums[1] 的第一个数字是 5 ,nums[3] 的最后一个数字是 4 。2 和 5 互质,因此 gcd(5,4) == 1 。
i = 2 和 j = 3 :nums[2] 的第一个数字是 1 ,nums[3] 的最后一个数字是 4 。2 和 5 互质,因此 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 <= 1001 <= nums[i] <= 9999nums[i] % 10 != 0
解题方法:模拟
两层循环模拟 i i i和 j j j即可。
获得某个数十进制下第一位的方法:
对于整数 n n n,当 n ≥ 10 n\geq 10 n≥10时不断令 n = ⌊ n 10 ⌋ n=\lfloor\frac{n}{10}\rfloor n=⌊10n⌋,最终的 n n n即为所求。
获得某个数十进制下最后一位的方法:
对于整数 n n n, n m o d 10 n\mod 10 nmod10即为所求。
获得两个数的最大公因数的方法:
可用辗转相除法。对于两个数 a a a和 b b b,令 c = a m o d b c=a\mod b c=amodb:
- 若 c = 0 c=0 c=0则 b b b即为所求;
 - 否则令 a , b = b , c a, b = b, c a,b=b,c。
 
- 时间复杂度 O ( n 2 + n log  C ) O(n^2+n\log C) O(n2+nlogC),其中 C C C为一个数的最大值
 - 空间复杂度 O ( 1 ) O(1) O(1)
 
AC代码
C++
            
            
              cpp
              
              
            
          
          class Solution {
private:
    inline int getFirst(int n) {
        while (n >= 10) {
            n /= 10;
        }
        return n;
    }
public:
    int countBeautifulPairs(vector<int>& nums) {
        int ans = 0;
        for (int i = 0; i < nums.size(); i++) {
            for (int j = i + 1; j < nums.size(); j++) {
                if (__gcd(getFirst(nums[i]), nums[j] % 10) == 1) {
                    ans++;
                }
            }
        }
        return ans;
    }
};
        Go
            
            
              go
              
              
            
          
          // package main
func getFirst(n int) int {
    for ; n >= 10; {
        n /= 10;
    }
    return n
}
func gcd(a int, b int) int {
    if b == 0 {
        return a
    }
    return gcd(b, a % b)
}
func countBeautifulPairs(nums []int) int {
    ans := 0
    for i := 0; i < len(nums); i++ {
        for j := i + 1; j < len(nums); j++ {
            if gcd(getFirst((nums[i])), nums[j] % 10) == 1 {
                ans++
            }
        }
    }
    return ans
}
        Java
            
            
              java
              
              
            
          
          // import java.math.BigInteger;
class Solution {
    private BigInteger getFirst(int n) {
        while (n >= 10) {
            n /= 10;
        }
        return BigInteger.valueOf(n);
    }
    public int countBeautifulPairs(int[] nums) {
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (getFirst(nums[i]).gcd(BigInteger.valueOf(nums[j] % 10)).equals(BigInteger.valueOf(1))) {
                    ans++;
                }
            }
        }
        return ans;
    }
}
        Python
            
            
              python
              
              
            
          
          from typing import List
from math import gcd
"""
[2,5,1,4]
"""
class Solution:
    def countBeautifulPairs(self, nums: List[int]) -> int:
        ans = 0
        for i in range(len(nums) - 1):
            for j in range(i + 1, len(nums)):
                if gcd(ord(str(nums[i])[0]) - ord('0'), nums[j] % 10) == 1:
                    ans += 1
        return ans
        同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/139842524