From f44f6009cce50b0ea9a867cc782c0539657a9a7e Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Mon, 15 Jun 2020 08:20:41 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=95=B0=E7=BB=84=E7=9B=B8=E5=85=B3.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blogs/Algorithm/剑指 Offer/数组相关.md | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/数组相关.md b/blogs/Algorithm/剑指 Offer/数组相关.md index 0ec813a..a8763e6 100644 --- a/blogs/Algorithm/剑指 Offer/数组相关.md +++ b/blogs/Algorithm/剑指 Offer/数组相关.md @@ -232,3 +232,56 @@ class Solution { } ``` +[56 - II. 数组中数字出现的次数 II](https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/) + +```java +class Solution { + public int singleNumber(int[] nums) { + Arrays.sort(nums); + for (int i = 0; i < nums.length - 2; i += 3) { + if (nums[i] != nums[i + 2]) { + return nums[i]; + } + } + return nums[nums.length - 1]; + } +} +``` + +```java +class Solution { + public int singleNumber(int[] nums) { + Map map = new HashMap<>(); + for (int i : nums) { + int count = map.getOrDefault(i, 0); + map.put(i, count + 1); + } + for (Integer i : map.keySet()) { + if (map.get(i) == 1) { + return i; + } + } + return -1; + } +} +``` + +```java +class Solution { + public int singleNumber(int[] nums) { + int[] count = new int[32]; + for (int i : nums) { + for (int j = 0; j < 32; j++) { + count[j] += i & 1; + i = i >>> 1; + } + } + int res = 0, flag = 3; + for (int i = 0; i < 32; i++) { + res += (1 << i) * (count[i] % flag); + } + return res; + } +} +``` +