Skip to content

Commit ff61144

Browse files
authored
feat: add php solution to lc problem: No.0169 (doocs#955)
1 parent 3ee3279 commit ff61144

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

solution/0100-0199/0169.Majority Element/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ impl Solution {
199199
}
200200
```
201201

202+
### **PHP**
203+
204+
```php
205+
class Solution {
206+
/**
207+
* @param Integer[] $nums
208+
* @return Integer
209+
*/
210+
function majorityElement($nums) {
211+
$major = 0;
212+
$count = 0;
213+
for ($i = 0; $i < count($nums); $i++) {
214+
if ($count == 0) $major = $nums[$i];
215+
if ($major == $nums[$i]) $count++;
216+
else $count--;
217+
}
218+
return $major;
219+
}
220+
}
221+
```
222+
202223
### **...**
203224

204225
```

solution/0100-0199/0169.Majority Element/README_EN.md

+21
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,27 @@ impl Solution {
169169
}
170170
```
171171

172+
### **PHP**
173+
174+
```php
175+
class Solution {
176+
/**
177+
* @param Integer[] $nums
178+
* @return Integer
179+
*/
180+
function majorityElement($nums) {
181+
$major = 0;
182+
$count = 0;
183+
for ($i = 0; $i < count($nums); $i++) {
184+
if ($count == 0) $major = $nums[$i];
185+
if ($major == $nums[$i]) $count++;
186+
else $count--;
187+
}
188+
return $major;
189+
}
190+
}
191+
```
192+
172193
### **...**
173194

174195
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
/**
3+
* @param Integer[] $nums
4+
* @return Integer
5+
*/
6+
function majorityElement($nums) {
7+
$major = 0;
8+
$count = 0;
9+
for ($i = 0; $i < count($nums); $i++) {
10+
if ($count == 0) $major = $nums[$i];
11+
if ($major == $nums[$i]) $count++;
12+
else $count--;
13+
}
14+
return $major;
15+
}
16+
}

0 commit comments

Comments
 (0)