Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
leetcode: finished #229
Browse files Browse the repository at this point in the history
  • Loading branch information
xqm32 committed Sep 17, 2023
1 parent f6f1cd4 commit f5be8af
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions content/leetcode/2023/9.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,60 @@ title: 2023.9
draft: false
---

# 2023.9.17

```python
#
# @lc app=leetcode.cn id=229 lang=python3
#
# [229] 多数元素 II
#

# @lc code=start
from typing import List


class Solution:
def majorityElement(self, nums: List[int]) -> List[int]:
cand1, cand2 = 0, 0
count1, count2 = 0, 0

for num in nums:
if num == cand1:
count1 += 1
continue
if num == cand2:
count2 += 1
continue
if count1 == 0:
cand1 = num
count1 += 1
continue
if count2 == 0:
cand2 = num
count2 += 1
continue
count1 -= 1
count2 -= 1

count1, count2 = 0, 0
for num in nums:
if num == cand1:
count1 += 1
elif num == cand2:
count2 += 1

res = []
if count1 > len(nums) // 3:
res.append(cand1)
if count2 > len(nums) // 3:
res.append(cand2)
return res


# @lc code=end
```

# 2023.9.16

```python
Expand Down

0 comments on commit f5be8af

Please sign in to comment.