File tree Expand file tree Collapse file tree 15 files changed +490
-0
lines changed
0041.First Missing Positive Expand file tree Collapse file tree 15 files changed +490
-0
lines changed Original file line number Diff line number Diff line change @@ -222,6 +222,40 @@ void swap(int* a, int* b) {
222222}
223223```
224224
225+ ```php
226+ class Solution {
227+ /**
228+ * @param integer[] $nums
229+ * @return integer
230+ */
231+
232+ function firstMissingPositive($nums) {
233+ $n = count($nums);
234+
235+ for ($i = 0; $i < $n; $i++) {
236+ if ($nums[$i] <= 0) {
237+ $nums[$i] = $n + 1;
238+ }
239+ }
240+
241+ for ($i = 0; $i < $n; $i++) {
242+ $num = abs($nums[$i]);
243+ if ($num <= $n) {
244+ $nums[$num - 1] = -abs($nums[$num - 1]);
245+ }
246+ }
247+
248+ for ($i = 0; $i < $n; $i++) {
249+ if ($nums[$i] > 0) {
250+ return $i + 1;
251+ }
252+ }
253+
254+ return $n + 1;
255+ }
256+ }
257+ ```
258+
225259<!-- tabs:end -->
226260
227261<!-- end -->
Original file line number Diff line number Diff line change @@ -222,6 +222,40 @@ void swap(int* a, int* b) {
222222}
223223```
224224
225+ ```php
226+ class Solution {
227+ /**
228+ * @param integer[] $nums
229+ * @return integer
230+ */
231+
232+ function firstMissingPositive($nums) {
233+ $n = count($nums);
234+
235+ for ($i = 0; $i < $n; $i++) {
236+ if ($nums[$i] <= 0) {
237+ $nums[$i] = $n + 1;
238+ }
239+ }
240+
241+ for ($i = 0; $i < $n; $i++) {
242+ $num = abs($nums[$i]);
243+ if ($num <= $n) {
244+ $nums[$num - 1] = -abs($nums[$num - 1]);
245+ }
246+ }
247+
248+ for ($i = 0; $i < $n; $i++) {
249+ if ($nums[$i] > 0) {
250+ return $i + 1;
251+ }
252+ }
253+
254+ return $n + 1;
255+ }
256+ }
257+ ```
258+
225259<!-- tabs:end -->
226260
227261<!-- end -->
Original file line number Diff line number Diff line change 1+ class Solution {
2+ /**
3+ * @param integer[] $nums
4+ * @return integer
5+ */
6+
7+ function firstMissingPositive ($nums ) {
8+ $n = count ($nums );
9+
10+ for ($i = 0 ; $i < $n ; $i ++ ) {
11+ if ($nums [$i ] <= 0 ) {
12+ $nums [$i ] = $n + 1 ;
13+ }
14+ }
15+
16+ for ($i = 0 ; $i < $n ; $i ++ ) {
17+ $num = abs ($nums [$i ]);
18+ if ($num <= $n ) {
19+ $nums [$num - 1 ] = - abs ($nums [$num - 1 ]);
20+ }
21+ }
22+
23+ for ($i = 0 ; $i < $n ; $i ++ ) {
24+ if ($nums [$i ] > 0 ) {
25+ return $i + 1 ;
26+ }
27+ }
28+
29+ return $n + 1 ;
30+ }
31+ }
Original file line number Diff line number Diff line change @@ -187,6 +187,48 @@ public class Solution {
187187}
188188```
189189
190+ ``` php
191+ class Solution {
192+ /**
193+ * @param integer[] $height
194+ * @return integer
195+ */
196+
197+ function trap($height) {
198+ $n = count($height);
199+
200+ if ($n == 0) {
201+ return 0;
202+ }
203+
204+ $left = 0;
205+ $right = $n - 1;
206+ $leftMax = 0;
207+ $rightMax = 0;
208+ $ans = 0;
209+
210+ while ($left < $right) {
211+ if ($height[$left] < $height[$right]) {
212+ if ($height[$left] > $leftMax) {
213+ $leftMax = $height[$left];
214+ } else {
215+ $ans += $leftMax - $height[$left];
216+ }
217+ $left++;
218+ } else {
219+ if ($height[$right] > $rightMax) {
220+ $rightMax = $height[$right];
221+ } else {
222+ $ans += $rightMax - $height[$right];
223+ }
224+ $right--;
225+ }
226+ }
227+ return $ans;
228+ }
229+ }
230+ ```
231+
190232<!-- tabs: end -->
191233
192234<!-- end -->
Original file line number Diff line number Diff line change @@ -181,6 +181,48 @@ public class Solution {
181181}
182182```
183183
184+ ``` php
185+ class Solution {
186+ /**
187+ * @param integer[] $height
188+ * @return integer
189+ */
190+
191+ function trap($height) {
192+ $n = count($height);
193+
194+ if ($n == 0) {
195+ return 0;
196+ }
197+
198+ $left = 0;
199+ $right = $n - 1;
200+ $leftMax = 0;
201+ $rightMax = 0;
202+ $ans = 0;
203+
204+ while ($left < $right) {
205+ if ($height[$left] < $height[$right]) {
206+ if ($height[$left] > $leftMax) {
207+ $leftMax = $height[$left];
208+ } else {
209+ $ans += $leftMax - $height[$left];
210+ }
211+ $left++;
212+ } else {
213+ if ($height[$right] > $rightMax) {
214+ $rightMax = $height[$right];
215+ } else {
216+ $ans += $rightMax - $height[$right];
217+ }
218+ $right--;
219+ }
220+ }
221+ return $ans;
222+ }
223+ }
224+ ```
225+
184226<!-- tabs: end -->
185227
186228<!-- end -->
Original file line number Diff line number Diff line change 1+ class Solution {
2+ /**
3+ * @param integer[] $height
4+ * @return integer
5+ */
6+
7+ function trap ($height ) {
8+ $n = count ($height );
9+
10+ if ($n == 0 ) {
11+ return 0 ;
12+ }
13+
14+ $left = 0 ;
15+ $right = $n - 1 ;
16+ $leftMax = 0 ;
17+ $rightMax = 0 ;
18+ $ans = 0 ;
19+
20+ while ($left < $right ) {
21+ if ($height [$left ] < $height [$right ]) {
22+ if ($height [$left ] > $leftMax ) {
23+ $leftMax = $height [$left ];
24+ } else {
25+ $ans += $leftMax - $height [$left ];
26+ }
27+ $left ++ ;
28+ } else {
29+ if ($height [$right ] > $rightMax ) {
30+ $rightMax = $height [$right ];
31+ } else {
32+ $ans += $rightMax - $height [$right ];
33+ }
34+ $right -- ;
35+ }
36+ }
37+ return $ans ;
38+ }
39+ }
Original file line number Diff line number Diff line change @@ -263,6 +263,38 @@ public class Solution {
263263}
264264```
265265
266+ ``` php
267+ class Solution {
268+ /**
269+ * @param string $num1
270+ * @param string $num2
271+ * @return string
272+ */
273+
274+ function multiply($num1, $num2) {
275+ $length1 = strlen($num1);
276+ $length2 = strlen($num2);
277+ $product = array_fill(0, $length1 + $length2, 0);
278+
279+ for ($i = $length1 - 1; $i >= 0; $i--) {
280+ for ($j = $length2 - 1; $j >= 0; $j--) {
281+ $digit1 = intval($num1[$i]);
282+ $digit2 = intval($num2[$j]);
283+
284+ $temp = $digit1 * $digit2 + $product[$i + $j + 1];
285+ $product[$i + $j + 1] = $temp % 10;
286+
287+ $carry = intval($temp / 10);
288+ $product[$i + $j] += $carry;
289+ }
290+ }
291+ $result = implode("", $product);
292+ $result = ltrim($result, '0');
293+ return $result === "" ? "0" : $result;
294+ }
295+ }
296+ ```
297+
266298<!-- tabs: end -->
267299
268300<!-- end -->
Original file line number Diff line number Diff line change @@ -254,6 +254,38 @@ public class Solution {
254254}
255255```
256256
257+ ``` php
258+ class Solution {
259+ /**
260+ * @param string $num1
261+ * @param string $num2
262+ * @return string
263+ */
264+
265+ function multiply($num1, $num2) {
266+ $length1 = strlen($num1);
267+ $length2 = strlen($num2);
268+ $product = array_fill(0, $length1 + $length2, 0);
269+
270+ for ($i = $length1 - 1; $i >= 0; $i--) {
271+ for ($j = $length2 - 1; $j >= 0; $j--) {
272+ $digit1 = intval($num1[$i]);
273+ $digit2 = intval($num2[$j]);
274+
275+ $temp = $digit1 * $digit2 + $product[$i + $j + 1];
276+ $product[$i + $j + 1] = $temp % 10;
277+
278+ $carry = intval($temp / 10);
279+ $product[$i + $j] += $carry;
280+ }
281+ }
282+ $result = implode("", $product);
283+ $result = ltrim($result, '0');
284+ return $result === "" ? "0" : $result;
285+ }
286+ }
287+ ```
288+
257289<!-- tabs: end -->
258290
259291<!-- end -->
Original file line number Diff line number Diff line change 1+ class Solution {
2+ /**
3+ * @param string $num1
4+ * @param string $num2
5+ * @return string
6+ */
7+
8+ function multiply ($num1 , $num2 ) {
9+ $length1 = strlen ($num1 );
10+ $length2 = strlen ($num2 );
11+ $product = array_fill (0 , $length1 + $length2 , 0 );
12+
13+ for ($i = $length1 - 1 ; $i >= 0 ; $i -- ) {
14+ for ($j = $length2 - 1 ; $j >= 0 ; $j -- ) {
15+ $digit1 = intval ($num1 [$i ]);
16+ $digit2 = intval ($num2 [$j ]);
17+
18+ $temp = $digit1 * $digit2 + $product [$i + $j + 1 ];
19+ $product [$i + $j + 1 ] = $temp % 10 ;
20+
21+ $carry = intval ($temp / 10 );
22+ $product [$i + $j ] += $carry ;
23+ }
24+ }
25+ $result = implode (" " , $product );
26+ $result = ltrim ($result , ' 0' );
27+ return $result === " " ? " 0" : $result ;
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments