diff --git a/solution/0000-0099/0029.Divide Two Integers/README.md b/solution/0000-0099/0029.Divide Two Integers/README.md index 0d26aa3d471c9..3b356398fb4c9 100644 --- a/solution/0000-0099/0029.Divide Two Integers/README.md +++ b/solution/0000-0099/0029.Divide Two Integers/README.md @@ -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; + } +} +``` + diff --git a/solution/0000-0099/0029.Divide Two Integers/README_EN.md b/solution/0000-0099/0029.Divide Two Integers/README_EN.md index 776afc3dce99b..f124c64261b31 100644 --- a/solution/0000-0099/0029.Divide Two Integers/README_EN.md +++ b/solution/0000-0099/0029.Divide Two Integers/README_EN.md @@ -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; + } +} +``` + diff --git a/solution/0000-0099/0029.Divide Two Integers/Solution.php b/solution/0000-0099/0029.Divide Two Integers/Solution.php new file mode 100644 index 0000000000000..b3330dbacd0c3 --- /dev/null +++ b/solution/0000-0099/0029.Divide Two Integers/Solution.php @@ -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; + } +} diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md b/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md index 996c75fdbcdf0..e350a36811517 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md @@ -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; + } +} +``` + ### 方法二 diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md b/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md index 6d34b1471b385..b120574d75236 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md @@ -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; + } +} +``` + ### Solution 2 diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php new file mode 100644 index 0000000000000..666c296650ef1 --- /dev/null +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php @@ -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 によって変換されたページ (->オリジナル) /