From 4d7d4fa85a4b380c236034f7220d93a14037375d Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 12 Jun 2021 21:09:13 +0800 Subject: [PATCH] feat: update solutions to lc/lcof/lcci problem: Majority Element --- README.md | 1 + README_EN.md | 1 + lcci/17.10.Find Majority Element/README.md | 88 +++++++++++++++--- lcci/17.10.Find Majority Element/README_EN.md | 86 +++++++++++++++--- lcci/17.10.Find Majority Element/Solution.cpp | 15 +++ lcci/17.10.Find Majority Element/Solution.cs | 18 ++++ lcci/17.10.Find Majority Element/Solution.go | 16 ++++ .../17.10.Find Majority Element/Solution.java | 14 +++ lcci/17.10.Find Majority Element/Solution.js | 19 ++-- lcci/17.10.Find Majority Element/Solution.py | 10 ++ .../README.md" | 59 ++++++++---- .../Solution.cs" | 18 ++++ .../Solution.go" | 16 ++++ .../Solution.js" | 24 +++-- .../0100-0199/0169.Majority Element/README.md | 91 ++++++++++++++++++- .../0169.Majority Element/README_EN.md | 89 +++++++++++++++++- .../0169.Majority Element/Solution.cpp | 15 +++ .../0169.Majority Element/Solution.cs | 32 ++----- .../0169.Majority Element/Solution.go | 16 ++++ .../0169.Majority Element/Solution.java | 18 ++-- .../0169.Majority Element/Solution.js | 17 ++++ .../0169.Majority Element/Solution.py | 10 ++ 22 files changed, 573 insertions(+), 100 deletions(-) create mode 100644 lcci/17.10.Find Majority Element/Solution.cpp create mode 100644 lcci/17.10.Find Majority Element/Solution.cs create mode 100644 lcci/17.10.Find Majority Element/Solution.go create mode 100644 lcci/17.10.Find Majority Element/Solution.java create mode 100644 lcci/17.10.Find Majority Element/Solution.py create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.cs" create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.go" create mode 100644 solution/0100-0199/0169.Majority Element/Solution.cpp create mode 100644 solution/0100-0199/0169.Majority Element/Solution.go create mode 100644 solution/0100-0199/0169.Majority Element/Solution.js create mode 100644 solution/0100-0199/0169.Majority Element/Solution.py diff --git a/README.md b/README.md index 9de1f00d59e29..28be4e98f3583 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ ### 数组 +- [多数元素](./solution/0100-0199/0169.Majority%20Element/README.md) - [删除排序数组中的重复项](./solution/0000-0099/0026.Remove%20Duplicates%20from%20Sorted%20Array/README.md) - [删除排序数组中的重复项 II](./solution/0000-0099/0080.Remove%20Duplicates%20from%20Sorted%20Array%20II/README.md) - [移除元素](./solution/0000-0099/0027.Remove%20Element/README.md) diff --git a/README_EN.md b/README_EN.md index 118f17ffbb892..a5decbb67f2cf 100644 --- a/README_EN.md +++ b/README_EN.md @@ -50,6 +50,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF ### Arrays +- [Majority Element](./solution/0100-0199/0169.Majority%20Element/README_EN.md) - [Remove Duplicates from Sorted Array](./solution/0000-0099/0026.Remove%20Duplicates%20from%20Sorted%20Array/README_EN.md) - [Remove Duplicates from Sorted Array II](./solution/0000-0099/0080.Remove%20Duplicates%20from%20Sorted%20Array%20II/README_EN.md) - [Remove Element](./solution/0000-0099/0027.Remove%20Element/README_EN.md) diff --git a/lcci/17.10.Find Majority Element/README.md b/lcci/17.10.Find Majority Element/README.md index 8f7464c2fea1c..fcc62b24e51dc 100644 --- a/lcci/17.10.Find Majority Element/README.md +++ b/lcci/17.10.Find Majority Element/README.md @@ -35,7 +35,7 @@ -摩尔投票法 +摩尔投票法。时间复杂度 O(n),空间复杂度 O(1)。 @@ -44,7 +44,16 @@ ```python - +class Solution: + def majorityElement(self, nums: List[int]) -> int: + cnt = major = 0 + for num in nums: + if cnt == 0: + major = num + cnt = 1 + else: + cnt += (1 if major == num else -1) + return major ``` ### **Java** @@ -52,7 +61,20 @@ ```java - +class Solution { + public int majorityElement(int[] nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} ``` ### **JavaScript** @@ -63,23 +85,63 @@ * @return {number} */ var majorityElement = function(nums) { - let candidate = 0, count = 0; - for (let num of nums) { - if (count == 0) candidate = num; - if (candidate == num) { - count++; + let cnt = 0; + let major = 0; + for (const num of nums) { + if (cnt == 0) { + major = num; + cnt = 1; } else { - count--; + cnt += (major == num ? 1 : -1); } } - let n = 0; - for (let num of nums) { - if (candidate == num) n++; + return major; +}; +``` + +### **C++** + +```cpp +class Solution { +public: + int majorityElement(vector& nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; } - return n > (nums.length / 2) ? candidate : -1; }; ``` +### **C#** + +```cs +public class Solution { + public int MajorityElement(int[] nums) { + int cnt = 0, major = 0; + foreach (int num in nums) + { + if (cnt == 0) + { + major = num; + cnt = 1; + } + else + { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} +``` + ### **...** ``` diff --git a/lcci/17.10.Find Majority Element/README_EN.md b/lcci/17.10.Find Majority Element/README_EN.md index 724bc670dc26e..122c3740009ee 100644 --- a/lcci/17.10.Find Majority Element/README_EN.md +++ b/lcci/17.10.Find Majority Element/README_EN.md @@ -45,13 +45,35 @@ Boyer–Moore majority vote algorithm ### **Python3** ```python - +class Solution: + def majorityElement(self, nums: List[int]) -> int: + cnt = major = 0 + for num in nums: + if cnt == 0: + major = num + cnt = 1 + else: + cnt += (1 if major == num else -1) + return major ``` ### **Java** ```java - +class Solution { + public int majorityElement(int[] nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} ``` ### **JavaScript** @@ -62,23 +84,63 @@ Boyer–Moore majority vote algorithm * @return {number} */ var majorityElement = function(nums) { - let candidate = 0, count = 0; - for (let num of nums) { - if (count == 0) candidate = num; - if (candidate == num) { - count++; + let cnt = 0; + let major = 0; + for (const num of nums) { + if (cnt == 0) { + major = num; + cnt = 1; } else { - count--; + cnt += (major == num ? 1 : -1); } } - let n = 0; - for (let num of nums) { - if (candidate == num) n++; + return major; +}; +``` + +### **C++** + +```cpp +class Solution { +public: + int majorityElement(vector& nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; } - return n > (nums.length / 2) ? candidate : -1; }; ``` +### **C#** + +```cs +public class Solution { + public int MajorityElement(int[] nums) { + int cnt = 0, major = 0; + foreach (int num in nums) + { + if (cnt == 0) + { + major = num; + cnt = 1; + } + else + { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} +``` + ### **...** ``` diff --git a/lcci/17.10.Find Majority Element/Solution.cpp b/lcci/17.10.Find Majority Element/Solution.cpp new file mode 100644 index 0000000000000..478da2ca51d04 --- /dev/null +++ b/lcci/17.10.Find Majority Element/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int majorityElement(vector& nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +}; \ No newline at end of file diff --git a/lcci/17.10.Find Majority Element/Solution.cs b/lcci/17.10.Find Majority Element/Solution.cs new file mode 100644 index 0000000000000..54120887021bb --- /dev/null +++ b/lcci/17.10.Find Majority Element/Solution.cs @@ -0,0 +1,18 @@ +public class Solution { + public int MajorityElement(int[] nums) { + int cnt = 0, major = 0; + foreach (int num in nums) + { + if (cnt == 0) + { + major = num; + cnt = 1; + } + else + { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} \ No newline at end of file diff --git a/lcci/17.10.Find Majority Element/Solution.go b/lcci/17.10.Find Majority Element/Solution.go new file mode 100644 index 0000000000000..60a4b12826bbf --- /dev/null +++ b/lcci/17.10.Find Majority Element/Solution.go @@ -0,0 +1,16 @@ +func majorityElement(nums []int) int { + var cnt, major int + for _, num := range nums { + if cnt == 0 { + major = num + cnt = 1 + } else { + if major == num { + cnt++ + } else { + cnt-- + } + } + } + return major +} \ No newline at end of file diff --git a/lcci/17.10.Find Majority Element/Solution.java b/lcci/17.10.Find Majority Element/Solution.java new file mode 100644 index 0000000000000..6c7bb35b712e2 --- /dev/null +++ b/lcci/17.10.Find Majority Element/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public int majorityElement(int[] nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} \ No newline at end of file diff --git a/lcci/17.10.Find Majority Element/Solution.js b/lcci/17.10.Find Majority Element/Solution.js index 0c6247c1dc9de..c9445926ba5e0 100644 --- a/lcci/17.10.Find Majority Element/Solution.js +++ b/lcci/17.10.Find Majority Element/Solution.js @@ -3,18 +3,15 @@ * @return {number} */ var majorityElement = function(nums) { - let candidate = 0, count = 0; - for (let num of nums) { - if (count == 0) candidate = num; - if (candidate == num) { - count++; + let cnt = 0; + let major = 0; + for (const num of nums) { + if (cnt == 0) { + major = num; + cnt = 1; } else { - count--; + cnt += (major == num ? 1 : -1); } } - let n = 0; - for (let num of nums) { - if (candidate == num) n++; - } - return n > (nums.length / 2) ? candidate : -1; + return major; }; \ No newline at end of file diff --git a/lcci/17.10.Find Majority Element/Solution.py b/lcci/17.10.Find Majority Element/Solution.py new file mode 100644 index 0000000000000..34dfb6fa89e91 --- /dev/null +++ b/lcci/17.10.Find Majority Element/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def majorityElement(self, nums: List[int]) -> int: + cnt = major = 0 + for num in nums: + if cnt == 0: + major = num + cnt = 1 + else: + cnt += (1 if major == num else -1) + return major diff --git "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/README.md" index aaf23edebb4cf..1592509946eb9 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/README.md" @@ -25,6 +25,8 @@ ### **Python3** + + ```python class Solution: def majorityElement(self, nums: List[int]) -> int: @@ -32,7 +34,7 @@ class Solution: for num in nums: if cnt == 0: major = num - cnt += 1 + cnt = 1 else: cnt += (1 if major == num else -1) return major @@ -40,16 +42,18 @@ class Solution: ### **Java** + + ```java class Solution { public int majorityElement(int[] nums) { - int major = 0, cnt = 0; + int cnt = 0, major = 0; for (int num : nums) { if (cnt == 0) { major = num; - ++cnt; + cnt = 1; } else { - cnt += (num == major ? 1 : -1); + cnt += (major == num ? 1 : -1); } } return major; @@ -64,20 +68,18 @@ class Solution { * @param {number[]} nums * @return {number} */ -var majorityElement = function (nums) { - let cnt = 0; - let mode = -1; - for (let num of nums) { - if (!cnt) { - mode = num; - cnt++; - } else { - if (mode === num) cnt++; - else cnt--; +var majorityElement = function(nums) { + let cnt = 0; + let major = 0; + for (const num of nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } } - } - return mode; - // return nums.sort((a,b)=>a-b)[~~(nums.length/2)] + return major; }; ``` @@ -97,6 +99,29 @@ public: }; ``` +### **C#** + +```cs +public class Solution { + public int MajorityElement(int[] nums) { + int cnt = 0, major = 0; + foreach (int num in nums) + { + if (cnt == 0) + { + major = num; + cnt = 1; + } + else + { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} +``` + ### **...** ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.cs" "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.cs" new file mode 100644 index 0000000000000..54120887021bb --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.cs" @@ -0,0 +1,18 @@ +public class Solution { + public int MajorityElement(int[] nums) { + int cnt = 0, major = 0; + foreach (int num in nums) + { + if (cnt == 0) + { + major = num; + cnt = 1; + } + else + { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.go" "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.go" new file mode 100644 index 0000000000000..60a4b12826bbf --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.go" @@ -0,0 +1,16 @@ +func majorityElement(nums []int) int { + var cnt, major int + for _, num := range nums { + if cnt == 0 { + major = num + cnt = 1 + } else { + if major == num { + cnt++ + } else { + cnt-- + } + } + } + return major +} \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.js" index 62cac584d93ca..9a6c4e17ad37f 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.js" +++ "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.js" @@ -2,18 +2,16 @@ * @param {number[]} nums * @return {number} */ -var majorityElement = function (nums) { + var majorityElement = function(nums) { let cnt = 0; - let mode = -1; - for (let num of nums) { - if (!cnt) { - mode = num; - cnt++; - } else { - if (mode === num) cnt++; - else cnt--; - } + let major = 0; + for (const num of nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } } - return mode; - // return nums.sort((a,b)=>a-b)[~~(nums.length/2)] -}; + return major; +}; \ No newline at end of file diff --git a/solution/0100-0199/0169.Majority Element/README.md b/solution/0100-0199/0169.Majority Element/README.md index 1f52e35aee3db..0470e6cd6975a 100644 --- a/solution/0100-0199/0169.Majority Element/README.md +++ b/solution/0100-0199/0169.Majority Element/README.md @@ -38,6 +38,8 @@ +摩尔投票法。时间复杂度 O(n),空间复杂度 O(1)。 + ### **Python3** @@ -45,7 +47,16 @@ ```python - +class Solution: + def majorityElement(self, nums: List[int]) -> int: + cnt = major = 0 + for num in nums: + if cnt == 0: + major = num + cnt = 1 + else: + cnt += (1 if major == num else -1) + return major ``` ### **Java** @@ -53,7 +64,85 @@ ```java +class Solution { + public int majorityElement(int[] nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} +``` + +### **JavaScript** + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var majorityElement = function(nums) { + let cnt = 0; + let major = 0; + for (const num of nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; +}; +``` + +### **C++** + +```cpp +class Solution { +public: + int majorityElement(vector& nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +}; +``` +### **C#** + +```cs +public class Solution { + public int MajorityElement(int[] nums) { + int cnt = 0, major = 0; + foreach (int num in nums) + { + if (cnt == 0) + { + major = num; + cnt = 1; + } + else + { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} ``` ### **...** diff --git a/solution/0100-0199/0169.Majority Element/README_EN.md b/solution/0100-0199/0169.Majority Element/README_EN.md index e682c95377be9..5acf1b12d48f5 100644 --- a/solution/0100-0199/0169.Majority Element/README_EN.md +++ b/solution/0100-0199/0169.Majority Element/README_EN.md @@ -35,13 +35,100 @@ ### **Python3** ```python - +class Solution: + def majorityElement(self, nums: List[int]) -> int: + cnt = major = 0 + for num in nums: + if cnt == 0: + major = num + cnt = 1 + else: + cnt += (1 if major == num else -1) + return major ``` ### **Java** ```java +class Solution { + public int majorityElement(int[] nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} +``` + +### **JavaScript** + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var majorityElement = function(nums) { + let cnt = 0; + let major = 0; + for (const num of nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; +}; +``` + +### **C++** + +```cpp +class Solution { +public: + int majorityElement(vector& nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +}; +``` +### **C#** + +```cs +public class Solution { + public int MajorityElement(int[] nums) { + int cnt = 0, major = 0; + foreach (int num in nums) + { + if (cnt == 0) + { + major = num; + cnt = 1; + } + else + { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +} ``` ### **...** diff --git a/solution/0100-0199/0169.Majority Element/Solution.cpp b/solution/0100-0199/0169.Majority Element/Solution.cpp new file mode 100644 index 0000000000000..478da2ca51d04 --- /dev/null +++ b/solution/0100-0199/0169.Majority Element/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int majorityElement(vector& nums) { + int cnt = 0, major = 0; + for (int num : nums) { + if (cnt == 0) { + major = num; + cnt = 1; + } else { + cnt += (major == num ? 1 : -1); + } + } + return major; + } +}; \ No newline at end of file diff --git a/solution/0100-0199/0169.Majority Element/Solution.cs b/solution/0100-0199/0169.Majority Element/Solution.cs index 59948bdce1888..54120887021bb 100644 --- a/solution/0100-0199/0169.Majority Element/Solution.cs +++ b/solution/0100-0199/0169.Majority Element/Solution.cs @@ -1,30 +1,18 @@ public class Solution { public int MajorityElement(int[] nums) { - return Sort(nums, 0, nums.Length - 1); - } - - private int Sort(int[] nums, int left, int right) - { - if (left == right) return nums[left]; - var targetIndex = nums.Length / 2; - var mid = nums[(left + right) / 2]; - var i = left; - var j = right; - while (i <= j) + int cnt = 0, major = 0; + foreach (int num in nums) { - while (nums[i] < mid) ++i; - while (nums[j] > mid) --j; - if (i <= j) + if (cnt == 0) + { + major = num; + cnt = 1; + } + else { - var temp = nums[i]; - nums[i] = nums[j]; - nums[j] = temp; - ++i; - --j; + cnt += (major == num ? 1 : -1); } } - if (targetIndex <= j) return Sort(nums, left, j); - if (targetIndex >= i) return Sort(nums, i, right); - return mid; + return major; } } \ No newline at end of file diff --git a/solution/0100-0199/0169.Majority Element/Solution.go b/solution/0100-0199/0169.Majority Element/Solution.go new file mode 100644 index 0000000000000..60a4b12826bbf --- /dev/null +++ b/solution/0100-0199/0169.Majority Element/Solution.go @@ -0,0 +1,16 @@ +func majorityElement(nums []int) int { + var cnt, major int + for _, num := range nums { + if cnt == 0 { + major = num + cnt = 1 + } else { + if major == num { + cnt++ + } else { + cnt-- + } + } + } + return major +} \ No newline at end of file diff --git a/solution/0100-0199/0169.Majority Element/Solution.java b/solution/0100-0199/0169.Majority Element/Solution.java index 608cd870f47f6..6c7bb35b712e2 100644 --- a/solution/0100-0199/0169.Majority Element/Solution.java +++ b/solution/0100-0199/0169.Majority Element/Solution.java @@ -1,16 +1,14 @@ class Solution { public int majorityElement(int[] nums) { - int count=1; - int res=nums[0]; - for(int i=1; i int: + cnt = major = 0 + for num in nums: + if cnt == 0: + major = num + cnt = 1 + else: + cnt += (1 if major == num else -1) + return major