Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion problems/169.majority-element.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ https://leetcode-cn.com/problems/majority-element/

## 代码

- 语言支持:JS,Python, CPP
- 语言支持:JS,Python, CPP,Java

Javascript Code:

Expand Down Expand Up @@ -112,6 +112,26 @@ public:
};
```

Java Code:

```java
class Solution {
public int majorityElement(int[] nums) {
int count = 0;
Integer candidate = null;

for (int num : nums) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
}

return candidate;
}
}
```

**复杂度分析**

- 时间复杂度:$O(N)$,其中 N 为数组长度
Expand Down