Skip to content

Apply fixes from StyleCI #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/leetcode/AddToArrayFormOfInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public static function addToArrayForm(array $nums, int $k): array
[$ans, $n] = [[], count($nums)];
for ($i = $n - 1; $i >= 0; $i--) {
$sum = $nums[$i] + $k % 10;
$k = (int)($k / 10);
$k = (int) ($k / 10);
if ($sum >= 10) {
$k++;
$sum -= 10;
}
$ans[] = (int)$sum;
$ans[] = (int) $sum;
}
for (; $k > 0; $k = (int)($k /= 10)) {
for (; $k > 0; $k = (int) ($k /= 10)) {
$ans[] = $k % 10;
}

Expand All @@ -36,11 +36,11 @@ public static function addToArrayForm2(array $nums, int $k): array
$n = count($nums);
for ($i = $n - 1; $i >= 0; $i--) {
$ans[] = ($nums[$i] + $k) % 10;
$k = (int)(($nums[$i] + $k) / 10);
$k = (int) (($nums[$i] + $k) / 10);
}
while ($k > 0) {
$ans[] = $k % 10;
$k = (int)($k / 10);
$k = (int) ($k / 10);
}

return array_reverse($ans);
Expand All @@ -54,7 +54,7 @@ public static function addToArrayForm3(array $nums, int $k): array
$n = count($nums);
for ($i = $n - 1; $i >= 0 || $k > 0; $i--) {
$ans[] = ($i >= 0 ? $nums[$i] + $k : $k) % 10;
$k = (int)(($i >= 0 ? $nums[$i] + $k : $k) / 10);
$k = (int) (($i >= 0 ? $nums[$i] + $k : $k) / 10);
}

return array_reverse($ans);
Expand Down
8 changes: 4 additions & 4 deletions src/leetcode/AndroidUnlockPatterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ private static function dfs(
int $ans
) {
if ($len >= $m) {
++$ans;
$ans++;
}
++$len;
$len++;
if ($len > $n) {
return $ans;
}
$visited[$curr] = true;
for ($next = 1; $next < 10; $next++) {
$jump = $jumps[$curr][$next];
if (! $visited[$next] && ($jump === 0 || $visited[$jump])) {
if (!$visited[$next] && ($jump === 0 || $visited[$jump])) {
$ans = self::dfs($next, $len, $m, $n, $jumps, $visited, $ans);
}
}
Expand All @@ -90,7 +90,7 @@ private static function dfs2(
$visited[$curr] = true;
for ($next = 1; $next < 10; $next++) {
$jump = $jumps[$curr][$next];
if (! $visited[$next] && ($jump === 0 || $visited[$jump])) {
if (!$visited[$next] && ($jump === 0 || $visited[$jump])) {
$ans += self::dfs2($next, $jumps, $visited, $remain - 1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/leetcode/BestTimeToBuyAndSellStockII.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function maxProfit3(array $prices): int
return 0;
}
$ans = [];
for ($i = $n - 2; $i >= 0; --$i) {
for ($i = $n - 2; $i >= 0; $i--) {
$ans[] = max($prices[$i + 1] - $prices[$i], 0);
}

Expand All @@ -61,7 +61,7 @@ public static function maxProfit4(array $prices): int
return 0;
}
$ans = 0;
for ($i = 1; $i < $n; ++$i) {
for ($i = 1; $i < $n; $i++) {
$ans += max(0, $prices[$i] - $prices[$i - 1]);
}

Expand Down
8 changes: 4 additions & 4 deletions src/leetcode/BestTimeToBuyAndSellStockIII.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static function maxProfit(array $prices): int
}
$dp = array_fill(0, $n, array_fill(0, $k, [0, 0, 0]));
foreach ($prices as $i => $price) {
for ($j = 1; $j < $k; ++$j) {
for ($j = 1; $j < $k; $j++) {
if ($i - 1 < 0) {
$dp[$i][$j][0] = 0;
$dp[$i][$j][1] = -$price;
Expand Down Expand Up @@ -53,17 +53,17 @@ public static function maxProfit3(array $prices): int
}
[$x, $y] = [array_pad([], $n, 0), array_pad([], $n, 0)];

for ($i = 1, $min = $prices[0]; $i < $n; ++$i) {
for ($i = 1, $min = $prices[0]; $i < $n; $i++) {
$min = min($min, $prices[$i]);
$x[$i] = max($x[$i - 1], $prices[$i] - $min);
}

for ($i = $n - 2, $max = $prices[$n - 1]; $i > 0; --$i) {
for ($i = $n - 2, $max = $prices[$n - 1]; $i > 0; $i--) {
$max = max($max, $prices[$i]);
$y[$i] = max($y[$i + 1], $max - $prices[$i]);
}

for ($i = 0; $i < $n; ++$i) {
for ($i = 0; $i < $n; $i++) {
$ans = max($ans, $x[$i] + $y[$i]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/leetcode/BestTimeToBuyAndSellStockIV.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function maxProfit(int $k, array $prices)

$dp = array_fill(0, $n, array_fill(0, $n, [0, 0, 0]));
foreach ($prices as $i => $price) {
for ($j = $k; $j > 0; --$j) {
for ($j = $k; $j > 0; $j--) {
if ($i - 1 < 0) {
$dp[$i][$j][0] = 0;
$dp[$i][$j][1] = -$price;
Expand All @@ -50,7 +50,7 @@ public static function maxProfit2(int $k, array $prices): int
}
$helper = static function () use ($prices) {
[$n, $maxProfit] = [count($prices), 0];
for ($i = 1; $i < $n; ++$i) {
for ($i = 1; $i < $n; $i++) {
if ($prices[$i] > $prices[$i - 1]) {
$maxProfit += max(0, $prices[$i] - $prices[$i - 1]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/leetcode/ClimbingStairs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function climbStairs(int $n): int
}

$dp = [1 => 1, 2 => 2];
for ($i = 3; $i <= $n; ++$i) {
for ($i = 3; $i <= $n; $i++) {
$dp[$i] = $dp[$i - 1] + $dp[$i - 2];
}

Expand All @@ -32,7 +32,7 @@ public static function climbStairs2(int $n): int
return $n;
}
[$prev, $curr] = [1, 1];
for ($i = 2; $i < $n + 1; ++$i) {
for ($i = 2; $i < $n + 1; $i++) {
$temp = $curr;
$curr = $prev + $curr;
$prev = $temp;
Expand Down
8 changes: 3 additions & 5 deletions src/leetcode/CoinChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace leetcode;

use SplFixedArray;

class CoinChange
{
public static function coinChange(array $coins, int $amount): int
Expand All @@ -15,8 +13,8 @@ public static function coinChange(array $coins, int $amount): int
return 0;
}
[$dp, $dp[0]] = [array_fill(0, $m, $m), 0];
for ($i = 1; $i < $m; ++$i) {
for ($j = 0; $j < $n; ++$j) {
for ($i = 1; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
$coin = $coins[$j];
if ($coin <= $i) {
$dp[$i] = min($dp[$i], $dp[$i - $coin] + 1);
Expand All @@ -34,7 +32,7 @@ public static function coinChange2(array $coins, int $amount): int
return 0;
}
[$dp, $max] = [array_fill(0, $m, 0), PHP_INT_MAX];
for ($i = 1; $i < $m; ++$i) {
for ($i = 1; $i < $m; $i++) {
$dp[$i] = $max;
foreach ($coins as $coin) {
if ($coin <= $i && $dp[$i - $coin] !== $max) {
Expand Down
3 changes: 1 addition & 2 deletions src/leetcode/CountNumbersWithUniqueDigits.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ private static function dfs(int $target, int $index, array &$visited): int
}
$count = 0;
for ($i = $index ? 0 : 1; $i < 10; $i++) {
if (! $visited[$i]) {
if (!$visited[$i]) {
$visited[$i] = true;
$count += self::dfs($target, $index + 1, $visited);
$visited[$i] = false;
}
}

return $count;

}
}
6 changes: 3 additions & 3 deletions src/leetcode/DecodeWays.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public static function numDecodings(string $s): int

$dp = array_fill(0, $n + 1, 0);
[$dp[0], $dp[1]] = [1, 1];
for ($i = 2; $i <= $n; ++$i) {
[$p, $q] = [(int)$s[$i - 1], (int)$s[$i - 2]];
for ($i = 2; $i <= $n; $i++) {
[$p, $q] = [(int) $s[$i - 1], (int) $s[$i - 2]];
$dp[$i] = $p === 0 ? 0 : $dp[$i - 1];
if ($q === 1 || ($q === 2 && $p <= 6)) {
$dp[$i] += $dp[$i - 2];
Expand All @@ -42,7 +42,7 @@ public static function numDecodings2(string $s): int

$dp = array_fill(0, $n + 1, 0);
[$dp[0], $dp[1]] = [1, 1];
for ($i = 2; $i <= $n; ++$i) {
for ($i = 2; $i <= $n; $i++) {
$p = $helper($s, $i - 1, $i);
$q = $helper($s, $i - 2, $i);
if ($p >= 1 && $p <= 9) {
Expand Down
4 changes: 2 additions & 2 deletions src/leetcode/DegreeOfAnArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public static function findShortestSubArray(array $nums): int
$cnt = $map = [];
$res = $degree = 0;
foreach ($nums as $k => $v) {
if (! isset($map[$v])) {
if (!isset($map[$v])) {
$map[$v] = $k;
}
if (! isset($cnt[$v])) {
if (!isset($cnt[$v])) {
$cnt[$v] = 0;
}
$cnt[$v]++;
Expand Down
6 changes: 3 additions & 3 deletions src/leetcode/DivisorGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public static function divisorGame2(int $n): bool
$dp = array_fill(0, $n + 1, false);
$dp[1] = false;
$dp[2] = true;
for ($i = 3; $i <= $n; ++$i) {
for ($j = 1; $j < $i; ++$j) {
if ($i % $j === 0 && ! $dp[$i - $j]) {
for ($i = 3; $i <= $n; $i++) {
for ($j = 1; $j < $i; $j++) {
if ($i % $j === 0 && !$dp[$i - $j]) {
$dp[$i] = true;
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/leetcode/FairCandySwap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static function fairCandySwap(array $a, array $b): array
return [];
}
[$aSum, $bSum] = [array_sum($a), array_sum($b)];
$dif = (int)(($aSum - $bSum) / 2);
$dif = (int) (($aSum - $bSum) / 2);
for ($i = 0, $m = count($a); $i < $m; $i++) {
for ($j = 0, $n = count($b); $j < $n; $j++) {
if ($a[$i] - $b[$j] === $dif) {
Expand All @@ -29,9 +29,9 @@ public static function fairCandySwap2(array $a, array $b): array
if (empty($a) || empty($b)) {
return [];
}
[$map, $dif] = [[], (int)((array_sum($a) - array_sum($b)) / 2)];
[$map, $dif] = [[], (int) ((array_sum($a) - array_sum($b)) / 2)];
foreach ($a as $v) {
if (! isset($map[$v])) {
if (!isset($map[$v])) {
$map[$v] = $v;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/leetcode/FindPivotIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static function pivotIndex(array $nums): int
if (empty($nums)) {
return 0;
}
[$left, $right]= [0, array_sum($nums)];
[$left, $right] = [0, array_sum($nums)];
foreach ($nums as $key => $num) {
$right -= $num;
if ($left === $right) {
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/GenerateParentheses.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private static function addParenthesis(array &$ans, int $left, int $right, strin
{
if ($left === 0 && $right === 0) {
$ans[] = $s;

return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/leetcode/HouseRobber.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function rob3(array $nums): int
$dp = array_fill(0, $n, 0);
$dp[0] = $nums[0];
$dp[1] = max($nums[0], $nums[1]);
for ($i = 2; $i < $n; ++$i) {
for ($i = 2; $i < $n; $i++) {
$dp[$i] = max($dp[$i - 1], $dp[$i - 2] + $nums[$i]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/leetcode/ImageSmoother.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function imageSmoother(array $arr): array
}
}
if ($cnt > 0) {
$ans[$i][$j] = (int)floor($ans[$i][$j] / $cnt);
$ans[$i][$j] = (int) floor($ans[$i][$j] / $cnt);
}
}
}
Expand Down Expand Up @@ -74,7 +74,7 @@ public static function imageSmoother2(array $arr): array
if (isset($arr[$i + 1][$j + 1])) {
$tmp[] = $arr[$i + 1][$j + 1];
}
$tmp = (int)floor(array_sum($tmp) / count($tmp));
$tmp = (int) floor(array_sum($tmp) / count($tmp));
$res[$i][$j] = $tmp;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/leetcode/ImplementQueueUsingStacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ public function push(int $value): void
if (empty($this->stack)) {
$this->front = $value;
}
while (! empty($this->stack)) {
while (!empty($this->stack)) {
$this->cache[] = array_shift($this->stack);
}
$this->cache[] = $value;
while (! empty($this->cache)) {
while (!empty($this->cache)) {
$this->stack[] = array_shift($this->cache);
}
}

public function pop(): ?int
{
$value = array_shift($this->stack);
if (! empty($this->stack)) {
if (!empty($this->stack)) {
$this->front = current($this->stack);
}

Expand Down
2 changes: 1 addition & 1 deletion src/leetcode/ImplementQueueUsingStacks2.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function pop(): ?int
public function peek(): ?int
{
if (empty($this->stack)) {
while (! empty($this->cache)) {
while (!empty($this->cache)) {
$this->stack[] = array_shift($this->cache);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/leetcode/ImplementTriePrefixTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class ImplementTriePrefixTree
{
/** @var \leetcode\util\TrieNode $root */
/** @var \leetcode\util\TrieNode */
private $root;

public function __construct()
Expand All @@ -22,20 +22,20 @@ public function insert(string $word): void
$node = $this->root;
for ($i = 0, $n = strlen($word); $i < $n; $i++) {
$value = $word[$i];
if (! isset($node->children[$value])) {
if (!isset($node->children[$value])) {
$node->children[$value] = new TrieNode($value);
}
$node = $node->children[$value];
}
$node->isWord = true;
}

public function search (string $prefix): bool
public function search(string $prefix): bool
{
$node = $this->root;
for ($i = 0, $n = strlen($prefix); $i < $n; $i++) {
$value = $prefix[$i];
if (! isset($node->children[$value])) {
if (!isset($node->children[$value])) {
return false;
}
$node = $node->children[$value];
Expand All @@ -46,10 +46,10 @@ public function search (string $prefix): bool

public function startsWith(string $prefix): bool
{
$node= $this->root;
$node = $this->root;
for ($i = 0, $n = strlen($prefix); $i < $n; $i++) {
$value = $prefix[$i];
if (! isset($node->children[$value])) {
if (!isset($node->children[$value])) {
return false;
}
$node = $node->children[$value];
Expand Down
Loading