# LeetCode 200: Number of Islands

``````class Solution {

/**
* @param String[][] \$grid
* @return Integer
*/
function numIslands(\$grid) {
\$count = 0;

for (\$i=0; \$i<count(\$grid); \$i++) {
for (\$j=0; \$j<count(\$grid[\$i]); \$j++) {
if (\$grid[\$i][\$j] == "1") {
\$count++;
// zero out rest of 1s
\$this->zeroOut(\$grid, \$i, \$j);
}
}
}

return \$count;
}

function zeroOut(&\$grid, \$i, \$j) {
if (\$i<0 || \$i>=count(\$grid) || \$j<0 || \$j>=count(\$grid[\$i]) || \$grid[\$i][\$j] == "0")
return;

\$grid[\$i][\$j] = "0";

\$this->zeroOut(\$grid, \$i-1, \$j);
\$this->zeroOut(\$grid, \$i+1, \$j);
\$this->zeroOut(\$grid, \$i, \$j-1);
\$this->zeroOut(\$grid, \$i, \$j+1);
}
}``````

I had to watch some YouTube videos for solution. Initially, I was creating over-complicated solutions to track islands.

# LeetCode 88: Merge Sorted Array solution

``````class Solution {

/**
* @param Integer[] \$nums1
* @param Integer \$m
* @param Integer[] \$nums2
* @param Integer \$n
* @return NULL
*/
function merge(&\$nums1, \$m, \$nums2, \$n) {
if (\$n == 0) {
return;
}

if (\$m == 0) {
for (\$i=0; \$i<\$n; \$i++) {
\$nums1[\$i] = \$nums2[\$i];
}
}

\$i = 0;
\$j = 0;

while (\$i<\$m && \$j<\$n) {
if (\$nums1[\$i]<=\$nums2[\$j]) {
\$i++;
} else {
\$this->array_insert(\$nums1, \$i++, \$nums2[\$j]);
\$m++;
\$j++;
}
}

while (\$j < \$n) {
\$nums1[\$i++] = \$nums2[\$j++];
}

}

function array_insert(&\$arr, \$i, \$num) {
\$endArr = array_slice(\$arr, \$i);
\$arr[\$i] = \$num;

for (\$x=\$i+1; \$x<count(\$arr); \$x++) {
\$arr[\$x] = array_shift(\$endArr);
}
}
}``````

Not very clean but this will do for now. Ref: https://leetcode.com/problems/merge-sorted-array/

# LeetCode 1089: Duplicate Zeros PHP Solution

``````class Solution {

/**
* @param Integer[] \$arr
* @return NULL
*/
function duplicateZeros(&\$arr) {
\$len = count(\$arr);
for (\$i=0; \$i < \$len; \$i++) {
if (0 === \$arr[\$i]) {
array_splice(\$arr, \$i++, 0, 0);
}
}
array_splice(\$arr, \$len);
}
}``````