Conditions | 14 |
Paths | 4 |
Total Lines | 31 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
46 | public static function threeSum2(array $nums): array |
||
47 | { |
||
48 | [$ans, $n] = [[], count($nums)]; |
||
49 | if (empty($nums) || $n < 3) { |
||
50 | return $ans; |
||
51 | } |
||
52 | sort($nums); |
||
53 | for ($i = 0; $i < $n - 2; $i++) { |
||
54 | if ($i === 0 || ($i > 0 && $nums[$i] !== $nums[$i - 1])) { |
||
55 | [$low, $high, $sum] = [$i + 1, $n - 1, -$nums[$i]]; |
||
56 | while ($low < $high) { |
||
57 | if ($nums[$low] + $nums[$high] === $sum) { |
||
58 | $ans[] = [$nums[$i], $nums[$low], $nums[$high]]; |
||
59 | while ($low < $high && $nums[$low] === $nums[$low + 1]) { |
||
60 | $low++; |
||
61 | } |
||
62 | while ($low < $high && $nums[$high] === $nums[$high - 1]) { |
||
63 | $high--; |
||
64 | } |
||
65 | $low++; |
||
66 | $high--; |
||
67 | } elseif ($nums[$low] + $nums[$high] < $sum) { |
||
68 | $low++; |
||
69 | } else { |
||
70 | $high--; |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | |||
76 | return $ans; |
||
77 | } |
||
116 |