From 55e185b5e651860f0363cea4a5fdfdbe72ba1310 Mon Sep 17 00:00:00 2001 From: jerryfishcode <91447694+jerryfishcode@users.noreply.github.com> Date: Mon, 27 Sep 2021 13:06:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=201207.=E7=8B=AC=E4=B8=80?= =?UTF-8?q?=E6=97=A0=E4=BA=8C=E7=9A=84=E5=87=BA=E7=8E=B0=E6=AC=A1=E6=95=B0?= =?UTF-8?q?=20JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1207.独一无二的出现次数.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md index 027c9f5a..f18af16c 100644 --- a/problems/1207.独一无二的出现次数.md +++ b/problems/1207.独一无二的出现次数.md @@ -118,7 +118,23 @@ class Solution: Go: JavaScript: - +``` javascript +var uniqueOccurrences = function(arr) { + const count = new Array(2002).fill(0);// -1000 <= arr[i] <= 1000 + for(let i = 0; i < arr.length; i++){ + count[arr[i] + 1000]++;// 防止负数作为下标 + } + // 标记相同频率是否重复出现 + const fre = new Array(1002).fill(false);// 1 <= arr.length <= 1000 + for(let i = 0; i <= 2000; i++){ + if(count[i] > 0){//有i出现过 + if(fre[count[i]] === false) fre[count[i]] = true;//之前未出现过,标记为出现 + else return false;//之前就出现了,重复出现 + } + } + return true; +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)