Skip to content

Commit f325cc5

Browse files
feat: add php solution to lc problems: No.0029,0030 (doocs#2413)
1 parent 891dc6e commit f325cc5

File tree

6 files changed

+276
-0
lines changed

6 files changed

+276
-0
lines changed

solution/0000-0099/0029.Divide Two Integers/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,44 @@ public class Solution {
227227
}
228228
```
229229

230+
```php
231+
class Solution {
232+
/**
233+
* @param integer $a
234+
* @param integer $b
235+
* @return integer
236+
*/
237+
238+
function divide($a, $b) {
239+
if ($b == 0) {
240+
throw new Exception('Can not divide by 0');
241+
} elseif ($a == 0) {
242+
return 0;
243+
}
244+
if ($a == -2147483648 && $b == -1) {
245+
return 2147483647;
246+
}
247+
$sign = $a < 0 != $b < 0;
248+
249+
$a = abs($a);
250+
$b = abs($b);
251+
$ans = 0;
252+
while ($a >= $b) {
253+
$x = $b;
254+
$cnt = 1;
255+
while ($a >= $x << 1) {
256+
$x <<= 1;
257+
$cnt <<= 1;
258+
}
259+
$a -= $x;
260+
$ans += $cnt;
261+
}
262+
263+
return $sign ? -$ans : $ans;
264+
}
265+
}
266+
```
267+
230268
<!-- tabs:end -->
231269

232270
<!-- end -->

solution/0000-0099/0029.Divide Two Integers/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,44 @@ public class Solution {
225225
}
226226
```
227227

228+
```php
229+
class Solution {
230+
/**
231+
* @param integer $a
232+
* @param integer $b
233+
* @return integer
234+
*/
235+
236+
function divide($a, $b) {
237+
if ($b == 0) {
238+
throw new Exception('Can not divide by 0');
239+
} elseif ($a == 0) {
240+
return 0;
241+
}
242+
if ($a == -2147483648 && $b == -1) {
243+
return 2147483647;
244+
}
245+
$sign = $a < 0 != $b < 0;
246+
247+
$a = abs($a);
248+
$b = abs($b);
249+
$ans = 0;
250+
while ($a >= $b) {
251+
$x = $b;
252+
$cnt = 1;
253+
while ($a >= $x << 1) {
254+
$x <<= 1;
255+
$cnt <<= 1;
256+
}
257+
$a -= $x;
258+
$ans += $cnt;
259+
}
260+
261+
return $sign ? -$ans : $ans;
262+
}
263+
}
264+
```
265+
228266
<!-- tabs:end -->
229267

230268
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
/**
3+
* @param integer $a
4+
* @param integer $b
5+
* @return integer
6+
*/
7+
8+
function divide($a, $b) {
9+
if ($b == 0) {
10+
throw new Exception('Can not divide by 0');
11+
} elseif ($a == 0) {
12+
return 0;
13+
}
14+
if ($a == -2147483648 && $b == -1) {
15+
return 2147483647;
16+
}
17+
$sign = $a < 0 != $b < 0;
18+
19+
$a = abs($a);
20+
$b = abs($b);
21+
$ans = 0;
22+
while ($a >= $b) {
23+
$x = $b;
24+
$cnt = 1;
25+
while ($a >= $x << 1) {
26+
$x <<= 1;
27+
$cnt <<= 1;
28+
}
29+
$a -= $x;
30+
$ans += $cnt;
31+
}
32+
33+
return $sign ? -$ans : $ans;
34+
}
35+
}

solution/0000-0099/0030.Substring with Concatenation of All Words/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,62 @@ public class Solution {
308308
}
309309
```
310310

311+
```php
312+
class Solution {
313+
/**
314+
* @param String $s
315+
* @param String[] $words
316+
* @return Integer[]
317+
*/
318+
function findSubstring($s, $words) {
319+
$cnt = [];
320+
foreach ($words as $w) {
321+
if (!isset($cnt[$w])) {
322+
$cnt[$w] = 1;
323+
} else {
324+
$cnt[$w]++;
325+
}
326+
}
327+
$m = strlen($s);
328+
$n = count($words);
329+
$k = strlen($words[0]);
330+
$ans = [];
331+
for ($i = 0; $i < $k; ++$i) {
332+
$cnt1 = [];
333+
$l = $i;
334+
$r = $i;
335+
$t = 0;
336+
while ($r + $k <= $m) {
337+
$w = substr($s, $r, $k);
338+
$r += $k;
339+
if (!array_key_exists($w, $cnt)) {
340+
$cnt1 = [];
341+
$l = $r;
342+
$t = 0;
343+
continue;
344+
}
345+
if (!isset($cnt1[$w])) {
346+
$cnt1[$w] = 1;
347+
} else {
348+
$cnt1[$w]++;
349+
}
350+
++$t;
351+
while ($cnt1[$w] > $cnt[$w]) {
352+
$remove = substr($s, $l, $k);
353+
$l += $k;
354+
$cnt1[$remove]--;
355+
$t--;
356+
}
357+
if ($t == $n) {
358+
$ans[] = $l;
359+
}
360+
}
361+
}
362+
return $ans;
363+
}
364+
}
365+
```
366+
311367
<!-- tabs:end -->
312368

313369
### 方法二

solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md

+56
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,62 @@ public class Solution {
305305
}
306306
```
307307

308+
```php
309+
class Solution {
310+
/**
311+
* @param String $s
312+
* @param String[] $words
313+
* @return Integer[]
314+
*/
315+
function findSubstring($s, $words) {
316+
$cnt = [];
317+
foreach ($words as $w) {
318+
if (!isset($cnt[$w])) {
319+
$cnt[$w] = 1;
320+
} else {
321+
$cnt[$w]++;
322+
}
323+
}
324+
$m = strlen($s);
325+
$n = count($words);
326+
$k = strlen($words[0]);
327+
$ans = [];
328+
for ($i = 0; $i < $k; ++$i) {
329+
$cnt1 = [];
330+
$l = $i;
331+
$r = $i;
332+
$t = 0;
333+
while ($r + $k <= $m) {
334+
$w = substr($s, $r, $k);
335+
$r += $k;
336+
if (!array_key_exists($w, $cnt)) {
337+
$cnt1 = [];
338+
$l = $r;
339+
$t = 0;
340+
continue;
341+
}
342+
if (!isset($cnt1[$w])) {
343+
$cnt1[$w] = 1;
344+
} else {
345+
$cnt1[$w]++;
346+
}
347+
++$t;
348+
while ($cnt1[$w] > $cnt[$w]) {
349+
$remove = substr($s, $l, $k);
350+
$l += $k;
351+
$cnt1[$remove]--;
352+
$t--;
353+
}
354+
if ($t == $n) {
355+
$ans[] = $l;
356+
}
357+
}
358+
}
359+
return $ans;
360+
}
361+
}
362+
```
363+
308364
<!-- tabs:end -->
309365

310366
### Solution 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
/**
3+
* @param String $s
4+
* @param String[] $words
5+
* @return Integer[]
6+
*/
7+
function findSubstring($s, $words) {
8+
$cnt = [];
9+
foreach ($words as $w) {
10+
if (!isset($cnt[$w])) {
11+
$cnt[$w] = 1;
12+
} else {
13+
$cnt[$w]++;
14+
}
15+
}
16+
$m = strlen($s);
17+
$n = count($words);
18+
$k = strlen($words[0]);
19+
$ans = [];
20+
for ($i = 0; $i < $k; ++$i) {
21+
$cnt1 = [];
22+
$l = $i;
23+
$r = $i;
24+
$t = 0;
25+
while ($r + $k <= $m) {
26+
$w = substr($s, $r, $k);
27+
$r += $k;
28+
if (!array_key_exists($w, $cnt)) {
29+
$cnt1 = [];
30+
$l = $r;
31+
$t = 0;
32+
continue;
33+
}
34+
if (!isset($cnt1[$w])) {
35+
$cnt1[$w] = 1;
36+
} else {
37+
$cnt1[$w]++;
38+
}
39+
++$t;
40+
while ($cnt1[$w] > $cnt[$w]) {
41+
$remove = substr($s, $l, $k);
42+
$l += $k;
43+
$cnt1[$remove]--;
44+
$t--;
45+
}
46+
if ($t == $n) {
47+
$ans[] = $l;
48+
}
49+
}
50+
}
51+
return $ans;
52+
}
53+
}

0 commit comments

Comments
 (0)