LeetCode 1748 Sum of Unique Elements (Python)

Posted by 小明MaxMing on November 1, 2024

题目

You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array.

Return the sum of all the unique elements of nums.

解题思路

使用Counter求出每个数出现的次数

代码

class Solution:
    def sumOfUnique(self, nums: List[int]) -> int:
        ct = Counter(nums)
        res = 0
        for key, val in ct.items():
            if val == 1:
                res += key
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅