From 263fd37ebd6ca96cb6f018b7fde99405b910e870 Mon Sep 17 00:00:00 2001 From: ZylalMinollari Date: 2024年2月20日 10:38:12 +0100 Subject: [PATCH 01/15] PHP solution of problems 0011,0012 --- .../0011.Container With Most Water/README.md | 32 +++++++++++++++ .../README_EN.md | 32 +++++++++++++++ .../Solution.php | 29 ++++++++++++++ .../0000-0099/0012.Integer to Roman/README.md | 40 +++++++++++++++++++ .../0012.Integer to Roman/README_EN.md | 40 +++++++++++++++++++ .../0012.Integer to Roman/Solution.php | 37 +++++++++++++++++ 6 files changed, 210 insertions(+) create mode 100644 solution/0000-0099/0011.Container With Most Water/Solution.php create mode 100644 solution/0000-0099/0012.Integer to Roman/Solution.php diff --git a/solution/0000-0099/0011.Container With Most Water/README.md b/solution/0000-0099/0011.Container With Most Water/README.md index 4d81674da317f..96c789483c657 100644 --- a/solution/0000-0099/0011.Container With Most Water/README.md +++ b/solution/0000-0099/0011.Container With Most Water/README.md @@ -206,6 +206,38 @@ public class Solution { } ``` +```php +class Solution +{ + /** + * @param int[] $height + * @return int + */ + + function maxArea($height) + { + $left = 0; + $right = count($height) - 1; + $maxArea = 0; + + while ($left < $right) { + + $area = min($height[$left], $height[$right]) * ($right - $left); + + $maxArea = max($maxArea, $area); + + if ($height[$left] < $height[$right]) { + $left++; + } else { + $right--; + } + } + + return $maxArea; + } +} +``` + diff --git a/solution/0000-0099/0011.Container With Most Water/README_EN.md b/solution/0000-0099/0011.Container With Most Water/README_EN.md index 68cdb99432b7d..403470079cb3b 100644 --- a/solution/0000-0099/0011.Container With Most Water/README_EN.md +++ b/solution/0000-0099/0011.Container With Most Water/README_EN.md @@ -201,6 +201,38 @@ public class Solution { } ``` +```php +class Solution +{ + /** + * @param int[] $height + * @return int + */ + + function maxArea($height) + { + $left = 0; + $right = count($height) - 1; + $maxArea = 0; + + while ($left < $right) { + + $area = min($height[$left], $height[$right]) * ($right - $left); + + $maxArea = max($maxArea, $area); + + if ($height[$left] < $height[$right]) { + $left++; + } else { + $right--; + } + } + + return $maxArea; + } +} +``` + diff --git a/solution/0000-0099/0011.Container With Most Water/Solution.php b/solution/0000-0099/0011.Container With Most Water/Solution.php new file mode 100644 index 0000000000000..7e4025b882e21 --- /dev/null +++ b/solution/0000-0099/0011.Container With Most Water/Solution.php @@ -0,0 +1,29 @@ +class Solution +{ + /** + * @param int[] $height + * @return int + */ + + function maxArea($height) + { + $left = 0; + $right = count($height) - 1; + $maxArea = 0; + + while ($left < $right) { + + $area = min($height[$left], $height[$right]) * ($right - $left); + + $maxArea = max($maxArea, $area); + + if ($height[$left] < $height[$right]) { + $left++; + } else { + $right--; + } + } + + return $maxArea; + } +} diff --git a/solution/0000-0099/0012.Integer to Roman/README.md b/solution/0000-0099/0012.Integer to Roman/README.md index 6e406775637ec..23501e8b23db8 100644 --- a/solution/0000-0099/0012.Integer to Roman/README.md +++ b/solution/0000-0099/0012.Integer to Roman/README.md @@ -181,6 +181,46 @@ public class Solution { } ``` +```php +class Solution +{ + /** + * @param int $num + * @return string + */ + + function intToRoman($num) + { + $values = [ + 'M' => 1000, + 'CM' => 900, + 'D' => 500, + 'CD' => 400, + 'C' => 100, + 'XC' => 90, + 'L' => 50, + 'XL' => 40, + 'X' => 10, + 'IX' => 9, + 'V' => 5, + 'IV' => 4, + 'I' => 1, + ]; + + $result = ''; + + foreach ($values as $roman => $value) { + while ($num>= $value) { + $result .= $roman; + $num -= $value; + } + } + + return $result; + } +} +``` + diff --git a/solution/0000-0099/0012.Integer to Roman/README_EN.md b/solution/0000-0099/0012.Integer to Roman/README_EN.md index 110d2047b445c..6519e6941420d 100644 --- a/solution/0000-0099/0012.Integer to Roman/README_EN.md +++ b/solution/0000-0099/0012.Integer to Roman/README_EN.md @@ -168,6 +168,46 @@ public class Solution { } ``` +```php +class Solution +{ + /** + * @param int $num + * @return string + */ + + function intToRoman($num) + { + $values = [ + 'M' => 1000, + 'CM' => 900, + 'D' => 500, + 'CD' => 400, + 'C' => 100, + 'XC' => 90, + 'L' => 50, + 'XL' => 40, + 'X' => 10, + 'IX' => 9, + 'V' => 5, + 'IV' => 4, + 'I' => 1, + ]; + + $result = ''; + + foreach ($values as $roman => $value) { + while ($num>= $value) { + $result .= $roman; + $num -= $value; + } + } + + return $result; + } +} +``` + diff --git a/solution/0000-0099/0012.Integer to Roman/Solution.php b/solution/0000-0099/0012.Integer to Roman/Solution.php new file mode 100644 index 0000000000000..a6106716d5b81 --- /dev/null +++ b/solution/0000-0099/0012.Integer to Roman/Solution.php @@ -0,0 +1,37 @@ +class Solution +{ + /** + * @param int $num + * @return string + */ + + function intToRoman($num) + { + $values = [ + 'M' => 1000, + 'CM' => 900, + 'D' => 500, + 'CD' => 400, + 'C' => 100, + 'XC' => 90, + 'L' => 50, + 'XL' => 40, + 'X' => 10, + 'IX' => 9, + 'V' => 5, + 'IV' => 4, + 'I' => 1, + ]; + + $result = ''; + + foreach ($values as $roman => $value) { + while ($num>= $value) { + $result .= $roman; + $num -= $value; + } + } + + return $result; + } +} From 1f9c25e3758c17ec2ddc5974d7b09dc41ddfd635 Mon Sep 17 00:00:00 2001 From: ZylalMinollari Date: 2024年2月20日 09:44:49 +0000 Subject: [PATCH 02/15] style: format code and docs with prettier --- .../0000-0099/0011.Container With Most Water/README.md | 7 ++----- .../0000-0099/0011.Container With Most Water/README_EN.md | 7 ++----- solution/0000-0099/0012.Integer to Roman/README.md | 6 ++---- solution/0000-0099/0012.Integer to Roman/README_EN.md | 6 ++---- 4 files changed, 8 insertions(+), 18 deletions(-) diff --git a/solution/0000-0099/0011.Container With Most Water/README.md b/solution/0000-0099/0011.Container With Most Water/README.md index 96c789483c657..6dbd8cf41fce7 100644 --- a/solution/0000-0099/0011.Container With Most Water/README.md +++ b/solution/0000-0099/0011.Container With Most Water/README.md @@ -207,21 +207,18 @@ public class Solution { ``` ```php -class Solution -{ +class Solution { /** * @param int[] $height * @return int */ - function maxArea($height) - { + function maxArea($height) { $left = 0; $right = count($height) - 1; $maxArea = 0; while ($left < $right) { - $area = min($height[$left], $height[$right]) * ($right - $left); $maxArea = max($maxArea, $area); diff --git a/solution/0000-0099/0011.Container With Most Water/README_EN.md b/solution/0000-0099/0011.Container With Most Water/README_EN.md index 403470079cb3b..f321d2f5d264a 100644 --- a/solution/0000-0099/0011.Container With Most Water/README_EN.md +++ b/solution/0000-0099/0011.Container With Most Water/README_EN.md @@ -202,21 +202,18 @@ public class Solution { ``` ```php -class Solution -{ +class Solution { /** * @param int[] $height * @return int */ - function maxArea($height) - { + function maxArea($height) { $left = 0; $right = count($height) - 1; $maxArea = 0; while ($left < $right) { - $area = min($height[$left], $height[$right]) * ($right - $left); $maxArea = max($maxArea, $area); diff --git a/solution/0000-0099/0012.Integer to Roman/README.md b/solution/0000-0099/0012.Integer to Roman/README.md index 23501e8b23db8..1002a00bb1425 100644 --- a/solution/0000-0099/0012.Integer to Roman/README.md +++ b/solution/0000-0099/0012.Integer to Roman/README.md @@ -182,15 +182,13 @@ public class Solution { ``` ```php -class Solution -{ +class Solution { /** * @param int $num * @return string */ - function intToRoman($num) - { + function intToRoman($num) { $values = [ 'M' => 1000, 'CM' => 900, diff --git a/solution/0000-0099/0012.Integer to Roman/README_EN.md b/solution/0000-0099/0012.Integer to Roman/README_EN.md index 6519e6941420d..5d2bbfa90b174 100644 --- a/solution/0000-0099/0012.Integer to Roman/README_EN.md +++ b/solution/0000-0099/0012.Integer to Roman/README_EN.md @@ -169,15 +169,13 @@ public class Solution { ``` ```php -class Solution -{ +class Solution { /** * @param int $num * @return string */ - function intToRoman($num) - { + function intToRoman($num) { $values = [ 'M' => 1000, 'CM' => 900, From d79fef4c749a40f7b0679c8e0f4e9b9e35e2a36e Mon Sep 17 00:00:00 2001 From: Libin YANG Date: 2024年2月21日 09:25:13 +0800 Subject: [PATCH 03/15] Update Solution.php --- .../0000-0099/0011.Container With Most Water/Solution.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/solution/0000-0099/0011.Container With Most Water/Solution.php b/solution/0000-0099/0011.Container With Most Water/Solution.php index 7e4025b882e21..320347510d2fa 100644 --- a/solution/0000-0099/0011.Container With Most Water/Solution.php +++ b/solution/0000-0099/0011.Container With Most Water/Solution.php @@ -1,18 +1,15 @@ -class Solution -{ +class Solution { /** * @param int[] $height * @return int */ - function maxArea($height) - { + function maxArea($height) { $left = 0; $right = count($height) - 1; $maxArea = 0; while ($left < $right) { - $area = min($height[$left], $height[$right]) * ($right - $left); $maxArea = max($maxArea, $area); From e19bac3a5edbd54980de93c37c9d151b9809db09 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: 2024年2月21日 09:25:57 +0800 Subject: [PATCH 04/15] Update Solution.php --- solution/0000-0099/0012.Integer to Roman/Solution.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/solution/0000-0099/0012.Integer to Roman/Solution.php b/solution/0000-0099/0012.Integer to Roman/Solution.php index a6106716d5b81..1f0523d2f333b 100644 --- a/solution/0000-0099/0012.Integer to Roman/Solution.php +++ b/solution/0000-0099/0012.Integer to Roman/Solution.php @@ -1,12 +1,10 @@ -class Solution -{ +class Solution { /** * @param int $num * @return string */ - function intToRoman($num) - { + function intToRoman($num) { $values = [ 'M' => 1000, 'CM' => 900, From ed2227791c0129a8fe1abf71cb7681476cdf33e1 Mon Sep 17 00:00:00 2001 From: ZylalMinollari Date: Thu, 7 Mar 2024 16:05:55 +0100 Subject: [PATCH 05/15] Php solution for problems 0029, 0030 --- .../0029.Divide Two Integers/README.md | 39 +++++++++++++++++++ .../0029.Divide Two Integers/README_EN.md | 39 +++++++++++++++++++ .../0029.Divide Two Integers/Solution.php | 36 +++++++++++++++++ .../README.md | 37 ++++++++++++++++++ .../README_EN.md | 37 ++++++++++++++++++ .../Solution.php | 34 ++++++++++++++++ 6 files changed, 222 insertions(+) create mode 100644 solution/0000-0099/0029.Divide Two Integers/Solution.php create mode 100644 solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php diff --git a/solution/0000-0099/0029.Divide Two Integers/README.md b/solution/0000-0099/0029.Divide Two Integers/README.md index 0d26aa3d471c9..bedb9f07b070f 100644 --- a/solution/0000-0099/0029.Divide Two Integers/README.md +++ b/solution/0000-0099/0029.Divide Two Integers/README.md @@ -227,6 +227,45 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param integer $dividend + * @param integer $divisor + * @return integer + */ + + function divide($dividend, $divisor) { + + if ($divisor == 0) { + throw new Exception("Can not divide by 0"); + } else if ($dividend == 0) { + return 0; + } + if ($dividend == -2147483648 && $divisor == -1) { + return 2147483647; + } + $isNegative = ($dividend < 0) != ($divisor < 0); + + $dividend = abs($dividend); + $divisor = abs($divisor); + $quotient = 0; + while ($dividend>= $divisor) { + $tempDivisor = $divisor; + $count = 1; + while ($dividend>= ($tempDivisor << 1)) { + $tempDivisor <<= 1; + $count <<= 1; + } + $dividend -= $tempDivisor; + $quotient += $count; + } + + return $isNegative ? -$quotient : $quotient; + } +} +``` + 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..07bfcfb70a21a 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,45 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param integer $dividend + * @param integer $divisor + * @return integer + */ + + function divide($dividend, $divisor) { + + if ($divisor == 0) { + throw new Exception("Can not divide by 0"); + } else if ($dividend == 0) { + return 0; + } + if ($dividend == -2147483648 && $divisor == -1) { + return 2147483647; + } + $isNegative = ($dividend < 0) != ($divisor < 0); + + $dividend = abs($dividend); + $divisor = abs($divisor); + $quotient = 0; + while ($dividend>= $divisor) { + $tempDivisor = $divisor; + $count = 1; + while ($dividend>= ($tempDivisor << 1)) { + $tempDivisor <<= 1; + $count <<= 1; + } + $dividend -= $tempDivisor; + $quotient += $count; + } + + return $isNegative ? -$quotient : $quotient; + } +} +``` + 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..8ad2c0a06bb90 --- /dev/null +++ b/solution/0000-0099/0029.Divide Two Integers/Solution.php @@ -0,0 +1,36 @@ +class Solution { + /** + * @param integer $dividend + * @param integer $divisor + * @return integer + */ + + function divide($dividend, $divisor) { + + if ($divisor == 0) { + throw new Exception("Can not divide by 0"); + } else if ($dividend == 0) { + return 0; + } + if ($dividend == -2147483648 && $divisor == -1) { + return 2147483647; + } + $isNegative = ($dividend < 0) != ($divisor < 0); + + $dividend = abs($dividend); + $divisor = abs($divisor); + $quotient = 0; + while ($dividend>= $divisor) { + $tempDivisor = $divisor; + $count = 1; + while ($dividend>= ($tempDivisor << 1)) { + $tempDivisor <<= 1; + $count <<= 1; + } + $dividend -= $tempDivisor; + $quotient += $count; + } + + return $isNegative ? -$quotient : $quotient; + } +} 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..1bb5f967fdc08 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,43 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param string $s + * @param string[] $words + * @return integer[] + */ + + function findSubstring($s, $words) { + $wordLength = strlen($words[0]); + $totalLength = count($words) * $wordLength; + $wordFreq = array_count_values($words); + $result = []; + for ($i = 0; $i <= strlen($s) - $totalLength; $i++) { + $seen = []; + $j = 0; + while ($j < $totalLength) { + $word = substr($s, $i + $j, $wordLength); + if (isset($wordFreq[$word])) { + $seen[$word] = isset($seen[$word]) ? $seen[$word] + 1 : 1; + if ($seen[$word]> $wordFreq[$word]) { + break; + } + $j += $wordLength; + } else { + break; + } + } + if ($j === $totalLength) { + $result[] = $i; + } + } + return $result; + } +} +``` + ### 方法二 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..a0b9884b06fa9 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,43 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param string $s + * @param string[] $words + * @return integer[] + */ + + function findSubstring($s, $words) { + $wordLength = strlen($words[0]); + $totalLength = count($words) * $wordLength; + $wordFreq = array_count_values($words); + $result = []; + for ($i = 0; $i <= strlen($s) - $totalLength; $i++) { + $seen = []; + $j = 0; + while ($j < $totalLength) { + $word = substr($s, $i + $j, $wordLength); + if (isset($wordFreq[$word])) { + $seen[$word] = isset($seen[$word]) ? $seen[$word] + 1 : 1; + if ($seen[$word]> $wordFreq[$word]) { + break; + } + $j += $wordLength; + } else { + break; + } + } + if ($j === $totalLength) { + $result[] = $i; + } + } + return $result; + } +} +``` + ### 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..f7ff58b8afdb2 --- /dev/null +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php @@ -0,0 +1,34 @@ +class Solution { + /** + * @param string $s + * @param string[] $words + * @return integer[] + */ + + function findSubstring($s, $words) { + $wordLength = strlen($words[0]); + $totalLength = count($words) * $wordLength; + $wordFreq = array_count_values($words); + $result = []; + for ($i = 0; $i <= strlen($s) - $totalLength; $i++) { + $seen = []; + $j = 0; + while ($j < $totalLength) { + $word = substr($s, $i + $j, $wordLength); + if (isset($wordFreq[$word])) { + $seen[$word] = isset($seen[$word]) ? $seen[$word] + 1 : 1; + if ($seen[$word]> $wordFreq[$word]) { + break; + } + $j += $wordLength; + } else { + break; + } + } + if ($j === $totalLength) { + $result[] = $i; + } + } + return $result; + } +} From fe30b99a31d83f398ecd245bd07acdbc436ade97 Mon Sep 17 00:00:00 2001 From: ZylalMinollari Date: Thu, 7 Mar 2024 15:10:38 +0000 Subject: [PATCH 06/15] style: format code and docs with prettier --- solution/0000-0099/0029.Divide Two Integers/README.md | 9 ++++----- solution/0000-0099/0029.Divide Two Integers/README_EN.md | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/solution/0000-0099/0029.Divide Two Integers/README.md b/solution/0000-0099/0029.Divide Two Integers/README.md index bedb9f07b070f..7a5d519e0c277 100644 --- a/solution/0000-0099/0029.Divide Two Integers/README.md +++ b/solution/0000-0099/0029.Divide Two Integers/README.md @@ -236,16 +236,15 @@ class Solution { */ function divide($dividend, $divisor) { - if ($divisor == 0) { - throw new Exception("Can not divide by 0"); - } else if ($dividend == 0) { + throw new Exception('Can not divide by 0'); + } elseif ($dividend == 0) { return 0; } if ($dividend == -2147483648 && $divisor == -1) { return 2147483647; } - $isNegative = ($dividend < 0) != ($divisor < 0); + $isNegative = $dividend < 0 != $divisor < 0; $dividend = abs($dividend); $divisor = abs($divisor); @@ -253,7 +252,7 @@ class Solution { while ($dividend>= $divisor) { $tempDivisor = $divisor; $count = 1; - while ($dividend>= ($tempDivisor << 1)) { + while ($dividend>= $tempDivisor << 1) { $tempDivisor <<= 1; $count <<= 1; } 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 07bfcfb70a21a..61f7b246884fa 100644 --- a/solution/0000-0099/0029.Divide Two Integers/README_EN.md +++ b/solution/0000-0099/0029.Divide Two Integers/README_EN.md @@ -234,16 +234,15 @@ class Solution { */ function divide($dividend, $divisor) { - if ($divisor == 0) { - throw new Exception("Can not divide by 0"); - } else if ($dividend == 0) { + throw new Exception('Can not divide by 0'); + } elseif ($dividend == 0) { return 0; } if ($dividend == -2147483648 && $divisor == -1) { return 2147483647; } - $isNegative = ($dividend < 0) != ($divisor < 0); + $isNegative = $dividend < 0 != $divisor < 0; $dividend = abs($dividend); $divisor = abs($divisor); @@ -251,7 +250,7 @@ class Solution { while ($dividend>= $divisor) { $tempDivisor = $divisor; $count = 1; - while ($dividend>= ($tempDivisor << 1)) { + while ($dividend>= $tempDivisor << 1) { $tempDivisor <<= 1; $count <<= 1; } From 61045084368b98ee55909ffef130b609ee0106c5 Mon Sep 17 00:00:00 2001 From: ZylalMinollari Date: Fri, 8 Mar 2024 17:37:12 +0100 Subject: [PATCH 07/15] Changes in naming variables --- .../0029.Divide Two Integers/README.md | 43 ++++++++++--------- .../0029.Divide Two Integers/README_EN.md | 43 ++++++++++--------- .../0029.Divide Two Integers/Solution.php | 39 +++++++++-------- 3 files changed, 64 insertions(+), 61 deletions(-) diff --git a/solution/0000-0099/0029.Divide Two Integers/README.md b/solution/0000-0099/0029.Divide Two Integers/README.md index 7a5d519e0c277..48fc90b08ad0a 100644 --- a/solution/0000-0099/0029.Divide Two Integers/README.md +++ b/solution/0000-0099/0029.Divide Two Integers/README.md @@ -230,37 +230,38 @@ public class Solution { ```php class Solution { /** - * @param integer $dividend - * @param integer $divisor + * @param integer $a + * @param integer $b * @return integer */ - function divide($dividend, $divisor) { - if ($divisor == 0) { - throw new Exception('Can not divide by 0'); - } elseif ($dividend == 0) { + function divide($a, $b) { + + if ($b == 0) { + throw new Exception("Can not divide by 0"); + } else if ($a == 0) { return 0; } - if ($dividend == -2147483648 && $divisor == -1) { + if ($a == -2147483648 && $b == -1) { return 2147483647; } - $isNegative = $dividend < 0 != $divisor < 0; - - $dividend = abs($dividend); - $divisor = abs($divisor); - $quotient = 0; - while ($dividend>= $divisor) { - $tempDivisor = $divisor; - $count = 1; - while ($dividend>= $tempDivisor << 1) { - $tempDivisor <<= 1; - $count <<= 1; + $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; } - $dividend -= $tempDivisor; - $quotient += $count; + $a -= $x; + $ans += $cnt; } - return $isNegative ? -$quotient : $quotient; + 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 61f7b246884fa..6a80456393fda 100644 --- a/solution/0000-0099/0029.Divide Two Integers/README_EN.md +++ b/solution/0000-0099/0029.Divide Two Integers/README_EN.md @@ -228,37 +228,38 @@ public class Solution { ```php class Solution { /** - * @param integer $dividend - * @param integer $divisor + * @param integer $a + * @param integer $b * @return integer */ - function divide($dividend, $divisor) { - if ($divisor == 0) { - throw new Exception('Can not divide by 0'); - } elseif ($dividend == 0) { + function divide($a, $b) { + + if ($b == 0) { + throw new Exception("Can not divide by 0"); + } else if ($a == 0) { return 0; } - if ($dividend == -2147483648 && $divisor == -1) { + if ($a == -2147483648 && $b == -1) { return 2147483647; } - $isNegative = $dividend < 0 != $divisor < 0; - - $dividend = abs($dividend); - $divisor = abs($divisor); - $quotient = 0; - while ($dividend>= $divisor) { - $tempDivisor = $divisor; - $count = 1; - while ($dividend>= $tempDivisor << 1) { - $tempDivisor <<= 1; - $count <<= 1; + $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; } - $dividend -= $tempDivisor; - $quotient += $count; + $a -= $x; + $ans += $cnt; } - return $isNegative ? -$quotient : $quotient; + 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 index 8ad2c0a06bb90..601423714b8b2 100644 --- a/solution/0000-0099/0029.Divide Two Integers/Solution.php +++ b/solution/0000-0099/0029.Divide Two Integers/Solution.php @@ -1,36 +1,37 @@ += $divisor) { - $tempDivisor = $divisor; - $count = 1; - while ($dividend>= ($tempDivisor << 1)) { - $tempDivisor <<= 1; - $count <<= 1; + $a = abs($a); + $b = abs($b); + $ans = 0; + while ($a>= $b) { + $x = $b; + $cnt = 1; + while ($a>= ($x << 1)) { + $x <<= 1; + $cnt <<= 1; } - $dividend -= $tempDivisor; - $quotient += $count; + $a -= $x; + $ans += $cnt; } - return $isNegative ? -$quotient : $quotient; + return $sign ? -$ans : $ans; } } From 9a36092079cfa2d46f0949d8baec0c0b11abca02 Mon Sep 17 00:00:00 2001 From: ZylalMinollari Date: Fri, 8 Mar 2024 16:38:02 +0000 Subject: [PATCH 08/15] style: format code and docs with prettier --- solution/0000-0099/0029.Divide Two Integers/README.md | 9 ++++----- solution/0000-0099/0029.Divide Two Integers/README_EN.md | 9 ++++----- solution/0000-0099/0029.Divide Two Integers/Solution.php | 9 ++++----- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/solution/0000-0099/0029.Divide Two Integers/README.md b/solution/0000-0099/0029.Divide Two Integers/README.md index 48fc90b08ad0a..3b356398fb4c9 100644 --- a/solution/0000-0099/0029.Divide Two Integers/README.md +++ b/solution/0000-0099/0029.Divide Two Integers/README.md @@ -236,16 +236,15 @@ class Solution { */ function divide($a, $b) { - if ($b == 0) { - throw new Exception("Can not divide by 0"); - } else if ($a == 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); + $sign = $a < 0 != $b < 0; $a = abs($a); $b = abs($b); @@ -253,7 +252,7 @@ class Solution { while ($a>= $b) { $x = $b; $cnt = 1; - while ($a>= ($x << 1)) { + while ($a>= $x << 1) { $x <<= 1; $cnt <<= 1; } 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 6a80456393fda..f124c64261b31 100644 --- a/solution/0000-0099/0029.Divide Two Integers/README_EN.md +++ b/solution/0000-0099/0029.Divide Two Integers/README_EN.md @@ -234,16 +234,15 @@ class Solution { */ function divide($a, $b) { - if ($b == 0) { - throw new Exception("Can not divide by 0"); - } else if ($a == 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); + $sign = $a < 0 != $b < 0; $a = abs($a); $b = abs($b); @@ -251,7 +250,7 @@ class Solution { while ($a>= $b) { $x = $b; $cnt = 1; - while ($a>= ($x << 1)) { + while ($a>= $x << 1) { $x <<= 1; $cnt <<= 1; } diff --git a/solution/0000-0099/0029.Divide Two Integers/Solution.php b/solution/0000-0099/0029.Divide Two Integers/Solution.php index 601423714b8b2..3bf4f34fe5ecb 100644 --- a/solution/0000-0099/0029.Divide Two Integers/Solution.php +++ b/solution/0000-0099/0029.Divide Two Integers/Solution.php @@ -7,16 +7,15 @@ class Solution { */ function divide($a, $b) { - if ($b == 0) { - throw new Exception("Can not divide by 0"); - } else if ($a == 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); + $sign = $a < 0 != $b < 0; $a = abs($a); $b = abs($b); @@ -24,7 +23,7 @@ function divide($a, $b) { while ($a>= $b) { $x = $b; $cnt = 1; - while ($a>= ($x << 1)) { + while ($a>= $x << 1) { $x <<= 1; $cnt <<= 1; } From aace25e07df5e1bd2b82ebec2db19572ee842dd9 Mon Sep 17 00:00:00 2001 From: ZylalMinollari Date: Fri, 8 Mar 2024 17:40:58 +0100 Subject: [PATCH 09/15] Removing php tag --- solution/0000-0099/0029.Divide Two Integers/Solution.php | 1 - 1 file changed, 1 deletion(-) diff --git a/solution/0000-0099/0029.Divide Two Integers/Solution.php b/solution/0000-0099/0029.Divide Two Integers/Solution.php index 3bf4f34fe5ecb..b3330dbacd0c3 100644 --- a/solution/0000-0099/0029.Divide Two Integers/Solution.php +++ b/solution/0000-0099/0029.Divide Two Integers/Solution.php @@ -1,4 +1,3 @@ - Date: Sat, 9 Mar 2024 09:41:47 +0800 Subject: [PATCH 10/15] Update README.md --- .../README.md | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 deletions(-) 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 1bb5f967fdc08..cc8e094fd915f 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 @@ -310,37 +310,57 @@ public class Solution { ```php class Solution { + /** - * @param string $s - * @param string[] $words - * @return integer[] + * @param String $s + * @param String[] $words + * @return Integer[] */ - function findSubstring($s, $words) { - $wordLength = strlen($words[0]); - $totalLength = count($words) * $wordLength; - $wordFreq = array_count_values($words); - $result = []; - for ($i = 0; $i <= strlen($s) - $totalLength; $i++) { - $seen = []; - $j = 0; - while ($j < $totalLength) { - $word = substr($s, $i + $j, $wordLength); - if (isset($wordFreq[$word])) { - $seen[$word] = isset($seen[$word]) ? $seen[$word] + 1 : 1; - if ($seen[$word]> $wordFreq[$word]) { - break; - } - $j += $wordLength; + $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 { - break; + $cnt1[$w]++; + } + ++$t; + while ($cnt1[$w]> $cnt[$w]) { + $remove = substr($s, $l, $k); + $l += $k; + $cnt1[$remove]--; + $t--; + } + if ($t == $n) { + $ans[] = $l; } - } - if ($j === $totalLength) { - $result[] = $i; } } - return $result; + return $ans; } } ``` From 5718110894272de5fe88b23d8ba0d5f4d23b0691 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 9 Mar 2024 01:42:34 +0000 Subject: [PATCH 11/15] style: format code and docs with prettier --- .../0030.Substring with Concatenation of All Words/README.md | 1 - 1 file changed, 1 deletion(-) 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 cc8e094fd915f..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 @@ -310,7 +310,6 @@ public class Solution { ```php class Solution { - /** * @param String $s * @param String[] $words From a3220ec51da294697b4a391caf9f6be1ee7ce606 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 9 Mar 2024 09:42:45 +0800 Subject: [PATCH 12/15] Update Solution.php --- .../Solution.php | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 deletions(-) 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 index f7ff58b8afdb2..e8213abe12f3f 100644 --- 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 @@ -1,34 +1,54 @@ class Solution { + /** - * @param string $s - * @param string[] $words - * @return integer[] + * @param String $s + * @param String[] $words + * @return Integer[] */ - function findSubstring($s, $words) { - $wordLength = strlen($words[0]); - $totalLength = count($words) * $wordLength; - $wordFreq = array_count_values($words); - $result = []; - for ($i = 0; $i <= strlen($s) - $totalLength; $i++) { - $seen = []; - $j = 0; - while ($j < $totalLength) { - $word = substr($s, $i + $j, $wordLength); - if (isset($wordFreq[$word])) { - $seen[$word] = isset($seen[$word]) ? $seen[$word] + 1 : 1; - if ($seen[$word]> $wordFreq[$word]) { - break; - } - $j += $wordLength; + $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 { - break; + $cnt1[$w]++; + } + ++$t; + while ($cnt1[$w]> $cnt[$w]) { + $remove = substr($s, $l, $k); + $l += $k; + $cnt1[$remove]--; + $t--; + } + if ($t == $n) { + $ans[] = $l; } - } - if ($j === $totalLength) { - $result[] = $i; } } - return $result; + return $ans; } } From 4884683d44e8bef0d8ad56d1200ce77ca54b5c4d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 9 Mar 2024 09:43:06 +0800 Subject: [PATCH 13/15] Update README_EN.md --- .../README_EN.md | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 deletions(-) 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 a0b9884b06fa9..e3947d8ae426b 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 @@ -307,37 +307,57 @@ public class Solution { ```php class Solution { + /** - * @param string $s - * @param string[] $words - * @return integer[] + * @param String $s + * @param String[] $words + * @return Integer[] */ - function findSubstring($s, $words) { - $wordLength = strlen($words[0]); - $totalLength = count($words) * $wordLength; - $wordFreq = array_count_values($words); - $result = []; - for ($i = 0; $i <= strlen($s) - $totalLength; $i++) { - $seen = []; - $j = 0; - while ($j < $totalLength) { - $word = substr($s, $i + $j, $wordLength); - if (isset($wordFreq[$word])) { - $seen[$word] = isset($seen[$word]) ? $seen[$word] + 1 : 1; - if ($seen[$word]> $wordFreq[$word]) { - break; - } - $j += $wordLength; + $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 { - break; + $cnt1[$w]++; + } + ++$t; + while ($cnt1[$w]> $cnt[$w]) { + $remove = substr($s, $l, $k); + $l += $k; + $cnt1[$remove]--; + $t--; + } + if ($t == $n) { + $ans[] = $l; } - } - if ($j === $totalLength) { - $result[] = $i; } } - return $result; + return $ans; } } ``` From dfe9a1f411d53f4149713041fcbe457adaee5047 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 9 Mar 2024 01:44:00 +0000 Subject: [PATCH 14/15] style: format code and docs with prettier --- .../0030.Substring with Concatenation of All Words/README_EN.md | 1 - 1 file changed, 1 deletion(-) 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 e3947d8ae426b..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 @@ -307,7 +307,6 @@ public class Solution { ```php class Solution { - /** * @param String $s * @param String[] $words From d78450f334fab9515b37f3c6991797250a023e43 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 9 Mar 2024 09:44:27 +0800 Subject: [PATCH 15/15] Update Solution.php --- .../0030.Substring with Concatenation of All Words/Solution.php | 1 - 1 file changed, 1 deletion(-) 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 index e8213abe12f3f..666c296650ef1 100644 --- 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 @@ -1,5 +1,4 @@ class Solution { - /** * @param String $s * @param String[] $words

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