Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add php solution to lc problems: No.0029, 0030 #2413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 20 commits into doocs:main from ZylalMinollari:problems-29-30
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
263fd37
PHP solution of problems 0011,0012
Feb 20, 2024
1f9c25e
style: format code and docs with prettier
ZylalMinollari Feb 20, 2024
d79fef4
Update Solution.php
yanglbme Feb 21, 2024
e19bac3
Update Solution.php
yanglbme Feb 21, 2024
2ecf5ec
Merge branch 'doocs:main' into main
ZylalMinollari Feb 21, 2024
1c2b471
Merge branch 'doocs:main' into main
ZylalMinollari Feb 21, 2024
92e2da6
Merge branch 'doocs:main' into main
ZylalMinollari Feb 23, 2024
062524c
Merge branch 'doocs:main' into main
ZylalMinollari Mar 7, 2024
ed22277
Php solution for problems 0029, 0030
Mar 7, 2024
fe30b99
style: format code and docs with prettier
ZylalMinollari Mar 7, 2024
0920203
Merge branch 'doocs:main' into problems-29-30
ZylalMinollari Mar 8, 2024
6104508
Changes in naming variables
Mar 8, 2024
9a36092
style: format code and docs with prettier
ZylalMinollari Mar 8, 2024
aace25e
Removing php tag
Mar 8, 2024
21db7e6
Update README.md
yanglbme Mar 9, 2024
5718110
style: format code and docs with prettier
yanglbme Mar 9, 2024
a3220ec
Update Solution.php
yanglbme Mar 9, 2024
4884683
Update README_EN.md
yanglbme Mar 9, 2024
dfe9a1f
style: format code and docs with prettier
yanglbme Mar 9, 2024
d78450f
Update Solution.php
yanglbme Mar 9, 2024
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
38 changes: 38 additions & 0 deletions solution/0000-0099/0029.Divide Two Integers/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,44 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param integer $a
* @param integer $b
* @return integer
*/

function divide($a, $b) {
if ($b == 0) {
throw new Exception('Can not divide by 0');
} elseif ($a == 0) {
return 0;
}
if ($a == -2147483648 && $b == -1) {
return 2147483647;
}
$sign = $a < 0 != $b < 0;

$a = abs($a);
$b = abs($b);
$ans = 0;
while ($a >= $b) {
$x = $b;
$cnt = 1;
while ($a >= $x << 1) {
$x <<= 1;
$cnt <<= 1;
}
$a -= $x;
$ans += $cnt;
}

return $sign ? -$ans : $ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
38 changes: 38 additions & 0 deletions solution/0000-0099/0029.Divide Two Integers/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,44 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param integer $a
* @param integer $b
* @return integer
*/

function divide($a, $b) {
if ($b == 0) {
throw new Exception('Can not divide by 0');
} elseif ($a == 0) {
return 0;
}
if ($a == -2147483648 && $b == -1) {
return 2147483647;
}
$sign = $a < 0 != $b < 0;

$a = abs($a);
$b = abs($b);
$ans = 0;
while ($a >= $b) {
$x = $b;
$cnt = 1;
while ($a >= $x << 1) {
$x <<= 1;
$cnt <<= 1;
}
$a -= $x;
$ans += $cnt;
}

return $sign ? -$ans : $ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
35 changes: 35 additions & 0 deletions solution/0000-0099/0029.Divide Two Integers/Solution.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
/**
* @param integer $a
* @param integer $b
* @return integer
*/

function divide($a, $b) {
if ($b == 0) {
throw new Exception('Can not divide by 0');
} elseif ($a == 0) {
return 0;
}
if ($a == -2147483648 && $b == -1) {
return 2147483647;
}
$sign = $a < 0 != $b < 0;

$a = abs($a);
$b = abs($b);
$ans = 0;
while ($a >= $b) {
$x = $b;
$cnt = 1;
while ($a >= $x << 1) {
$x <<= 1;
$cnt <<= 1;
}
$a -= $x;
$ans += $cnt;
}

return $sign ? -$ans : $ans;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,62 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param String $s
* @param String[] $words
* @return Integer[]
*/
function findSubstring($s, $words) {
$cnt = [];
foreach ($words as $w) {
if (!isset($cnt[$w])) {
$cnt[$w] = 1;
} else {
$cnt[$w]++;
}
}
$m = strlen($s);
$n = count($words);
$k = strlen($words[0]);
$ans = [];
for ($i = 0; $i < $k; ++$i) {
$cnt1 = [];
$l = $i;
$r = $i;
$t = 0;
while ($r + $k <= $m) {
$w = substr($s, $r, $k);
$r += $k;
if (!array_key_exists($w, $cnt)) {
$cnt1 = [];
$l = $r;
$t = 0;
continue;
}
if (!isset($cnt1[$w])) {
$cnt1[$w] = 1;
} else {
$cnt1[$w]++;
}
++$t;
while ($cnt1[$w] > $cnt[$w]) {
$remove = substr($s, $l, $k);
$l += $k;
$cnt1[$remove]--;
$t--;
}
if ($t == $n) {
$ans[] = $l;
}
}
}
return $ans;
}
}
```

<!-- tabs:end -->

### 方法二
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,62 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param String $s
* @param String[] $words
* @return Integer[]
*/
function findSubstring($s, $words) {
$cnt = [];
foreach ($words as $w) {
if (!isset($cnt[$w])) {
$cnt[$w] = 1;
} else {
$cnt[$w]++;
}
}
$m = strlen($s);
$n = count($words);
$k = strlen($words[0]);
$ans = [];
for ($i = 0; $i < $k; ++$i) {
$cnt1 = [];
$l = $i;
$r = $i;
$t = 0;
while ($r + $k <= $m) {
$w = substr($s, $r, $k);
$r += $k;
if (!array_key_exists($w, $cnt)) {
$cnt1 = [];
$l = $r;
$t = 0;
continue;
}
if (!isset($cnt1[$w])) {
$cnt1[$w] = 1;
} else {
$cnt1[$w]++;
}
++$t;
while ($cnt1[$w] > $cnt[$w]) {
$remove = substr($s, $l, $k);
$l += $k;
$cnt1[$remove]--;
$t--;
}
if ($t == $n) {
$ans[] = $l;
}
}
}
return $ans;
}
}
```

<!-- tabs:end -->

### Solution 2
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Solution {
/**
* @param String $s
* @param String[] $words
* @return Integer[]
*/
function findSubstring($s, $words) {
$cnt = [];
foreach ($words as $w) {
if (!isset($cnt[$w])) {
$cnt[$w] = 1;
} else {
$cnt[$w]++;
}
}
$m = strlen($s);
$n = count($words);
$k = strlen($words[0]);
$ans = [];
for ($i = 0; $i < $k; ++$i) {
$cnt1 = [];
$l = $i;
$r = $i;
$t = 0;
while ($r + $k <= $m) {
$w = substr($s, $r, $k);
$r += $k;
if (!array_key_exists($w, $cnt)) {
$cnt1 = [];
$l = $r;
$t = 0;
continue;
}
if (!isset($cnt1[$w])) {
$cnt1[$w] = 1;
} else {
$cnt1[$w]++;
}
++$t;
while ($cnt1[$w] > $cnt[$w]) {
$remove = substr($s, $l, $k);
$l += $k;
$cnt1[$remove]--;
$t--;
}
if ($t == $n) {
$ans[] = $l;
}
}
}
return $ans;
}
}

AltStyle によって変換されたページ (->オリジナル) /