返回介绍

solution / 2700-2799 / 2748.Number of Beautiful Pairs / README_EN

发布于 2024-06-17 01:03:00 字数 4915 浏览 0 评论 0 收藏 0

2748. Number of Beautiful Pairs

中文文档

Description

You are given a 0-indexed integer array nums. A pair of indices i, j where 0 <= i < j < nums.length is called beautiful if the first digit of nums[i] and the last digit of nums[j] are coprime.

Return _the total number of beautiful pairs in _nums.

Two integers x and y are coprime if there is no integer greater than 1 that divides both of them. In other words, x and y are coprime if gcd(x, y) == 1, where gcd(x, y) is the greatest common divisor of x and y.

 

Example 1:

Input: nums = [2,5,1,4]
Output: 5
Explanation: There are 5 beautiful pairs in nums:
When i = 0 and j = 1: the first digit of nums[0] is 2, and the last digit of nums[1] is 5. We can confirm that 2 and 5 are coprime, since gcd(2,5) == 1.
When i = 0 and j = 2: the first digit of nums[0] is 2, and the last digit of nums[2] is 1. Indeed, gcd(2,1) == 1.
When i = 1 and j = 2: the first digit of nums[1] is 5, and the last digit of nums[2] is 1. Indeed, gcd(5,1) == 1.
When i = 1 and j = 3: the first digit of nums[1] is 5, and the last digit of nums[3] is 4. Indeed, gcd(5,4) == 1.
When i = 2 and j = 3: the first digit of nums[2] is 1, and the last digit of nums[3] is 4. Indeed, gcd(1,4) == 1.
Thus, we return 5.

Example 2:

Input: nums = [11,21,12]
Output: 2
Explanation: There are 2 beautiful pairs:
When i = 0 and j = 1: the first digit of nums[0] is 1, and the last digit of nums[1] is 1. Indeed, gcd(1,1) == 1.
When i = 0 and j = 2: the first digit of nums[0] is 1, and the last digit of nums[2] is 2. Indeed, gcd(1,2) == 1.
Thus, we return 2.

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def countBeautifulPairs(self, nums: List[int]) -> int:
    cnt = [0] * 10
    ans = 0
    for x in nums:
      for y in range(10):
        if cnt[y] and gcd(x % 10, y) == 1:
          ans += cnt[y]
      cnt[int(str(x)[0])] += 1
    return ans
class Solution {
  public int countBeautifulPairs(int[] nums) {
    int[] cnt = new int[10];
    int ans = 0;
    for (int x : nums) {
      for (int y = 0; y < 10; ++y) {
        if (cnt[y] > 0 && gcd(x % 10, y) == 1) {
          ans += cnt[y];
        }
      }
      while (x > 9) {
        x /= 10;
      }
      ++cnt[x];
    }
    return ans;
  }

  private int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }
}
class Solution {
public:
  int countBeautifulPairs(vector<int>& nums) {
    int cnt[10]{};
    int ans = 0;
    for (int x : nums) {
      for (int y = 0; y < 10; ++y) {
        if (cnt[y] && gcd(x % 10, y) == 1) {
          ans += cnt[y];
        }
      }
      while (x > 9) {
        x /= 10;
      }
      ++cnt[x];
    }
    return ans;
  }
};
func countBeautifulPairs(nums []int) (ans int) {
  cnt := [10]int{}
  for _, x := range nums {
    for y := 0; y < 10; y++ {
      if cnt[y] > 0 && gcd(x%10, y) == 1 {
        ans += cnt[y]
      }
    }
    for x > 9 {
      x /= 10
    }
    cnt[x]++
  }
  return
}

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

function gcd(a: number, b: number): number {
  if (b === 0) {
    return a;
  }
  return gcd(b, a % b);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文