From 9c8eb683a74eb345d1074262c5cb9900f7e47ad7 Mon Sep 17 00:00:00 2001 From: ZylalMinollari Date: Thu, 9 Nov 2023 14:59:10 +0100 Subject: [PATCH 01/14] Add php solution to lc problems:No.0002,0003 --- .../0000-0099/0002.Add Two Numbers/README.md | 53 +++++++++++++++++++ .../0002.Add Two Numbers/README_EN.md | 53 +++++++++++++++++++ .../0002.Add Two Numbers/Solution.php | 49 +++++++++++++++++ .../README.md | 29 ++++++++++ .../README_EN.md | 29 ++++++++++ .../Solution.php | 25 +++++++++ 6 files changed, 238 insertions(+) create mode 100644 solution/0000-0099/0002.Add Two Numbers/Solution.php create mode 100644 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php diff --git a/solution/0000-0099/0002.Add Two Numbers/README.md b/solution/0000-0099/0002.Add Two Numbers/README.md index d6254efb74e06..f43c90f746132 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README.md +++ b/solution/0000-0099/0002.Add Two Numbers/README.md @@ -432,6 +432,59 @@ impl Solution { } ``` +### **PHP** + +```php +/** + * Definition for a singly-linked list. + * class ListNode { + * public $val = 0; + * public $next = null; + * function __construct($val = 0, $next = null) { + * $this->val = $val; + * $this->next = $next; + * } + * } + */ +class Solution { + + /** + * @param ListNode $l1 + * @param ListNode $l2 + * @return ListNode + */ + function addTwoNumbers($l1, $l2) { + $dummy = new ListNode(0); + $current = $dummy; + $carry = 0; + + while ($l1 !== null || $l2 !== null) { + $x = ($l1 !== null) ? $l1->val : 0; + $y = ($l2 !== null) ? $l2->val : 0; + + $sum = $x + $y + $carry; + $carry = (int) ($sum / 10); + $current->next = new ListNode($sum % 10); + $current = $current->next; + + if ($l1 !== null) { + $l1 = $l1->next; + } + + if ($l2 !== null) { + $l2 = $l2->next; + } + } + + if ($carry > 0) { + $current->next = new ListNode($carry); + } + + return $dummy->next; + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0002.Add Two Numbers/README_EN.md b/solution/0000-0099/0002.Add Two Numbers/README_EN.md index 4e31b588358df..ac624b47fd650 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README_EN.md +++ b/solution/0000-0099/0002.Add Two Numbers/README_EN.md @@ -420,6 +420,59 @@ impl Solution { } ``` +### **PHP** + +```php +/** + * Definition for a singly-linked list. + * class ListNode { + * public $val = 0; + * public $next = null; + * function __construct($val = 0, $next = null) { + * $this->val = $val; + * $this->next = $next; + * } + * } + */ +class Solution { + + /** + * @param ListNode $l1 + * @param ListNode $l2 + * @return ListNode + */ + function addTwoNumbers($l1, $l2) { + $dummy = new ListNode(0); + $current = $dummy; + $carry = 0; + + while ($l1 !== null || $l2 !== null) { + $x = ($l1 !== null) ? $l1->val : 0; + $y = ($l2 !== null) ? $l2->val : 0; + + $sum = $x + $y + $carry; + $carry = (int) ($sum / 10); + $current->next = new ListNode($sum % 10); + $current = $current->next; + + if ($l1 !== null) { + $l1 = $l1->next; + } + + if ($l2 !== null) { + $l2 = $l2->next; + } + } + + if ($carry > 0) { + $current->next = new ListNode($carry); + } + + return $dummy->next; + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0002.Add Two Numbers/Solution.php b/solution/0000-0099/0002.Add Two Numbers/Solution.php new file mode 100644 index 0000000000000..e3b5696398e9e --- /dev/null +++ b/solution/0000-0099/0002.Add Two Numbers/Solution.php @@ -0,0 +1,49 @@ +val = $val; + * $this->next = $next; + * } + * } + */ +class Solution { + + /** + * @param ListNode $l1 + * @param ListNode $l2 + * @return ListNode + */ + function addTwoNumbers($l1, $l2) { + $dummy = new ListNode(0); + $current = $dummy; + $carry = 0; + + while ($l1 !== null || $l2 !== null) { + $x = ($l1 !== null) ? $l1->val : 0; + $y = ($l2 !== null) ? $l2->val : 0; + + $sum = $x + $y + $carry; + $carry = (int) ($sum / 10); + $current->next = new ListNode($sum % 10); + $current = $current->next; + + if ($l1 !== null) { + $l1 = $l1->next; + } + + if ($l2 !== null) { + $l2 = $l2->next; + } + } + + if ($carry > 0) { + $current->next = new ListNode($carry); + } + + return $dummy->next; + } +} \ No newline at end of file diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index 497814a323c11..3a96bd06e267d 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -349,6 +349,35 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function lengthOfLongestSubstring($s) { + $max = 0; + for ($i = 0; $i < strlen($s); $i++) { + $chars = array(); + $sub = ''; + for ($j = $i; $j < strlen($s); $j++) { + if (in_array($s[$j], $chars)) { + break; + } + $sub .= $s[$j]; + $chars[] = $s[$j]; + } + if (strlen($sub) > $max) { + $max = strlen($sub); + } + } + return $max; + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md index 16d6fa6943582..a768c6122f680 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md @@ -339,6 +339,35 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function lengthOfLongestSubstring($s) { + $max = 0; + for ($i = 0; $i < strlen($s); $i++) { + $chars = array(); + $sub = ''; + for ($j = $i; $j < strlen($s); $j++) { + if (in_array($s[$j], $chars)) { + break; + } + $sub .= $s[$j]; + $chars[] = $s[$j]; + } + if (strlen($sub) > $max) { + $max = strlen($sub); + } + } + return $max; + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php new file mode 100644 index 0000000000000..233c4cce507b2 --- /dev/null +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php @@ -0,0 +1,25 @@ + $max) { + $max = strlen($sub); + } + } + return $max; + } +} \ No newline at end of file From a55b09ae14fa5056314ffe082d0ebf07b3bcc614 Mon Sep 17 00:00:00 2001 From: ZylalMinollari Date: Thu, 9 Nov 2023 16:27:55 +0000 Subject: [PATCH 02/14] style: format code and docs with prettier --- solution/0000-0099/0002.Add Two Numbers/README.md | 5 ++--- solution/0000-0099/0002.Add Two Numbers/README_EN.md | 5 ++--- solution/0000-0099/0002.Add Two Numbers/Solution.php | 7 +++---- .../README.md | 2 +- .../README_EN.md | 2 +- .../Solution.php | 4 ++-- 6 files changed, 11 insertions(+), 14 deletions(-) diff --git a/solution/0000-0099/0002.Add Two Numbers/README.md b/solution/0000-0099/0002.Add Two Numbers/README.md index f43c90f746132..88366a856db39 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README.md +++ b/solution/0000-0099/0002.Add Two Numbers/README.md @@ -447,7 +447,6 @@ impl Solution { * } */ class Solution { - /** * @param ListNode $l1 * @param ListNode $l2 @@ -459,8 +458,8 @@ class Solution { $carry = 0; while ($l1 !== null || $l2 !== null) { - $x = ($l1 !== null) ? $l1->val : 0; - $y = ($l2 !== null) ? $l2->val : 0; + $x = $l1 !== null ? $l1->val : 0; + $y = $l2 !== null ? $l2->val : 0; $sum = $x + $y + $carry; $carry = (int) ($sum / 10); diff --git a/solution/0000-0099/0002.Add Two Numbers/README_EN.md b/solution/0000-0099/0002.Add Two Numbers/README_EN.md index ac624b47fd650..fd42b5ee6c37c 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README_EN.md +++ b/solution/0000-0099/0002.Add Two Numbers/README_EN.md @@ -435,7 +435,6 @@ impl Solution { * } */ class Solution { - /** * @param ListNode $l1 * @param ListNode $l2 @@ -447,8 +446,8 @@ class Solution { $carry = 0; while ($l1 !== null || $l2 !== null) { - $x = ($l1 !== null) ? $l1->val : 0; - $y = ($l2 !== null) ? $l2->val : 0; + $x = $l1 !== null ? $l1->val : 0; + $y = $l2 !== null ? $l2->val : 0; $sum = $x + $y + $carry; $carry = (int) ($sum / 10); diff --git a/solution/0000-0099/0002.Add Two Numbers/Solution.php b/solution/0000-0099/0002.Add Two Numbers/Solution.php index e3b5696398e9e..4a79350fc0151 100644 --- a/solution/0000-0099/0002.Add Two Numbers/Solution.php +++ b/solution/0000-0099/0002.Add Two Numbers/Solution.php @@ -11,7 +11,6 @@ * } */ class Solution { - /** * @param ListNode $l1 * @param ListNode $l2 @@ -23,8 +22,8 @@ function addTwoNumbers($l1, $l2) { $carry = 0; while ($l1 !== null || $l2 !== null) { - $x = ($l1 !== null) ? $l1->val : 0; - $y = ($l2 !== null) ? $l2->val : 0; + $x = $l1 !== null ? $l1->val : 0; + $y = $l2 !== null ? $l2->val : 0; $sum = $x + $y + $carry; $carry = (int) ($sum / 10); @@ -46,4 +45,4 @@ function addTwoNumbers($l1, $l2) { return $dummy->next; } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index 3a96bd06e267d..c1f1114a6ce4c 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -360,7 +360,7 @@ class Solution { function lengthOfLongestSubstring($s) { $max = 0; for ($i = 0; $i < strlen($s); $i++) { - $chars = array(); + $chars = []; $sub = ''; for ($j = $i; $j < strlen($s); $j++) { if (in_array($s[$j], $chars)) { diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md index a768c6122f680..b81860c895c4d 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md @@ -350,7 +350,7 @@ class Solution { function lengthOfLongestSubstring($s) { $max = 0; for ($i = 0; $i < strlen($s); $i++) { - $chars = array(); + $chars = []; $sub = ''; for ($j = $i; $j < strlen($s); $j++) { if (in_array($s[$j], $chars)) { diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php index 233c4cce507b2..a9a934f562ff5 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php @@ -7,7 +7,7 @@ class Solution { function lengthOfLongestSubstring($s) { $max = 0; for ($i = 0; $i < strlen($s); $i++) { - $chars = array(); + $chars = []; $sub = ''; for ($j = $i; $j < strlen($s); $j++) { if (in_array($s[$j], $chars)) { @@ -22,4 +22,4 @@ function lengthOfLongestSubstring($s) { } return $max; } -} \ No newline at end of file +} From 55ba2c8999c2f9c08e9ded90d0b3080b020f311f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 10 Nov 2023 07:49:56 +0800 Subject: [PATCH 03/14] Update Solution.php --- solution/0000-0099/0002.Add Two Numbers/Solution.php | 1 - 1 file changed, 1 deletion(-) diff --git a/solution/0000-0099/0002.Add Two Numbers/Solution.php b/solution/0000-0099/0002.Add Two Numbers/Solution.php index 4a79350fc0151..f5f14f9b7d0dd 100644 --- a/solution/0000-0099/0002.Add Two Numbers/Solution.php +++ b/solution/0000-0099/0002.Add Two Numbers/Solution.php @@ -1,4 +1,3 @@ - Date: Fri, 10 Nov 2023 07:50:29 +0800 Subject: [PATCH 04/14] Update Solution.php --- .../Solution.php | 1 - 1 file changed, 1 deletion(-) diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php index a9a934f562ff5..e3d728856d3e9 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.php @@ -1,4 +1,3 @@ - Date: Fri, 19 Jan 2024 11:38:04 +0100 Subject: [PATCH 05/14] feat:add php solution to lc problems:No.0004-0010 --- .../README.md | 26 ++++++++++ .../README_EN.md | 26 ++++++++++ .../Solution.php | 23 +++++++++ .../README.md | 39 +++++++++++++++ .../README_EN.md | 39 +++++++++++++++ .../Solution.php | 37 +++++++++++++++ .../0006.Zigzag Conversion/README.md | 34 ++++++++++++++ .../0006.Zigzag Conversion/README_EN.md | 34 ++++++++++++++ .../0006.Zigzag Conversion/Solution.php | 31 ++++++++++++ .../0000-0099/0007.Reverse Integer/README.md | 32 +++++++++++++ .../0007.Reverse Integer/README_EN.md | 47 +++++++++++++++++++ .../0007.Reverse Integer/Solution.php | 29 ++++++++++++ .../0008.String to Integer (atoi)/README.md | 22 +++++++++ .../README_EN.md | 22 +++++++++ .../Solution.php | 19 ++++++++ .../0009.Palindrome Number/README.md | 17 +++++++ .../0009.Palindrome Number/README_EN.md | 17 +++++++ .../0009.Palindrome Number/Solution.php | 14 ++++++ .../README.md | 43 +++++++++++++++++ .../README_EN.md | 43 +++++++++++++++++ .../Solution.php | 41 ++++++++++++++++ 21 files changed, 635 insertions(+) create mode 100644 solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php create mode 100644 solution/0000-0099/0005.Longest Palindromic Substring/Solution.php create mode 100644 solution/0000-0099/0006.Zigzag Conversion/Solution.php create mode 100644 solution/0000-0099/0007.Reverse Integer/Solution.php create mode 100644 solution/0000-0099/0008.String to Integer (atoi)/Solution.php create mode 100644 solution/0000-0099/0009.Palindrome Number/Solution.php create mode 100644 solution/0000-0099/0010.Regular Expression Matching/Solution.php diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md index 839cecea32014..62914292ee020 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md @@ -271,6 +271,32 @@ public class Solution { } ``` +```php +class Solution +{ + + /** + * @param int[] $nums1 + * @param int[] $nums2 + * @return float + */ + + function findMedianSortedArrays($nums1, $nums2) + { + + $arr = array_merge($nums1, $nums2); + sort($arr); + $cnt_arr = count($arr); + + if ($cnt_arr % 2) { + return $arr[$cnt_arr / 2]; + } else { + return ($arr[intdiv($cnt_arr, 2) - 1] + $arr[intdiv($cnt_arr, 2)]) / 2; + } + } +} +``` + ```nim import std/[algorithm, sequtils] diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md index 044bb180b05d3..cc2a1d76924e2 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md @@ -265,6 +265,32 @@ public class Solution { } ``` +```php +class Solution +{ + + /** + * @param int[] $nums1 + * @param int[] $nums2 + * @return float + */ + + function findMedianSortedArrays($nums1, $nums2) + { + + $arr = array_merge($nums1, $nums2); + sort($arr); + $cnt_arr = count($arr); + + if ($cnt_arr % 2) { + return $arr[$cnt_arr / 2]; + } else { + return ($arr[intdiv($cnt_arr, 2) - 1] + $arr[intdiv($cnt_arr, 2)]) / 2; + } + } +} +``` + ```nim import std/[algorithm, sequtils] diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php new file mode 100644 index 0000000000000..3b4e74d46d0d7 --- /dev/null +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php @@ -0,0 +1,23 @@ +class Solution +{ + + /** + * @param int[] $nums1 + * @param int[] $nums2 + * @return float + */ + + function findMedianSortedArrays($nums1, $nums2) + { + + $arr = array_merge($nums1, $nums2); + sort($arr); + $cnt_arr = count($arr); + + if ($cnt_arr % 2) { + return $arr[$cnt_arr / 2]; + } else { + return ($arr[intdiv($cnt_arr, 2) - 1] + $arr[intdiv($cnt_arr, 2)]) / 2; + } + } +} diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README.md b/solution/0000-0099/0005.Longest Palindromic Substring/README.md index 1922159a8e432..cb72a2b4332c6 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README.md @@ -410,6 +410,45 @@ impl Solution { } ``` +```php +class Solution +{ + /** + * @param string $s + * @return string + */ + function longestPalindrome($s) + { + $start = 0; + $maxLength = 0; + + for ($i = 0; $i < strlen($s); $i++) { + $len1 = $this->expandFromCenter($s, $i, $i); + $len2 = $this->expandFromCenter($s, $i, $i + 1); + + $len = max($len1, $len2); + + if ($len > $maxLength) { + $start = $i - intval(($len - 1) / 2); + $maxLength = $len; + } + } + + return substr($s, $start, $maxLength); + } + + function expandFromCenter($s, $left, $right) + { + while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) { + $left--; + $right++; + } + + return $right - $left - 1; + } +} +``` + diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md index ccf126a3e3591..ec657fb12dd3e 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md @@ -404,6 +404,45 @@ impl Solution { } ``` +```php +class Solution +{ + /** + * @param string $s + * @return string + */ + function longestPalindrome($s) + { + $start = 0; + $maxLength = 0; + + for ($i = 0; $i < strlen($s); $i++) { + $len1 = $this->expandFromCenter($s, $i, $i); + $len2 = $this->expandFromCenter($s, $i, $i + 1); + + $len = max($len1, $len2); + + if ($len > $maxLength) { + $start = $i - intval(($len - 1) / 2); + $maxLength = $len; + } + } + + return substr($s, $start, $maxLength); + } + + function expandFromCenter($s, $left, $right) + { + while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) { + $left--; + $right++; + } + + return $right - $left - 1; + } +} +``` + diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php new file mode 100644 index 0000000000000..8106597ed68da --- /dev/null +++ b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php @@ -0,0 +1,37 @@ +expandFromCenter($s, $i, $i); + $len2 = $this->expandFromCenter($s, $i, $i + 1); + + $len = max($len1, $len2); + + if ($len > $maxLength) { + $start = $i - intval(($len - 1) / 2); + $maxLength = $len; + } + } + + return substr($s, $start, $maxLength); + } + + function expandFromCenter($s, $left, $right) + { + while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) { + $left--; + $right++; + } + + return $right - $left - 1; + } +} diff --git a/solution/0000-0099/0006.Zigzag Conversion/README.md b/solution/0000-0099/0006.Zigzag Conversion/README.md index 51157d9ea0ac2..9e91e00452a65 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README.md @@ -411,6 +411,40 @@ var convert = function (s, numRows) { }; ``` +```php +class Solution +{ + /** + * @param string $s + * @param int $numRows + * @return string + */ + + function convert($s, $numRows) + { + if ($numRows == 1 || strlen($s) <= $numRows) { + return $s; + } + + $result = ''; + $cycleLength = 2 * $numRows - 2; + $n = strlen($s); + + for ($i = 0; $i < $numRows; $i++) { + for ($j = 0; $j + $i < $n; $j += $cycleLength) { + $result .= $s[$j + $i]; + + if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) { + $result .= $s[$j + $cycleLength - $i]; + } + } + } + + return $result; + } +} +``` + diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md index 21c753e25c056..5be1402e316cf 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md @@ -407,6 +407,40 @@ var convert = function (s, numRows) { }; ``` +```php +class Solution +{ + /** + * @param string $s + * @param int $numRows + * @return string + */ + + function convert($s, $numRows) + { + if ($numRows == 1 || strlen($s) <= $numRows) { + return $s; + } + + $result = ''; + $cycleLength = 2 * $numRows - 2; + $n = strlen($s); + + for ($i = 0; $i < $numRows; $i++) { + for ($j = 0; $j + $i < $n; $j += $cycleLength) { + $result .= $s[$j + $i]; + + if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) { + $result .= $s[$j + $cycleLength - $i]; + } + } + } + + return $result; + } +} +``` + diff --git a/solution/0000-0099/0006.Zigzag Conversion/Solution.php b/solution/0000-0099/0006.Zigzag Conversion/Solution.php new file mode 100644 index 0000000000000..ea3750fe4a403 --- /dev/null +++ b/solution/0000-0099/0006.Zigzag Conversion/Solution.php @@ -0,0 +1,31 @@ +class Solution +{ + /** + * @param string $s + * @param int $numRows + * @return string + */ + + function convert($s, $numRows) + { + if ($numRows == 1 || strlen($s) <= $numRows) { + return $s; + } + + $result = ''; + $cycleLength = 2 * $numRows - 2; + $n = strlen($s); + + for ($i = 0; $i < $numRows; $i++) { + for ($j = 0; $j + $i < $n; $j += $cycleLength) { + $result .= $s[$j + $i]; + + if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) { + $result .= $s[$j + $cycleLength - $i]; + } + } + } + + return $result; + } +} diff --git a/solution/0000-0099/0007.Reverse Integer/README.md b/solution/0000-0099/0007.Reverse Integer/README.md index 1c0474905adb1..e249326abcb10 100644 --- a/solution/0000-0099/0007.Reverse Integer/README.md +++ b/solution/0000-0099/0007.Reverse Integer/README.md @@ -193,6 +193,38 @@ int reverse(int x) { } ``` +```php +class Solution +{ + /** + * @param int $x + * @return int + */ + + function reverse($x) + { + $isNegative = $x < 0; + $x = abs($x); + + $reversed = 0; + + while ($x > 0) { + $reversed = $reversed * 10 + $x % 10; + $x = (int) ($x / 10); + } + + if ($isNegative) { + $reversed *= -1; + } + if ($reversed < -pow(2, 31) || $reversed > pow(2, 31) - 1) { + return 0; + } + + return $reversed; + } +} +``` + diff --git a/solution/0000-0099/0007.Reverse Integer/README_EN.md b/solution/0000-0099/0007.Reverse Integer/README_EN.md index 7512badd0522a..ccab03102970c 100644 --- a/solution/0000-0099/0007.Reverse Integer/README_EN.md +++ b/solution/0000-0099/0007.Reverse Integer/README_EN.md @@ -181,6 +181,53 @@ int reverse(int x) { } ``` +```cs +public class Solution { + public int Reverse(int x) { + int ans = 0; + for (; x != 0; x /= 10) { + if (ans < int.MinValue / 10 || ans > int.MaxValue / 10) { + return 0; + } + ans = ans * 10 + x % 10; + } + return ans; + } +} +``` + +```php +class Solution +{ + /** + * @param int $x + * @return int + */ + + function reverse($x) + { + $isNegative = $x < 0; + $x = abs($x); + + $reversed = 0; + + while ($x > 0) { + $reversed = $reversed * 10 + $x % 10; + $x = (int) ($x / 10); + } + + if ($isNegative) { + $reversed *= -1; + } + if ($reversed < -pow(2, 31) || $reversed > pow(2, 31) - 1) { + return 0; + } + + return $reversed; + } +} +``` + diff --git a/solution/0000-0099/0007.Reverse Integer/Solution.php b/solution/0000-0099/0007.Reverse Integer/Solution.php new file mode 100644 index 0000000000000..45be68c9c8d4c --- /dev/null +++ b/solution/0000-0099/0007.Reverse Integer/Solution.php @@ -0,0 +1,29 @@ +class Solution +{ + /** + * @param int $x + * @return int + */ + + function reverse($x) + { + $isNegative = $x < 0; + $x = abs($x); + + $reversed = 0; + + while ($x > 0) { + $reversed = $reversed * 10 + $x % 10; + $x = (int) ($x / 10); + } + + if ($isNegative) { + $reversed *= -1; + } + if ($reversed < -pow(2, 31) || $reversed > pow(2, 31) - 1) { + return 0; + } + + return $reversed; + } +} diff --git a/solution/0000-0099/0008.String to Integer (atoi)/README.md b/solution/0000-0099/0008.String to Integer (atoi)/README.md index 510f85674addd..6e23a82588d93 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/README.md +++ b/solution/0000-0099/0008.String to Integer (atoi)/README.md @@ -275,6 +275,28 @@ public partial class Solution } ``` +```php +class Solution +{ + /** + * @param string $s + * @return int + */ + + function myAtoi($s) + { + $s = str_replace("e", "x", $s); + if (intval($s) < pow(-2, 31)) { + return -2147483648; + } + if (intval($s) > pow(2, 31) - 1) { + return 2147483647; + } + return intval($s); + } +} +``` + diff --git a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md index a93f2c2e3866f..45d75db2003b5 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md +++ b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md @@ -270,6 +270,28 @@ public partial class Solution } ``` +```php +class Solution +{ + /** + * @param string $s + * @return int + */ + + function myAtoi($s) + { + $s = str_replace("e", "x", $s); + if (intval($s) < pow(-2, 31)) { + return -2147483648; + } + if (intval($s) > pow(2, 31) - 1) { + return 2147483647; + } + return intval($s); + } +} +``` + diff --git a/solution/0000-0099/0008.String to Integer (atoi)/Solution.php b/solution/0000-0099/0008.String to Integer (atoi)/Solution.php new file mode 100644 index 0000000000000..b5c68b3e1a82b --- /dev/null +++ b/solution/0000-0099/0008.String to Integer (atoi)/Solution.php @@ -0,0 +1,19 @@ +class Solution +{ + /** + * @param string $s + * @return int + */ + + function myAtoi($s) + { + $s = str_replace("e", "x", $s); + if (intval($s) < pow(-2, 31)) { + return -2147483648; + } + if (intval($s) > pow(2, 31) - 1) { + return 2147483647; + } + return intval($s); + } +} diff --git a/solution/0000-0099/0009.Palindrome Number/README.md b/solution/0000-0099/0009.Palindrome Number/README.md index 8ac7f651bd3dc..829b2c76a613c 100644 --- a/solution/0000-0099/0009.Palindrome Number/README.md +++ b/solution/0000-0099/0009.Palindrome Number/README.md @@ -211,6 +211,23 @@ impl Solution { } ``` +```php +class Solution +{ + /** + * @param int $x + * @return boolean + */ + + function isPalindrome($x) + { + $str = (string) $x; + $str_reverse = strrev($str); + return ($str === $str_reverse); + } +} +``` + diff --git a/solution/0000-0099/0009.Palindrome Number/README_EN.md b/solution/0000-0099/0009.Palindrome Number/README_EN.md index 86cb617af9bea..f3c4e8e34f879 100644 --- a/solution/0000-0099/0009.Palindrome Number/README_EN.md +++ b/solution/0000-0099/0009.Palindrome Number/README_EN.md @@ -201,6 +201,23 @@ impl Solution { } ``` +```php +class Solution +{ + /** + * @param int $x + * @return boolean + */ + + function isPalindrome($x) + { + $str = (string) $x; + $str_reverse = strrev($str); + return ($str === $str_reverse); + } +} +``` + diff --git a/solution/0000-0099/0009.Palindrome Number/Solution.php b/solution/0000-0099/0009.Palindrome Number/Solution.php new file mode 100644 index 0000000000000..ce8399b28bf73 --- /dev/null +++ b/solution/0000-0099/0009.Palindrome Number/Solution.php @@ -0,0 +1,14 @@ +class Solution +{ + /** + * @param int $x + * @return boolean + */ + + function isPalindrome($x) + { + $str = (string) $x; + $str_reverse = strrev($str); + return ($str === $str_reverse); + } +} diff --git a/solution/0000-0099/0010.Regular Expression Matching/README.md b/solution/0000-0099/0010.Regular Expression Matching/README.md index d8da6408e5118..56649f1a9a54d 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/README.md +++ b/solution/0000-0099/0010.Regular Expression Matching/README.md @@ -451,6 +451,49 @@ public class Solution { } ``` +```php +class Solution +{ + /** + * @param string $s + * @param string $p + * @return boolean + */ + + function isMatch($s, $p) + { + $m = strlen($s); + $n = strlen($p); + + $dp = array_fill(0, $m + 1, array_fill(0, $n + 1, false)); + $dp[0][0] = true; + + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '*') { + $dp[0][$j] = $dp[0][$j - 2]; + } + } + + for ($i = 1; $i <= $m; $i++) { + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '.' || $p[$j - 1] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i - 1][$j - 1]; + } elseif ($p[$j - 1] == '*') { + $dp[$i][$j] = $dp[$i][$j - 2]; + if ($p[$j - 2] == '.' || $p[$j - 2] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i][$j] || $dp[$i - 1][$j]; + } + } else { + $dp[$i][$j] = false; + } + } + } + + return $dp[$m][$n]; + } +} +``` + diff --git a/solution/0000-0099/0010.Regular Expression Matching/README_EN.md b/solution/0000-0099/0010.Regular Expression Matching/README_EN.md index 22e6613f3ec73..244439aab5951 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/README_EN.md +++ b/solution/0000-0099/0010.Regular Expression Matching/README_EN.md @@ -448,6 +448,49 @@ public class Solution { } ``` +```php +class Solution +{ + /** + * @param string $s + * @param string $p + * @return boolean + */ + + function isMatch($s, $p) + { + $m = strlen($s); + $n = strlen($p); + + $dp = array_fill(0, $m + 1, array_fill(0, $n + 1, false)); + $dp[0][0] = true; + + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '*') { + $dp[0][$j] = $dp[0][$j - 2]; + } + } + + for ($i = 1; $i <= $m; $i++) { + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '.' || $p[$j - 1] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i - 1][$j - 1]; + } elseif ($p[$j - 1] == '*') { + $dp[$i][$j] = $dp[$i][$j - 2]; + if ($p[$j - 2] == '.' || $p[$j - 2] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i][$j] || $dp[$i - 1][$j]; + } + } else { + $dp[$i][$j] = false; + } + } + } + + return $dp[$m][$n]; + } +} +``` + diff --git a/solution/0000-0099/0010.Regular Expression Matching/Solution.php b/solution/0000-0099/0010.Regular Expression Matching/Solution.php new file mode 100644 index 0000000000000..a168a3eea58d5 --- /dev/null +++ b/solution/0000-0099/0010.Regular Expression Matching/Solution.php @@ -0,0 +1,41 @@ + Date: Fri, 19 Jan 2024 10:48:58 +0000 Subject: [PATCH 06/14] style: format code and docs with prettier --- .../0000-0099/0004.Median of Two Sorted Arrays/README.md | 8 ++------ .../0004.Median of Two Sorted Arrays/README_EN.md | 8 ++------ .../0005.Longest Palindromic Substring/README.md | 9 +++------ .../0005.Longest Palindromic Substring/README_EN.md | 9 +++------ .../0005.Longest Palindromic Substring/Solution.php | 9 +++------ solution/0000-0099/0006.Zigzag Conversion/README.md | 6 ++---- solution/0000-0099/0006.Zigzag Conversion/README_EN.md | 6 ++---- solution/0000-0099/0007.Reverse Integer/README.md | 8 +++----- solution/0000-0099/0007.Reverse Integer/README_EN.md | 8 +++----- .../0000-0099/0008.String to Integer (atoi)/README.md | 8 +++----- .../0000-0099/0008.String to Integer (atoi)/README_EN.md | 8 +++----- solution/0000-0099/0009.Palindrome Number/README.md | 8 +++----- solution/0000-0099/0009.Palindrome Number/README_EN.md | 8 +++----- .../0000-0099/0010.Regular Expression Matching/README.md | 6 ++---- .../0010.Regular Expression Matching/README_EN.md | 6 ++---- .../0010.Regular Expression Matching/Solution.php | 6 ++---- 16 files changed, 41 insertions(+), 80 deletions(-) diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md index 62914292ee020..3cbc61669a55c 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md @@ -272,18 +272,14 @@ public class Solution { ``` ```php -class Solution -{ - +class Solution { /** * @param int[] $nums1 * @param int[] $nums2 * @return float */ - function findMedianSortedArrays($nums1, $nums2) - { - + function findMedianSortedArrays($nums1, $nums2) { $arr = array_merge($nums1, $nums2); sort($arr); $cnt_arr = count($arr); diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md index cc2a1d76924e2..4d7e34f073df0 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md @@ -266,18 +266,14 @@ public class Solution { ``` ```php -class Solution -{ - +class Solution { /** * @param int[] $nums1 * @param int[] $nums2 * @return float */ - function findMedianSortedArrays($nums1, $nums2) - { - + function findMedianSortedArrays($nums1, $nums2) { $arr = array_merge($nums1, $nums2); sort($arr); $cnt_arr = count($arr); diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README.md b/solution/0000-0099/0005.Longest Palindromic Substring/README.md index cb72a2b4332c6..4febbf23c25b0 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README.md @@ -411,14 +411,12 @@ impl Solution { ``` ```php -class Solution -{ +class Solution { /** * @param string $s * @return string */ - function longestPalindrome($s) - { + function longestPalindrome($s) { $start = 0; $maxLength = 0; @@ -437,8 +435,7 @@ class Solution return substr($s, $start, $maxLength); } - function expandFromCenter($s, $left, $right) - { + function expandFromCenter($s, $left, $right) { while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) { $left--; $right++; diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md index ec657fb12dd3e..7618bff4fb816 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md @@ -405,14 +405,12 @@ impl Solution { ``` ```php -class Solution -{ +class Solution { /** * @param string $s * @return string */ - function longestPalindrome($s) - { + function longestPalindrome($s) { $start = 0; $maxLength = 0; @@ -431,8 +429,7 @@ class Solution return substr($s, $start, $maxLength); } - function expandFromCenter($s, $left, $right) - { + function expandFromCenter($s, $left, $right) { while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) { $left--; $right++; diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php index 8106597ed68da..28007ccedc967 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php +++ b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php @@ -1,12 +1,10 @@ = 0 && $right < strlen($s) && $s[$left] === $s[$right]) { $left--; $right++; diff --git a/solution/0000-0099/0006.Zigzag Conversion/README.md b/solution/0000-0099/0006.Zigzag Conversion/README.md index 9e91e00452a65..d6759dabc9a9d 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README.md @@ -412,16 +412,14 @@ var convert = function (s, numRows) { ``` ```php -class Solution -{ +class Solution { /** * @param string $s * @param int $numRows * @return string */ - function convert($s, $numRows) - { + function convert($s, $numRows) { if ($numRows == 1 || strlen($s) <= $numRows) { return $s; } diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md index 5be1402e316cf..c3e43cc7779a2 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md @@ -408,16 +408,14 @@ var convert = function (s, numRows) { ``` ```php -class Solution -{ +class Solution { /** * @param string $s * @param int $numRows * @return string */ - function convert($s, $numRows) - { + function convert($s, $numRows) { if ($numRows == 1 || strlen($s) <= $numRows) { return $s; } diff --git a/solution/0000-0099/0007.Reverse Integer/README.md b/solution/0000-0099/0007.Reverse Integer/README.md index e249326abcb10..caaa6795a595d 100644 --- a/solution/0000-0099/0007.Reverse Integer/README.md +++ b/solution/0000-0099/0007.Reverse Integer/README.md @@ -194,22 +194,20 @@ int reverse(int x) { ``` ```php -class Solution -{ +class Solution { /** * @param int $x * @return int */ - function reverse($x) - { + function reverse($x) { $isNegative = $x < 0; $x = abs($x); $reversed = 0; while ($x > 0) { - $reversed = $reversed * 10 + $x % 10; + $reversed = $reversed * 10 + ($x % 10); $x = (int) ($x / 10); } diff --git a/solution/0000-0099/0007.Reverse Integer/README_EN.md b/solution/0000-0099/0007.Reverse Integer/README_EN.md index ccab03102970c..63a47483bd050 100644 --- a/solution/0000-0099/0007.Reverse Integer/README_EN.md +++ b/solution/0000-0099/0007.Reverse Integer/README_EN.md @@ -197,22 +197,20 @@ public class Solution { ``` ```php -class Solution -{ +class Solution { /** * @param int $x * @return int */ - function reverse($x) - { + function reverse($x) { $isNegative = $x < 0; $x = abs($x); $reversed = 0; while ($x > 0) { - $reversed = $reversed * 10 + $x % 10; + $reversed = $reversed * 10 + ($x % 10); $x = (int) ($x / 10); } diff --git a/solution/0000-0099/0008.String to Integer (atoi)/README.md b/solution/0000-0099/0008.String to Integer (atoi)/README.md index 6e23a82588d93..b6a7666ce69f8 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/README.md +++ b/solution/0000-0099/0008.String to Integer (atoi)/README.md @@ -276,16 +276,14 @@ public partial class Solution ``` ```php -class Solution -{ +class Solution { /** * @param string $s * @return int */ - function myAtoi($s) - { - $s = str_replace("e", "x", $s); + function myAtoi($s) { + $s = str_replace('e', 'x', $s); if (intval($s) < pow(-2, 31)) { return -2147483648; } diff --git a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md index 45d75db2003b5..509b9fc3d0a6f 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md +++ b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md @@ -271,16 +271,14 @@ public partial class Solution ``` ```php -class Solution -{ +class Solution { /** * @param string $s * @return int */ - function myAtoi($s) - { - $s = str_replace("e", "x", $s); + function myAtoi($s) { + $s = str_replace('e', 'x', $s); if (intval($s) < pow(-2, 31)) { return -2147483648; } diff --git a/solution/0000-0099/0009.Palindrome Number/README.md b/solution/0000-0099/0009.Palindrome Number/README.md index 829b2c76a613c..64912341cadaf 100644 --- a/solution/0000-0099/0009.Palindrome Number/README.md +++ b/solution/0000-0099/0009.Palindrome Number/README.md @@ -212,18 +212,16 @@ impl Solution { ``` ```php -class Solution -{ +class Solution { /** * @param int $x * @return boolean */ - function isPalindrome($x) - { + function isPalindrome($x) { $str = (string) $x; $str_reverse = strrev($str); - return ($str === $str_reverse); + return $str === $str_reverse; } } ``` diff --git a/solution/0000-0099/0009.Palindrome Number/README_EN.md b/solution/0000-0099/0009.Palindrome Number/README_EN.md index f3c4e8e34f879..652c98e07d461 100644 --- a/solution/0000-0099/0009.Palindrome Number/README_EN.md +++ b/solution/0000-0099/0009.Palindrome Number/README_EN.md @@ -202,18 +202,16 @@ impl Solution { ``` ```php -class Solution -{ +class Solution { /** * @param int $x * @return boolean */ - function isPalindrome($x) - { + function isPalindrome($x) { $str = (string) $x; $str_reverse = strrev($str); - return ($str === $str_reverse); + return $str === $str_reverse; } } ``` diff --git a/solution/0000-0099/0010.Regular Expression Matching/README.md b/solution/0000-0099/0010.Regular Expression Matching/README.md index 56649f1a9a54d..cd840d24d6ed6 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/README.md +++ b/solution/0000-0099/0010.Regular Expression Matching/README.md @@ -452,16 +452,14 @@ public class Solution { ``` ```php -class Solution -{ +class Solution { /** * @param string $s * @param string $p * @return boolean */ - function isMatch($s, $p) - { + function isMatch($s, $p) { $m = strlen($s); $n = strlen($p); diff --git a/solution/0000-0099/0010.Regular Expression Matching/README_EN.md b/solution/0000-0099/0010.Regular Expression Matching/README_EN.md index 244439aab5951..2d435fcd70357 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/README_EN.md +++ b/solution/0000-0099/0010.Regular Expression Matching/README_EN.md @@ -449,16 +449,14 @@ public class Solution { ``` ```php -class Solution -{ +class Solution { /** * @param string $s * @param string $p * @return boolean */ - function isMatch($s, $p) - { + function isMatch($s, $p) { $m = strlen($s); $n = strlen($p); diff --git a/solution/0000-0099/0010.Regular Expression Matching/Solution.php b/solution/0000-0099/0010.Regular Expression Matching/Solution.php index a168a3eea58d5..1997c279102f5 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/Solution.php +++ b/solution/0000-0099/0010.Regular Expression Matching/Solution.php @@ -1,14 +1,12 @@ Date: Sat, 20 Jan 2024 09:22:16 +0800 Subject: [PATCH 07/14] Update Solution.php --- .../0004.Median of Two Sorted Arrays/Solution.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php index 3b4e74d46d0d7..60ea3f1c9a029 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php @@ -1,15 +1,11 @@ -class Solution -{ - +class Solution { /** * @param int[] $nums1 * @param int[] $nums2 * @return float */ - function findMedianSortedArrays($nums1, $nums2) - { - + function findMedianSortedArrays($nums1, $nums2) { $arr = array_merge($nums1, $nums2); sort($arr); $cnt_arr = count($arr); From 2bd9e6803f7250293bf9cba564322a8f71abe93e Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 20 Jan 2024 09:24:35 +0800 Subject: [PATCH 08/14] Update Solution.php --- .../0000-0099/0005.Longest Palindromic Substring/Solution.php | 1 - 1 file changed, 1 deletion(-) diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php index 28007ccedc967..854ef5be2cbe8 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php +++ b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php @@ -1,4 +1,3 @@ - Date: Sat, 20 Jan 2024 09:25:00 +0800 Subject: [PATCH 09/14] Update Solution.php --- solution/0000-0099/0006.Zigzag Conversion/Solution.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/solution/0000-0099/0006.Zigzag Conversion/Solution.php b/solution/0000-0099/0006.Zigzag Conversion/Solution.php index ea3750fe4a403..4eb4ee9795308 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/Solution.php +++ b/solution/0000-0099/0006.Zigzag Conversion/Solution.php @@ -1,13 +1,11 @@ -class Solution -{ +class Solution { /** * @param string $s * @param int $numRows * @return string */ - function convert($s, $numRows) - { + function convert($s, $numRows) { if ($numRows == 1 || strlen($s) <= $numRows) { return $s; } From 94da7a28474da40b13b6880c5b5bf1040f3c7d47 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 20 Jan 2024 09:25:32 +0800 Subject: [PATCH 10/14] Update Solution.php --- solution/0000-0099/0007.Reverse Integer/Solution.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/solution/0000-0099/0007.Reverse Integer/Solution.php b/solution/0000-0099/0007.Reverse Integer/Solution.php index 45be68c9c8d4c..ed969b9ba5adf 100644 --- a/solution/0000-0099/0007.Reverse Integer/Solution.php +++ b/solution/0000-0099/0007.Reverse Integer/Solution.php @@ -1,19 +1,17 @@ -class Solution -{ +class Solution { /** * @param int $x * @return int */ - function reverse($x) - { + function reverse($x) { $isNegative = $x < 0; $x = abs($x); $reversed = 0; while ($x > 0) { - $reversed = $reversed * 10 + $x % 10; + $reversed = $reversed * 10 + ($x % 10); $x = (int) ($x / 10); } From 4797edb35ff91f80fd7bae5d775ac379fe23fd4c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 20 Jan 2024 09:25:57 +0800 Subject: [PATCH 11/14] Update Solution.php --- .../0000-0099/0008.String to Integer (atoi)/Solution.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/solution/0000-0099/0008.String to Integer (atoi)/Solution.php b/solution/0000-0099/0008.String to Integer (atoi)/Solution.php index b5c68b3e1a82b..c4943192194ce 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/Solution.php +++ b/solution/0000-0099/0008.String to Integer (atoi)/Solution.php @@ -1,13 +1,11 @@ -class Solution -{ +class Solution { /** * @param string $s * @return int */ - function myAtoi($s) - { - $s = str_replace("e", "x", $s); + function myAtoi($s) { + $s = str_replace('e', 'x', $s); if (intval($s) < pow(-2, 31)) { return -2147483648; } From 460cb8f11dff3707f9ca71ff8d93b78f4264b7e9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 20 Jan 2024 09:26:11 +0800 Subject: [PATCH 12/14] Update Solution.php --- solution/0000-0099/0009.Palindrome Number/Solution.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/solution/0000-0099/0009.Palindrome Number/Solution.php b/solution/0000-0099/0009.Palindrome Number/Solution.php index ce8399b28bf73..9fd8c6654ec95 100644 --- a/solution/0000-0099/0009.Palindrome Number/Solution.php +++ b/solution/0000-0099/0009.Palindrome Number/Solution.php @@ -1,14 +1,12 @@ -class Solution -{ +class Solution { /** * @param int $x * @return boolean */ - function isPalindrome($x) - { + function isPalindrome($x) { $str = (string) $x; $str_reverse = strrev($str); - return ($str === $str_reverse); + return $str === $str_reverse; } } From f1a849af4caa65e6aad22d2b44de0c6435de21da Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 20 Jan 2024 09:26:31 +0800 Subject: [PATCH 13/14] Update Solution.php --- solution/0000-0099/0010.Regular Expression Matching/Solution.php | 1 - 1 file changed, 1 deletion(-) diff --git a/solution/0000-0099/0010.Regular Expression Matching/Solution.php b/solution/0000-0099/0010.Regular Expression Matching/Solution.php index 1997c279102f5..85a7f34fe75da 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/Solution.php +++ b/solution/0000-0099/0010.Regular Expression Matching/Solution.php @@ -1,4 +1,3 @@ - Date: Sat, 20 Jan 2024 09:27:31 +0800 Subject: [PATCH 14/14] Update README_EN.md --- .../0000-0099/0007.Reverse Integer/README_EN.md | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/solution/0000-0099/0007.Reverse Integer/README_EN.md b/solution/0000-0099/0007.Reverse Integer/README_EN.md index 63a47483bd050..f6d980486ee23 100644 --- a/solution/0000-0099/0007.Reverse Integer/README_EN.md +++ b/solution/0000-0099/0007.Reverse Integer/README_EN.md @@ -181,21 +181,6 @@ int reverse(int x) { } ``` -```cs -public class Solution { - public int Reverse(int x) { - int ans = 0; - for (; x != 0; x /= 10) { - if (ans < int.MinValue / 10 || ans > int.MaxValue / 10) { - return 0; - } - ans = ans * 10 + x % 10; - } - return ans; - } -} -``` - ```php class Solution { /**