Skip to content

Commit c20f82a

Browse files
authored
chore: add new lc problems (#1068)
1 parent c4eca26 commit c20f82a

File tree

22 files changed

+1025
-0
lines changed

22 files changed

+1025
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# [2748. 美丽下标对的数目](https://leetcode.cn/problems/number-of-beautiful-pairs)
2+
3+
[English Version](/solution/2700-2799/2748.Number%20of%20Beautiful%20Pairs/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。如果下标对 <code>i</code>、<code>j</code> 满足 <code>0 ≤ i &lt; j &lt; nums.length</code> ,如果&nbsp;<code>nums[i]</code> 的 <strong>第一个数字</strong> 和 <code>nums[j]</code> 的 <strong>最后一个数字</strong> <strong>互质</strong> ,则认为 <code>nums[i]</code> 和 <code>nums[j]</code> 是一组 <strong>美丽下标对</strong> 。</p>
10+
11+
<p>返回 <code>nums</code> 中 <strong>美丽下标对</strong> 的总数目。</p>
12+
13+
<p>对于两个整数 <code>x</code> 和 <code>y</code> ,如果不存在大于 1 的整数可以整除它们,则认为 <code>x</code> 和 <code>y</code> <strong>互质</strong> 。换而言之,如果 <code>gcd(x, y) == 1</code> ,则认为 <code>x</code> 和 <code>y</code> 互质,其中 <code>gcd(x, y)</code> 是 <code>x</code> 和 <code>k</code> <strong>最大公因数</strong> 。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre>
20+
<strong>输入:</strong>nums = [2,5,1,4]
21+
<strong>输出:</strong>5
22+
<strong>解释:</strong>nums 中共有 5 组美丽下标对:
23+
i = 0 和 j = 1 :nums[0] 的第一个数字是 2 ,nums[1] 的最后一个数字是 5 。2 和 5 互质,因此 gcd(2,5) == 1 。
24+
i = 0 和 j = 2 :nums[0] 的第一个数字是 2 ,nums[1] 的最后一个数字是 1 。2 和 5 互质,因此 gcd(2,1) == 1 。
25+
i = 1 和 j = 2 :nums[0] 的第一个数字是 5 ,nums[1] 的最后一个数字是 1 。2 和 5 互质,因此 gcd(5,1) == 1 。
26+
i = 1 和 j = 3 :nums[0] 的第一个数字是 5 ,nums[1] 的最后一个数字是 4 。2 和 5 互质,因此 gcd(5,4) == 1 。
27+
i = 2 和 j = 3 :nums[0] 的第一个数字是 1 ,nums[1] 的最后一个数字是 4 。2 和 5 互质,因此 gcd(1,4) == 1 。
28+
因此,返回 5 。
29+
</pre>
30+
31+
<p><strong>示例 2:</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>nums = [11,21,12]
35+
<strong>输出:</strong>2
36+
<strong>解释:</strong>共有 2 组美丽下标对:
37+
i = 0 和 j = 1 :nums[0] 的第一个数字是 1 ,nums[1] 的最后一个数字是 1 。gcd(1,1) == 1 。
38+
i = 0 和 j = 2 :nums[0] 的第一个数字是 1 ,nums[1] 的最后一个数字是 2 。gcd(1,2) == 1 。
39+
因此,返回 2 。</pre>
40+
41+
<p>&nbsp;</p>
42+
43+
<p><strong>提示:</strong></p>
44+
45+
<ul>
46+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
47+
<li><code>1 &lt;= nums[i] &lt;= 9999</code></li>
48+
<li><code>nums[i] % 10 != 0</code></li>
49+
</ul>
50+
51+
## 解法
52+
53+
<!-- 这里可写通用的实现逻辑 -->
54+
55+
<!-- tabs:start -->
56+
57+
### **Python3**
58+
59+
<!-- 这里可写当前语言的特殊实现逻辑 -->
60+
61+
```python
62+
class Solution:
63+
def countBeautifulPairs(self, nums: List[int]) -> int:
64+
cnt = [0] * 10
65+
ans = 0
66+
for x in nums:
67+
for y in range(10):
68+
if cnt[y] and gcd(x % 10, y) == 1:
69+
ans += cnt[y]
70+
cnt[int(str(x)[0])] += 1
71+
return ans
72+
```
73+
74+
### **Java**
75+
76+
<!-- 这里可写当前语言的特殊实现逻辑 -->
77+
78+
```java
79+
class Solution {
80+
public int countBeautifulPairs(int[] nums) {
81+
int[] cnt = new int[10];
82+
int ans = 0;
83+
for (int x : nums) {
84+
for (int y = 0; y < 10; ++y) {
85+
if (cnt[y] > 0 && gcd(x % 10, y) == 1) {
86+
ans += cnt[y];
87+
}
88+
}
89+
while (x > 9) {
90+
x /= 10;
91+
}
92+
++cnt[x];
93+
}
94+
return ans;
95+
}
96+
97+
private int gcd(int a, int b) {
98+
return b == 0 ? a : gcd(b, a % b);
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int countBeautifulPairs(vector<int>& nums) {
109+
int cnt[10]{};
110+
int ans = 0;
111+
for (int x : nums) {
112+
for (int y = 0; y < 10; ++y) {
113+
if (cnt[y] && gcd(x % 10, y) == 1) {
114+
ans += cnt[y];
115+
}
116+
}
117+
while (x > 9) {
118+
x /= 10;
119+
}
120+
++cnt[x];
121+
}
122+
return ans;
123+
}
124+
};
125+
```
126+
127+
### **Go**
128+
129+
```go
130+
func countBeautifulPairs(nums []int) (ans int) {
131+
cnt := [10]int{}
132+
for _, x := range nums {
133+
for y := 0; y < 10; y++ {
134+
if cnt[y] > 0 && gcd(x%10, y) == 1 {
135+
ans += cnt[y]
136+
}
137+
}
138+
for x > 9 {
139+
x /= 10
140+
}
141+
cnt[x]++
142+
}
143+
return
144+
}
145+
146+
func gcd(a, b int) int {
147+
if b == 0 {
148+
return a
149+
}
150+
return gcd(b, a%b)
151+
}
152+
```
153+
154+
### **TypeScript**
155+
156+
```ts
157+
function countBeautifulPairs(nums: number[]): number {
158+
const cnt: number[] = Array(10).fill(0);
159+
let ans = 0;
160+
for (let x of nums) {
161+
for (let y = 0; y < 10; ++y) {
162+
if (cnt[y] > 0 && gcd(x % 10, y) === 1) {
163+
ans += cnt[y];
164+
}
165+
}
166+
while (x > 9) {
167+
x = Math.floor(x / 10);
168+
}
169+
++cnt[x];
170+
}
171+
return ans;
172+
}
173+
174+
function gcd(a: number, b: number): number {
175+
if (b === 0) {
176+
return a;
177+
}
178+
return gcd(b, a % b);
179+
}
180+
```
181+
182+
### **...**
183+
184+
```
185+
186+
```
187+
188+
<!-- tabs:end -->
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# [2748. Number of Beautiful Pairs](https://leetcode.com/problems/number-of-beautiful-pairs)
2+
3+
[中文文档](/solution/2700-2799/2748.Number%20of%20Beautiful%20Pairs/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>. A pair of indices <code>i</code>, <code>j</code> where <code>0 &le; i &lt; j &lt; nums.length</code> is called beautiful if the <strong>first digit</strong> of <code>nums[i]</code> and the <strong>last digit</strong> of <code>nums[j]</code> are <strong>coprime</strong>.</p>
8+
9+
<p>Return <em>the total number of beautiful pairs in </em><code>nums</code>.</p>
10+
11+
<p>Two integers <code>x</code> and <code>y</code> are <strong>coprime</strong> if there is no integer greater than 1 that divides both of them. In other words, <code>x</code> and <code>y</code> are coprime if <code>gcd(x, y) == 1</code>, where <code>gcd(x, y)</code> is the <strong>greatest common divisor</strong> of <code>x</code> and <code>y</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [2,5,1,4]
18+
<strong>Output:</strong> 5
19+
<strong>Explanation:</strong> There are 5 beautiful pairs in nums:
20+
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.
21+
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.
22+
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.
23+
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.
24+
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.
25+
Thus, we return 5.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> nums = [11,21,12]
32+
<strong>Output:</strong> 2
33+
<strong>Explanation:</strong> There are 2 beautiful pairs:
34+
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.
35+
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.
36+
Thus, we return 2.
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
44+
<li><code>1 &lt;= nums[i] &lt;= 9999</code></li>
45+
<li><code>nums[i] % 10 != 0</code></li>
46+
</ul>
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
```python
55+
class Solution:
56+
def countBeautifulPairs(self, nums: List[int]) -> int:
57+
cnt = [0] * 10
58+
ans = 0
59+
for x in nums:
60+
for y in range(10):
61+
if cnt[y] and gcd(x % 10, y) == 1:
62+
ans += cnt[y]
63+
cnt[int(str(x)[0])] += 1
64+
return ans
65+
```
66+
67+
### **Java**
68+
69+
```java
70+
class Solution {
71+
public int countBeautifulPairs(int[] nums) {
72+
int[] cnt = new int[10];
73+
int ans = 0;
74+
for (int x : nums) {
75+
for (int y = 0; y < 10; ++y) {
76+
if (cnt[y] > 0 && gcd(x % 10, y) == 1) {
77+
ans += cnt[y];
78+
}
79+
}
80+
while (x > 9) {
81+
x /= 10;
82+
}
83+
++cnt[x];
84+
}
85+
return ans;
86+
}
87+
88+
private int gcd(int a, int b) {
89+
return b == 0 ? a : gcd(b, a % b);
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int countBeautifulPairs(vector<int>& nums) {
100+
int cnt[10]{};
101+
int ans = 0;
102+
for (int x : nums) {
103+
for (int y = 0; y < 10; ++y) {
104+
if (cnt[y] && gcd(x % 10, y) == 1) {
105+
ans += cnt[y];
106+
}
107+
}
108+
while (x > 9) {
109+
x /= 10;
110+
}
111+
++cnt[x];
112+
}
113+
return ans;
114+
}
115+
};
116+
```
117+
118+
### **Go**
119+
120+
```go
121+
func countBeautifulPairs(nums []int) (ans int) {
122+
cnt := [10]int{}
123+
for _, x := range nums {
124+
for y := 0; y < 10; y++ {
125+
if cnt[y] > 0 && gcd(x%10, y) == 1 {
126+
ans += cnt[y]
127+
}
128+
}
129+
for x > 9 {
130+
x /= 10
131+
}
132+
cnt[x]++
133+
}
134+
return
135+
}
136+
137+
func gcd(a, b int) int {
138+
if b == 0 {
139+
return a
140+
}
141+
return gcd(b, a%b)
142+
}
143+
```
144+
145+
### **TypeScript**
146+
147+
```ts
148+
function countBeautifulPairs(nums: number[]): number {
149+
const cnt: number[] = Array(10).fill(0);
150+
let ans = 0;
151+
for (let x of nums) {
152+
for (let y = 0; y < 10; ++y) {
153+
if (cnt[y] > 0 && gcd(x % 10, y) === 1) {
154+
ans += cnt[y];
155+
}
156+
}
157+
while (x > 9) {
158+
x = Math.floor(x / 10);
159+
}
160+
++cnt[x];
161+
}
162+
return ans;
163+
}
164+
165+
function gcd(a: number, b: number): number {
166+
if (b === 0) {
167+
return a;
168+
}
169+
return gcd(b, a % b);
170+
}
171+
```
172+
173+
### **...**
174+
175+
```
176+
177+
```
178+
179+
<!-- tabs:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int countBeautifulPairs(vector<int>& nums) {
4+
int cnt[10]{};
5+
int ans = 0;
6+
for (int x : nums) {
7+
for (int y = 0; y < 10; ++y) {
8+
if (cnt[y] && gcd(x % 10, y) == 1) {
9+
ans += cnt[y];
10+
}
11+
}
12+
while (x > 9) {
13+
x /= 10;
14+
}
15+
++cnt[x];
16+
}
17+
return ans;
18+
}
19+
};

0 commit comments

Comments
 (0)