首页 > 其他分享 >LeetCode 1748. Sum of Unique Elements

LeetCode 1748. Sum of Unique Elements

时间:2024-06-04 11:55:36浏览次数:12  
标签:Elements num Unique nums int Sum elements unique sum

原题链接在这里:https://leetcode.com/problems/sum-of-unique-elements/description/

题目:

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.

Example 1:

Input: nums = [1,2,3,2]
Output: 4
Explanation: The unique elements are [1,3], and the sum is 4.

Example 2:

Input: nums = [1,1,1,1,1]
Output: 0
Explanation: There are no unique elements, and the sum is 0.

Example 3:

Input: nums = [1,2,3,4,5]
Output: 15
Explanation: The unique elements are [1,2,3,4,5], and the sum is 15.

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

题解:

Have a map to maintain num to its frequency.

Iterate the map again to pick the entry with frequency as 1 and accumulate the key.

Time Complexity: O(n). n = nums.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int sumOfUnique(int[] nums) {
 3         HashMap<Integer, Integer> hm = new HashMap<>();
 4         for(int num : nums){
 5             hm.put(num, hm.getOrDefault(num, 0) + 1);
 6         }
 7         
 8         int res = 0;
 9         for(Map.Entry<Integer, Integer> entry : hm.entrySet()){
10             if(entry.getValue() == 1){
11                 res += entry.getKey();
12             }
13         }
14         
15         return res;
16     }
17 }

 

标签:Elements,num,Unique,nums,int,Sum,elements,unique,sum
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18230517

相关文章

  • [ABC238E] Range Sums
    原题链接题解把这里的数字看成间隔,不要看成点假设已知能和\(l\)组成区间的端点集合\(A\)和以\(r\)组成区间的端点集合\(B\),这时候加入一个以\(l,r\)为左右端点的区间,那么在加入区间\(l,r\)之后,这两个集合可以合并code#include<bits/stdc++.h>usingnamespacestd......
  • 文献阅读——Single Clause Assumption without Activation Literals to Speed-up IC
    SingleClauseAssumptionwithoutActivationLiteralstoSpeed-upIC3 @inproceedings{DBLP:conf/fmcad/FroleyksB21,author={NilsFroleyksandArminBiere},title={SingleClauseAssumptionwithoutActivationLitera......
  • LeetCode 1305. All Elements in Two Binary Search Trees
    原题链接在这里:https://leetcode.com/problems/all-elements-in-two-binary-search-trees/description/题目:Giventwobinarysearchtrees root1 and root2,return alistcontainingalltheintegersfrombothtreessortedin ascending order.Example1:Input:......
  • Linux低功耗Suspend/Resume梳理(基于STM32MP1)
    基于STM32MP1简单梳理Linuxsuspend/resume涉及到的内容:触发Suspend流程,以及唤醒手段和后续resume流程。Linuxkernel中Suspend/Resume流程。TFA中冷启动、热启动、SMC处理、PSCI实现等等。其他低功耗相关:poweroff、reboot、fiq处理。PowerDomainTree介绍;PSCI移植指导等。......
  • mysql中key 、primary key 、unique key 与index区别
    索引被用来快速找出在一个列上用一特定值的行。没有索引,MySQL不得不首先以第一条记录开始并然后读完整个表直到它找出相关的行。表越大,花费时间越多。如果表对于查询的列有一个索引,MySQL能快速到达一个位置去搜寻到数据文件的中间,没有必要考虑所有数据。如果一个表有1000行,这比......
  • css06 CSS Pseudo-elements
    https://www.w3schools.com/css/css_pseudo_elements.aspWhatarePseudo-Elements?ACSSpseudo-elementisusedtostylespecifiedpartsofanelement.Forexample,itcanbeusedto:Stylethefirstletter,orline,ofanelementInsertcontentbefore,or......
  • antdv: Each record in table should have a unique `key` prop,or set `rowKey` to a
    在使用ant-designvue框架的时候,表格组件里面会碰到 Eachrecordintableshouldhaveaunique key prop,orset rowKey toanuniqueprimarykey这样的报错,具体见下图 原因分析:我看了一下官网,以及搜索了很多答案,最终原因是:在Table中,dataSource和columns里的数据......
  • SUMER UI3.0组件库,基于Uni-app前端框架!一端开发,多端运行!本组件库可快速二次开发各种类
    sumer-ui介绍基于uView微信小程序UI组件库,兼容vue3。本插件是SUMER组件库,只提供组件库源码下载(不包含模板源码),本组件库可快速二次开发各种类别各行业模板,包括:商城、视频、直播、聊天、支付、新闻、社区、地图、导航、出行、社区、博客、新闻、游戏、影视、订票、广告等,......
  • CF1973F Maximum GCD Sum Queries 题解
    题目链接点击打开链接题目解法首先想到枚举两个数列的$\gcd$,求最小代价两个数列的\(\gcd\)应该分别是\(a_1,b_1\)的因数或\(b_1,a_1\)的因数这样就把枚举范围缩小到了\(d(a_1)\timesd(b_1)\),这求最小代价需要\(O(n)\),不够快假设枚举的\(\gcd\)分别为\(x,y\)......
  • 96-Unique Binary Search Trees 二叉搜索树的数量
    问题描述链接:https://leetcode.com/problems/unique-binary-search-trees/description/Givenaninteger n,return thenumberofstructurallyunique BST's(binarysearchtrees)whichhasexactly n nodesofuniquevaluesfrom 1 to n.解释:给定一个整数n,求1~n......