Skip to content

Commit 335d058

Browse files
authored
Added tasks 169-226
1 parent 5622877 commit 335d058

File tree

11 files changed

+753
-0
lines changed

11 files changed

+753
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 169\. Majority Element
5+
6+
Easy
7+
8+
Given an array `nums` of size `n`, return _the majority element_.
9+
10+
The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [3,2,3]
15+
16+
**Output:** 3
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [2,2,1,1,1,2,2]
21+
22+
**Output:** 2
23+
24+
**Constraints:**
25+
26+
* `n == nums.length`
27+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
28+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
29+
30+
**Follow-up:** Could you solve the problem in linear time and in `O(1)` space?
31+
32+
## Solution
33+
34+
```php
35+
class Solution {
36+
/**
37+
* @param Integer[] $nums
38+
* @return Integer
39+
*/
40+
public function majorityElement($nums) {
41+
$search = array_count_values($nums);
42+
$count = count($nums) / 2;
43+
foreach ($search as $key => $value) {
44+
if ($value > $count) {
45+
return $key;
46+
}
47+
}
48+
}
49+
}
50+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 189\. Rotate Array
5+
6+
Medium
7+
8+
Given an array, rotate the array to the right by `k` steps, where `k` is non-negative.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,2,3,4,5,6,7], k = 3
13+
14+
**Output:** [5,6,7,1,2,3,4]
15+
16+
**Explanation:**
17+
18+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
19+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
20+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [-1,-100,3,99], k = 2
25+
26+
**Output:** [3,99,-1,-100]
27+
28+
**Explanation:**
29+
30+
rotate 1 steps to the right: [99,-1,-100,3]
31+
rotate 2 steps to the right: [3,99,-1,-100]
32+
33+
**Constraints:**
34+
35+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
36+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
37+
* <code>0 <= k <= 10<sup>5</sup></code>
38+
39+
**Follow up:**
40+
41+
* Try to come up with as many solutions as you can. There are at least **three** different ways to solve this problem.
42+
* Could you do it in-place with `O(1)` extra space?
43+
44+
## Solution
45+
46+
```php
47+
class Solution {
48+
private function reverse(&$nums, $l, $r) {
49+
while ($l <= $r) {
50+
$temp = $nums[$l];
51+
$nums[$l] = $nums[$r];
52+
$nums[$r] = $temp;
53+
$l++;
54+
$r--;
55+
}
56+
}
57+
58+
/**
59+
* @param Integer[] $nums
60+
* @param Integer $k
61+
* @return NULL
62+
*/
63+
public function rotate(&$nums, $k) {
64+
$n = count($nums);
65+
$t = $n - ($k % $n);
66+
$this->reverse($nums, 0, $t - 1);
67+
$this->reverse($nums, $t, $n - 1);
68+
$this->reverse($nums, 0, $n - 1);
69+
}
70+
}
71+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 198\. House Robber
5+
6+
Medium
7+
8+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.
9+
10+
Given an integer array `nums` representing the amount of money of each house, return _the maximum amount of money you can rob tonight **without alerting the police**_.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,2,3,1]
15+
16+
**Output:** 4
17+
18+
**Explanation:**
19+
20+
Rob house 1 (money = 1) and then rob house 3 (money = 3).
21+
Total amount you can rob = 1 + 3 = 4.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [2,7,9,3,1]
26+
27+
**Output:** 12
28+
29+
**Explanation:**
30+
31+
Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
32+
Total amount you can rob = 2 + 9 + 1 = 12.
33+
34+
**Constraints:**
35+
36+
* `1 <= nums.length <= 100`
37+
* `0 <= nums[i] <= 400`
38+
39+
## Solution
40+
41+
```php
42+
class Solution {
43+
/**
44+
* @param Integer[] $nums
45+
* @return Integer
46+
*/
47+
public function rob($nums) {
48+
$n = count($nums);
49+
if ($n == 0) {
50+
return 0;
51+
}
52+
if ($n == 1) {
53+
return $nums[0];
54+
}
55+
if ($n == 2) {
56+
return max($nums[0], $nums[1]);
57+
}
58+
$profit = array();
59+
$profit[0] = $nums[0];
60+
$profit[1] = max($nums[1], $nums[0]);
61+
for ($i = 2; $i < $n; $i++) {
62+
$profit[$i] = max($profit[$i - 1], $nums[$i] + $profit[$i - 2]);
63+
}
64+
return $profit[$n - 1];
65+
}
66+
}
67+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 200\. Number of Islands
5+
6+
Medium
7+
8+
Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
9+
10+
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
11+
12+
**Example 1:**
13+
14+
**Input:**
15+
16+
grid = [
17+
["1","1","1","1","0"],
18+
["1","1","0","1","0"],
19+
["1","1","0","0","0"],
20+
["0","0","0","0","0"]
21+
]
22+
23+
**Output:** 1
24+
25+
**Example 2:**
26+
27+
**Input:**
28+
29+
grid = [
30+
["1","1","0","0","0"],
31+
["1","1","0","0","0"],
32+
["0","0","1","0","0"],
33+
["0","0","0","1","1"]
34+
]
35+
36+
**Output:** 3
37+
38+
**Constraints:**
39+
40+
* `m == grid.length`
41+
* `n == grid[i].length`
42+
* `1 <= m, n <= 300`
43+
* `grid[i][j]` is `'0'` or `'1'`.
44+
45+
## Solution
46+
47+
```php
48+
class Solution {
49+
/**
50+
* @param String[][] $grid
51+
* @return Integer
52+
*/
53+
public function numIslands($grid) {
54+
$islands = 0;
55+
if ($grid != null && !empty($grid) && count($grid[0]) != 0) {
56+
for ($i = 0; $i < count($grid); $i++) {
57+
for ($j = 0; $j < count($grid[0]); $j++) {
58+
if ($grid[$i][$j] == '1') {
59+
$this->dfs($grid, $i, $j);
60+
$islands++;
61+
}
62+
}
63+
}
64+
}
65+
return $islands;
66+
}
67+
68+
private function dfs(&$grid, $x, $y) {
69+
if ($x < 0 || count($grid) <= $x || $y < 0 || count($grid[0]) <= $y || $grid[$x][$y] != '1') {
70+
return;
71+
}
72+
$grid[$x][$y] = 'x';
73+
$this->dfs($grid, $x + 1, $y);
74+
$this->dfs($grid, $x - 1, $y);
75+
$this->dfs($grid, $x, $y + 1);
76+
$this->dfs($grid, $x, $y - 1);
77+
}
78+
}
79+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 206\. Reverse Linked List
5+
6+
Easy
7+
8+
Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)
13+
14+
**Input:** head = [1,2,3,4,5]
15+
16+
**Output:** [5,4,3,2,1]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)
21+
22+
**Input:** head = [1,2]
23+
24+
**Output:** [2,1]
25+
26+
**Example 3:**
27+
28+
**Input:** head = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the list is the range `[0, 5000]`.
35+
* `-5000 <= Node.val <= 5000`
36+
37+
**Follow up:** A linked list can be reversed either iteratively or recursively. Could you implement both?
38+
39+
## Solution
40+
41+
```php
42+
/**
43+
* Definition for a singly-linked list.
44+
* class ListNode {
45+
* public $val = 0;
46+
* public $next = null;
47+
* function __construct($val = 0, $next = null) {
48+
* $this->val = $val;
49+
* $this->next = $next;
50+
* }
51+
* }
52+
*/
53+
class Solution {
54+
/**
55+
* @param ListNode $head
56+
* @return ListNode
57+
*/
58+
public function reverseList($head) {
59+
$prev = null;
60+
$curr = $head;
61+
while ($curr != null) {
62+
$next = $curr->next;
63+
$curr->next = $prev;
64+
$prev = $curr;
65+
$curr = $next;
66+
}
67+
return $prev;
68+
}
69+
}
70+
```

0 commit comments

Comments
 (0)