From 9ea5c0a46eef3e20ad23971053d9d53452ae6157 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Thu, 11 Jun 2020 09:11:11 +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 | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/数组相关.md b/blogs/Algorithm/剑指 Offer/数组相关.md index 7883f54..1dc8f9f 100644 --- a/blogs/Algorithm/剑指 Offer/数组相关.md +++ b/blogs/Algorithm/剑指 Offer/数组相关.md @@ -96,3 +96,31 @@ class Solution { } ``` +[39. 数组中出现次数超过一半的数字](https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/) + +```java +class Solution { + + public int majorityElement(int[] nums) { + Arrays.sort(nums); + return nums[nums.length / 2]; + } +} +``` + +```java +class Solution { + + public int majorityElement(int[] nums) { + int count = 0, result = 0; + for (int num : nums) { + if (count == 0) { + result = num; + } + count += (result == num) ? 1 : -1; + } + return result; + } +} +``` +