TGViewer
Kotlin | LeetCode Kotlin | LeetCode @easy_kotlin_task · 1.7K subscribers
Post #1604 129
Задача: 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 {
fun numIdenticalPairs(nums: IntArray): Int {
var ans = 0
for (i in nums.indices) {
for (j in i + 1 until nums.size) {
if (nums[i] == nums[j]) {
ans++
}
}
}
return ans
}
}


Ставь 👍 и забирай 📚 Базу знаний
More from @easy_kotlin_task
  1. Oct 9, 2026Задача: 1102. Path With Maximum Minimum Value Сложность: medium Дана целочисленная матрица…
  2. Oct 7, 2026Задача: 257. Binary Tree Paths Сложность: easy Дано корневое дерево, верните все пути от к…
  3. Oct 7, 2026🔥 Скрытые вакансии с удаленной работой для Android разработчика, которые нигде больше не…
  4. Oct 6, 2026Задача: 491. Non-decreasing Subsequences Сложность: medium Дан массив целых чисел nums. Ве…
  5. Oct 5, 2026Задача: 635. Design Log Storage System Сложность: medium Вам дается несколько журналов, гд…
  6. Oct 5, 2026Задача: 1209. Remove All Adjacent Duplicates in String II Сложность: medium Вам дана строк…
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 →