Skip to content

Commit 63cea37

Browse files
feat: add php solution to lc problems: No.0011,0012 (#2360)
1 parent 2e8ca32 commit 63cea37

File tree

6 files changed

+195
-0
lines changed

6 files changed

+195
-0
lines changed

solution/0000-0099/0011.Container With Most Water/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,35 @@ public class Solution {
206206
}
207207
```
208208

209+
```php
210+
class Solution {
211+
/**
212+
* @param int[] $height
213+
* @return int
214+
*/
215+
216+
function maxArea($height) {
217+
$left = 0;
218+
$right = count($height) - 1;
219+
$maxArea = 0;
220+
221+
while ($left < $right) {
222+
$area = min($height[$left], $height[$right]) * ($right - $left);
223+
224+
$maxArea = max($maxArea, $area);
225+
226+
if ($height[$left] < $height[$right]) {
227+
$left++;
228+
} else {
229+
$right--;
230+
}
231+
}
232+
233+
return $maxArea;
234+
}
235+
}
236+
```
237+
209238
<!-- tabs:end -->
210239

211240
<!-- end -->

solution/0000-0099/0011.Container With Most Water/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,35 @@ public class Solution {
201201
}
202202
```
203203

204+
```php
205+
class Solution {
206+
/**
207+
* @param int[] $height
208+
* @return int
209+
*/
210+
211+
function maxArea($height) {
212+
$left = 0;
213+
$right = count($height) - 1;
214+
$maxArea = 0;
215+
216+
while ($left < $right) {
217+
$area = min($height[$left], $height[$right]) * ($right - $left);
218+
219+
$maxArea = max($maxArea, $area);
220+
221+
if ($height[$left] < $height[$right]) {
222+
$left++;
223+
} else {
224+
$right--;
225+
}
226+
}
227+
228+
return $maxArea;
229+
}
230+
}
231+
```
232+
204233
<!-- tabs:end -->
205234

206235
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
/**
3+
* @param int[] $height
4+
* @return int
5+
*/
6+
7+
function maxArea($height) {
8+
$left = 0;
9+
$right = count($height) - 1;
10+
$maxArea = 0;
11+
12+
while ($left < $right) {
13+
$area = min($height[$left], $height[$right]) * ($right - $left);
14+
15+
$maxArea = max($maxArea, $area);
16+
17+
if ($height[$left] < $height[$right]) {
18+
$left++;
19+
} else {
20+
$right--;
21+
}
22+
}
23+
24+
return $maxArea;
25+
}
26+
}

solution/0000-0099/0012.Integer to Roman/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,44 @@ public class Solution {
181181
}
182182
```
183183

184+
```php
185+
class Solution {
186+
/**
187+
* @param int $num
188+
* @return string
189+
*/
190+
191+
function intToRoman($num) {
192+
$values = [
193+
'M' => 1000,
194+
'CM' => 900,
195+
'D' => 500,
196+
'CD' => 400,
197+
'C' => 100,
198+
'XC' => 90,
199+
'L' => 50,
200+
'XL' => 40,
201+
'X' => 10,
202+
'IX' => 9,
203+
'V' => 5,
204+
'IV' => 4,
205+
'I' => 1,
206+
];
207+
208+
$result = '';
209+
210+
foreach ($values as $roman => $value) {
211+
while ($num >= $value) {
212+
$result .= $roman;
213+
$num -= $value;
214+
}
215+
}
216+
217+
return $result;
218+
}
219+
}
220+
```
221+
184222
<!-- tabs:end -->
185223

186224
<!-- end -->

solution/0000-0099/0012.Integer to Roman/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,44 @@ public class Solution {
168168
}
169169
```
170170

171+
```php
172+
class Solution {
173+
/**
174+
* @param int $num
175+
* @return string
176+
*/
177+
178+
function intToRoman($num) {
179+
$values = [
180+
'M' => 1000,
181+
'CM' => 900,
182+
'D' => 500,
183+
'CD' => 400,
184+
'C' => 100,
185+
'XC' => 90,
186+
'L' => 50,
187+
'XL' => 40,
188+
'X' => 10,
189+
'IX' => 9,
190+
'V' => 5,
191+
'IV' => 4,
192+
'I' => 1,
193+
];
194+
195+
$result = '';
196+
197+
foreach ($values as $roman => $value) {
198+
while ($num >= $value) {
199+
$result .= $roman;
200+
$num -= $value;
201+
}
202+
}
203+
204+
return $result;
205+
}
206+
}
207+
```
208+
171209
<!-- tabs:end -->
172210

173211
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
/**
3+
* @param int $num
4+
* @return string
5+
*/
6+
7+
function intToRoman($num) {
8+
$values = [
9+
'M' => 1000,
10+
'CM' => 900,
11+
'D' => 500,
12+
'CD' => 400,
13+
'C' => 100,
14+
'XC' => 90,
15+
'L' => 50,
16+
'XL' => 40,
17+
'X' => 10,
18+
'IX' => 9,
19+
'V' => 5,
20+
'IV' => 4,
21+
'I' => 1,
22+
];
23+
24+
$result = '';
25+
26+
foreach ($values as $roman => $value) {
27+
while ($num >= $value) {
28+
$result .= $roman;
29+
$num -= $value;
30+
}
31+
}
32+
33+
return $result;
34+
}
35+
}

0 commit comments

Comments
 (0)