for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace leetcode;
class MissingRanges
{
public static function findMissingRanges(array &$nums, int $lower, int $upper): array
$ans = [];
if (empty($nums)) {
return $ans;
}
foreach ($nums as $num) {
if ($num > $lower) {
$ans[] = $lower . ($num - 1 > $lower ? ('->' . ($num - 1)) : '');
if ($num === $upper) {
$lower = $num + 1;
if ($lower <= $upper) {
$ans[] = $lower . ($upper > $lower ? ('->' . $upper) : '');
public static function findMissingRanges2(array &$nums, int $lower, int $upper): array
$next = $lower;
if ($lower === PHP_INT_MAX) {
if ($num < $next) {
continue;
if ($num === $next) {
$next++;
$ans[] = self::getRange($next, $num - 1);
if ($num === PHP_INT_MAX) {
$next = $num + 1;
if ($next <= $upper) {
$ans[] = self::getRange($next, $upper);
private static function getRange(int $a, int $b): string
return $a === $b ? (string) $a : sprintf('%d->%d', $a, $b);