TGViewer
Swift | LeetCode Swift | LeetCode @easy_swift_task · 1.3K subscribers
Post #1014 122
Задача: 1512. Number of Good Pairs
Сложность: easy

Дан массив целых чисел nums, верните количество хороших пар.

Пара (i, j) называется хорошей, если nums[i] == nums[j] и i < j.

Пример:
Input: nums = [1,2,3,1,1,3]
Output: 4
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.


👨‍💻 Алгоритм:

1⃣Инициализируйте переменную ans значением 0.

2⃣Итерируйте i от 0 до nums.length:
Итерируйте j от i + 1 до nums.length:
Если nums[i] == nums[j], увеличьте ans на 1.

3⃣Верните ans.

😎 Решение
class Solution {
func numIdenticalPairs(_ nums: [Int]) -> Int {
var ans = 0
for i in 0..<nums.count {
for j in i + 1..<nums.count {
if nums[i] == nums[j] {
ans += 1
}
}
}
return ans
}
}


Ставь 👍 и забирай 📚 Базу знаний
More from @easy_swift_task
  1. Oct 13, 2026Задача: 786. K-th Smallest Prime Fraction Сложность: medium Вам дан отсортированный массив…
  2. Oct 11, 2026Задача: 1325. Delete Leaves With a Given Value Сложность: medium Дано корневое дерево root…
  3. Oct 7, 2026🔥 Скрытые вакансии с удаленной работой для iOS разработчика, которые нигде больше не публ…
  4. Oct 4, 2026Задача: 523. Continuous Subarray Sum Сложность: medium Дан целочисленный массив nums и цел…
  5. Oct 4, 2026Задача: 1329. Sort the Matrix Diagonally Сложность: medium Диагональ матрицы — это диагона…
  6. Oct 3, 2026Задача: 200. Number of Islands Сложность: medium Дана двумерная бинарная сетка размером m…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →