Skip to content

Commit b01144d

Browse files
committed
style: format php code
1 parent 3807c9b commit b01144d

File tree

75 files changed

+321
-180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+321
-180
lines changed

lcof/面试题05. 替换空格/Solution.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ class Solution {
44
* @return String
55
*/
66
function replaceSpace($s) {
7-
$rs = "";
7+
$rs = '';
88
for ($i = 0; $i < strlen($s); $i++) {
9-
if ($s[$i] === " ") $rs = $rs."%20";
10-
else $rs = $rs.$s[$i];
9+
if ($s[$i] === ' ') {
10+
$rs = $rs . '%20';
11+
} else {
12+
$rs = $rs . $s[$i];
13+
}
1114
}
1215
return $rs;
1316
}
14-
}
17+
}

run_format.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,38 @@ def format_py():
7777
os.remove(p2)
7878

7979

80+
def format_php():
81+
for root, _, files in os.walk(path):
82+
for name in files:
83+
if name.endswith('.php'):
84+
p1 = root + '/' + name
85+
with open(p1, 'r', encoding='utf-8') as f:
86+
content = f.read()
87+
content = '<?php\n' + content
88+
with open(p1, 'w', encoding='utf-8') as f:
89+
f.write(content)
90+
command = f'npx prettier --write "**/*.php"'
91+
os.system(command)
92+
for root, _, files in os.walk(path):
93+
for name in files:
94+
if name.endswith('.php'):
95+
p1 = root + '/' + name
96+
with open(p1, 'r', encoding='utf-8') as f:
97+
content = f.read()
98+
content = content.replace('<?php\n', '')
99+
with open(p1, 'w', encoding='utf-8') as f:
100+
f.write(content)
101+
102+
80103
def git_add():
81104
"""Git add all files"""
82105
command = 'git add .'
83106
os.system(command)
84107

85108

86109
if __name__ == '__main__':
87-
clang_format()
88-
prettier_format()
110+
# clang_format()
111+
# prettier_format()
89112
# format_py()
90113
# git_add()
114+
format_php()

solution/0000-0099/0001.Two Sum/Solution.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ function twoSum($nums, $target) {
1313
$hashtable[$x] = $key;
1414
}
1515
}
16-
}
16+
}

solution/0000-0099/0013.Roman to Integer/Solution.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ class Solution {
44
* @return Integer
55
*/
66
function romanToInt($s) {
7-
$hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000);
7+
$hashmap = [
8+
'I' => 1,
9+
'V' => 5,
10+
'X' => 10,
11+
'L' => 50,
12+
'C' => 100,
13+
'D' => 500,
14+
'M' => 1000,
15+
];
816
$rs = 0;
917
for ($i = 0; $i < strlen($s); $i++) {
1018
$left = $hashmap[$s[$i]];
1119
$right = $hashmap[$s[$i + 1]];
12-
if ($left >= $right) $rs += $left;
13-
else $rs -= $left;
20+
if ($left >= $right) {
21+
$rs += $left;
22+
} else {
23+
$rs -= $left;
24+
}
1425
}
1526
return $rs;
1627
}
17-
}
28+
}

solution/0000-0099/0014.Longest Common Prefix/Solution.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ class Solution {
44
* @return String
55
*/
66
function longestCommonPrefix($strs) {
7-
$rs = "";
7+
$rs = '';
88
for ($i = 0; $i < strlen($strs[0]); $i++) {
99
for ($j = 1; $j < count($strs); $j++) {
10-
if ($strs[0][$i] != $strs[$j][$i]) return $rs;
10+
if ($strs[0][$i] != $strs[$j][$i]) {
11+
return $rs;
12+
}
1113
}
12-
$rs = $rs.$strs[0][$i];
14+
$rs = $rs . $strs[0][$i];
1315
}
1416
return $rs;
1517
}
16-
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
class Solution {
2-
32
/**
43
* @param Integer[] $nums
54
* @return Integer
65
*/
76
function removeDuplicates(&$nums) {
87
$k = 0;
9-
foreach($nums as $x) {
8+
foreach ($nums as $x) {
109
if ($k == 0 || $x != $nums[$k - 1]) {
1110
$nums[$k++] = $x;
1211
}
1312
}
1413
return $k;
1514
}
16-
}
15+
}

solution/0000-0099/0027.Remove Element/Solution.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Solution {
2-
32
/**
43
* @param Integer[] $nums
54
* @param Integer $val

solution/0000-0099/0028.Find the Index of the First Occurrence in a String/Solution.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ class Solution {
55
* @return Integer
66
*/
77
function strStr($haystack, $needle) {
8-
$strNew = str_replace($needle, "+", $haystack);
9-
$cnt = substr_count($strNew, "+");
8+
$strNew = str_replace($needle, '+', $haystack);
9+
$cnt = substr_count($strNew, '+');
1010
if ($cnt > 0) {
1111
for ($i = 0; $i < strlen($strNew); $i++) {
12-
if ($strNew[$i] == "+") {
12+
if ($strNew[$i] == '+') {
1313
return $i;
1414
}
1515
}
1616
} else {
1717
return -1;
1818
}
1919
}
20-
}
20+
}

solution/0000-0099/0058.Length of Last Word/Solution.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ class Solution {
55
*/
66
function lengthOfLastWord($s) {
77
$count = 0;
8-
while ($s[strlen($s) - 1] == " ") {
8+
while ($s[strlen($s) - 1] == ' ') {
99
$s = substr($s, 0, -1);
1010
}
11-
while (strlen($s) != 0 && $s[strlen($s) - 1] != " ") {
11+
while (strlen($s) != 0 && $s[strlen($s) - 1] != ' ') {
1212
$count++;
1313
$s = substr($s, 0, -1);
1414
}
1515
return $count;
1616
}
17-
}
17+
}

solution/0000-0099/0070.Climbing Stairs/Solution.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ function climbStairs($n) {
1313
}
1414
return $dp[$n];
1515
}
16-
}
16+
}

solution/0000-0099/0088.Merge Sorted Array/Solution.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ function merge(&$nums1, $m, $nums2, $n) {
1515
}
1616
asort($nums1);
1717
}
18-
}
18+
}

solution/0100-0199/0100.Same Tree/Solution.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ class Solution {
1818
* @return Boolean
1919
*/
2020
function isSameTree($p, $q) {
21-
if ($p == Null && $q == Null) {
21+
if ($p == null && $q == null) {
2222
return true;
2323
}
24-
if ($p == Null || $q == Null) {
24+
if ($p == null || $q == null) {
2525
return false;
2626
}
2727
if ($p->val != $q->val) {
2828
return false;
2929
}
30-
return $this->isSameTree($p->left, $q->left) && $this->isSameTree($p->right, $q->right);
30+
return $this->isSameTree($p->left, $q->left) &&
31+
$this->isSameTree($p->right, $q->right);
3132
}
32-
}
33+
}

solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ function maxProfit($prices) {
1313
}
1414
return $win;
1515
}
16-
}
16+
}

solution/0100-0199/0125.Valid Palindrome/Solution.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ class Solution {
44
* @return Boolean
55
*/
66
function isPalindrome($s) {
7-
$regex = "/[a-z0-9]/";
7+
$regex = '/[a-z0-9]/';
88
$s = strtolower($s);
99
preg_match_all($regex, $s, $matches);
10-
if ($matches[0] == Null) return true;
10+
if ($matches[0] == null) {
11+
return true;
12+
}
1113
$len = floor(count($matches[0]) / 2);
1214
for ($i = 0; $i < $len; $i++) {
1315
if ($matches[0][$i] != $matches[0][count($matches[0]) - 1 - $i]) {
@@ -16,4 +18,4 @@ function isPalindrome($s) {
1618
}
1719
return true;
1820
}
19-
}
21+
}

solution/0100-0199/0169.Majority Element/Solution.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Solution {
2-
32
/**
43
* @param Integer[] $nums
54
* @return Integer
@@ -19,4 +18,4 @@ function majorityElement($nums) {
1918
}
2019
return $m;
2120
}
22-
}
21+
}

solution/0200-0299/0217.Contains Duplicate/Solution.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ function containsDuplicate($nums) {
77
$numsUnique = array_unique($nums);
88
return count($nums) != count($numsUnique);
99
}
10-
}
10+
}

solution/0200-0299/0229.Majority Element II/Solution.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ function majorityElement($nums) {
88
$n = count($nums);
99
for ($i = 0; $i < $n; $i++) {
1010
$hashmap[$nums[$i]] += 1;
11-
if ($hashmap[$nums[$i]] > $n / 3)
11+
if ($hashmap[$nums[$i]] > $n / 3) {
1212
array_push($rs, $nums[$i]);
13+
}
1314
}
1415
return array_unique($rs);
1516
}
16-
}
17+
}

solution/0200-0299/0263.Ugly Number/Solution.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ class Solution {
55
*/
66
function isUgly($n) {
77
while ($n) {
8-
if ($n % 2 == 0) $n = $n / 2;
9-
else if ($n % 3 == 0) $n = $n / 3;
10-
else if ($n % 5 == 0) $n = $n / 5;
11-
else break;
8+
if ($n % 2 == 0) {
9+
$n = $n / 2;
10+
} elseif ($n % 3 == 0) {
11+
$n = $n / 3;
12+
} elseif ($n % 5 == 0) {
13+
$n = $n / 5;
14+
} else {
15+
break;
16+
}
1217
}
1318
return $n == 1;
1419
}
15-
}
20+
}

solution/0200-0299/0268.Missing Number/Solution.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ class Solution {
55
*/
66
function missingNumber($nums) {
77
$n = count($nums);
8-
$sumN = ($n + 1) * $n / 2;
8+
$sumN = (($n + 1) * $n) / 2;
99
for ($i = 0; $i < $n; $i++) {
1010
$sumN -= $nums[$i];
1111
}
1212
return $sumN;
1313
}
14-
}
14+
}

solution/0200-0299/0299.Bulls and Cows/Solution.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ function getHint($secret, $guess) {
99
$cntB = 0;
1010
$len = strlen($secret);
1111
for ($i = 0; $i < $len; $i++) {
12-
if ($secret[$i] == $guess[$i]) $cntA++;
13-
else $hashtable[$secret[$i]] += 1;
12+
if ($secret[$i] == $guess[$i]) {
13+
$cntA++;
14+
} else {
15+
$hashtable[$secret[$i]] += 1;
16+
}
1417
}
1518
for ($i = 0; $i < $len; $i++) {
1619
if ($secret[$i] != $guess[$i] && $hashtable[$guess[$i]] > 0) {
1720
$cntB++;
1821
$hashtable[$guess[$i]] -= 1;
1922
}
2023
}
21-
return $cntA."A".$cntB."B";
24+
return $cntA . 'A' . $cntB . 'B';
2225
}
23-
}
26+
}

solution/0300-0399/0349.Intersection of Two Arrays/Solution.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ function intersection($nums1, $nums2) {
1212
$hashmap[$set1[$i]] = 1;
1313
}
1414
for ($j = 0; $j < count($set2); $j++) {
15-
if ($hashmap[$set2[$j]]) array_push($rs, $set2[$j]);
15+
if ($hashmap[$set2[$j]]) {
16+
array_push($rs, $set2[$j]);
17+
}
1618
}
1719
return $rs;
1820
}
19-
}
21+
}

solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ function intersect($nums1, $nums2) {
1717
}
1818
return $rs;
1919
}
20-
}
20+
}

solution/0300-0399/0383.Ransom Note/Solution.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ function canConstruct($ransomNote, $magazine) {
1010
$hashtable[$arrM[$i]] += 1;
1111
}
1212
for ($j = 0; $j < strlen($ransomNote); $j++) {
13-
if (!isset($hashtable[$ransomNote[$j]]) || $hashtable[$ransomNote[$j]] == 0) return false;
14-
else $hashtable[$ransomNote[$j]] -= 1;
13+
if (
14+
!isset($hashtable[$ransomNote[$j]]) ||
15+
$hashtable[$ransomNote[$j]] == 0
16+
) {
17+
return false;
18+
} else {
19+
$hashtable[$ransomNote[$j]] -= 1;
20+
}
1521
}
1622
return true;
1723
}
18-
}
24+
}

solution/0300-0399/0387.First Unique Character in a String/Solution.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ function firstUniqChar($s) {
1414
}
1515
return -1;
1616
}
17-
}
17+
}

0 commit comments

Comments
 (0)