Skip to content

Commit fc963c1

Browse files
authored
Improved tasks 6-149
1 parent 61e2662 commit fc963c1

File tree

20 files changed

+80
-84
lines changed

20 files changed

+80
-84
lines changed

src/main/python/g0001_0100/s0006_zigzag_conversion/Solution0006.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
# #2025_07_22_Time_5_ms_(93.09%)_Space_17.85_MB_(70.00%)
33

44
class Solution:
5-
def convert(self, s: str, numRows: int) -> str:
5+
def convert(self, s: str, num_rows: int) -> str:
66
s_len = len(s)
7-
if numRows == 1:
7+
if num_rows == 1:
88
return s
99

10-
max_dist = numRows * 2 - 2
10+
max_dist = num_rows * 2 - 2
1111
buf = []
1212

13-
for i in range(numRows):
13+
for i in range(num_rows):
1414
index = i
15-
if i == 0 or i == numRows - 1:
15+
if i == 0 or i == num_rows - 1:
1616
while index < s_len:
1717
buf.append(s[index])
1818
index += max_dist

src/main/python/g0001_0100/s0009_palindrome_number/Solution0009_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
class SolutionTest(unittest.TestCase):
55
def test_isPalindrome(self):
6-
self.assertEqual(Solution().isPalindrome(121), True)
6+
self.assertTrue(Solution().isPalindrome(121))
77

88
def test_isPalindrome2(self):
9-
self.assertEqual(Solution().isPalindrome(-121), False)
9+
self.assertFalse(Solution().isPalindrome(-121))
1010

1111
def test_isPalindrome3(self):
12-
self.assertEqual(Solution().isPalindrome(10), False)
12+
self.assertFalse(Solution().isPalindrome(10))

src/main/python/g0001_0100/s0010_regular_expression_matching/Solution0010_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
class SolutionTest(unittest.TestCase):
55
def test_isMatch(self):
6-
self.assertEqual(Solution().isMatch("aa", "a"), False)
6+
self.assertFalse(Solution().isMatch("aa", "a"))
77

88
def test_isMatch2(self):
9-
self.assertEqual(Solution().isMatch("aa", "a*"), True)
9+
self.assertTrue(Solution().isMatch("aa", "a*"))
1010

1111
def test_isMatch3(self):
12-
self.assertEqual(Solution().isMatch("ab", ".*"), True)
12+
self.assertTrue(Solution().isMatch("ab", ".*"))
1313

1414
def test_isMatch4(self):
15-
self.assertEqual(Solution().isMatch("aab", "c*a*b"), True)
15+
self.assertTrue(Solution().isMatch("aab", "c*a*b"))
1616

1717
def test_isMatch5(self):
18-
self.assertEqual(Solution().isMatch("mississippi", "mis*is*p*."), False)
18+
self.assertFalse(Solution().isMatch("mississippi", "mis*is*p*."))

src/main/python/g0001_0100/s0035_search_insert_position/Solution0035.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ def searchInsert(self, nums: List[int], target: int) -> int:
1818

1919
elif guess < target:
2020
low = mid + 1
21-
if nums[-1] == guess:
21+
if nums[-1] == guess or nums[low] > target:
2222
return mid + 1
23-
elif nums[low] > target:
24-
return mid + 1
2523
else:
2624
high = mid - 1
2725

src/main/python/g0001_0100/s0051_n_queens/Solution0051.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def helper(self, n: int, row: int, pos: List[bool], pos2: List[int], ans: List[L
3131
def construct(self, n: int, pos: List[int], ans: List[List[str]]):
3232
sol = []
3333
for r in range(n):
34-
queenRow = ['.'] * n
35-
queenRow[pos[r]] = 'Q'
36-
sol.append(''.join(queenRow))
34+
queen_row = ['.'] * n
35+
queen_row[pos[r]] = 'Q'
36+
sol.append(''.join(queen_row))
3737
ans.append(sol)

src/main/python/g0001_0100/s0052_n_queens_ii/Solution0052.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ def totalNQueens(self, n: int) -> int:
66
row = [False] * n
77
col = [False] * n
88
diagonal = [False] * (2 * n - 1)
9-
antiDiagonal = [False] * (2 * n - 1)
10-
return self._totalNQueens(n, 0, row, col, diagonal, antiDiagonal)
9+
anti_diagonal = [False] * (2 * n - 1)
10+
return self._totalNQueens(n, 0, row, col, diagonal, anti_diagonal)
1111

12-
def _totalNQueens(self, n, r, row, col, diagonal, antiDiagonal):
12+
def _totalNQueens(self, n, r, row, col, diagonal, anti_diagonal):
1313
if r == n:
1414
return 1
1515
count = 0
1616
for c in range(n):
17-
if not row[r] and not col[c] and not diagonal[r + c] and not antiDiagonal[r - c + n - 1]:
18-
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = True
19-
count += self._totalNQueens(n, r + 1, row, col, diagonal, antiDiagonal)
20-
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = False
17+
if not row[r] and not col[c] and not diagonal[r + c] and not anti_diagonal[r - c + n - 1]:
18+
row[r] = col[c] = diagonal[r + c] = anti_diagonal[r - c + n - 1] = True
19+
count += self._totalNQueens(n, r + 1, row, col, diagonal, anti_diagonal)
20+
row[r] = col[c] = diagonal[r + c] = anti_diagonal[r - c + n - 1] = False
2121
return count

src/main/python/g0001_0100/s0057_insert_interval/Solution0057.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
from typing import List
55

66
class Solution:
7-
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
7+
def insert(self, intervals: List[List[int]], new_interval: List[int]) -> List[List[int]]:
88
n = len(intervals)
99
l = 0
1010
r = n - 1
11-
while l < n and newInterval[0] > intervals[l][1]:
11+
while l < n and new_interval[0] > intervals[l][1]:
1212
l += 1
13-
while r >= 0 and newInterval[1] < intervals[r][0]:
13+
while r >= 0 and new_interval[1] < intervals[r][0]:
1414
r -= 1
1515
res = [[0, 0] for _ in range(l + n - r)]
1616
for i in range(l):
1717
res[i] = intervals[i][:]
18-
res[l][0] = min(newInterval[0], newInterval[0] if l == n else intervals[l][0])
19-
res[l][1] = max(newInterval[1], newInterval[1] if r == -1 else intervals[r][1])
18+
res[l][0] = min(new_interval[0], new_interval[0] if l == n else intervals[l][0])
19+
res[l][1] = max(new_interval[1], new_interval[1] if r == -1 else intervals[r][1])
2020
for i in range(l + 1, l + n - r):
2121
j = r + 1 + i - l - 1
2222
res[i] = intervals[j][:]

src/main/python/g0001_0100/s0061_rotate_list/Solution0061.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
2424
return head
2525
temp = head
2626
# iterate and go to the K+1 th node from the end or count - K - 1 node from start
27-
for i in range(1, count - times):
27+
for _ in range(1, count - times):
2828
if temp is not None:
2929
temp = temp.next
3030
new_head = None

src/main/python/g0001_0100/s0063_unique_paths_ii/Solution0063.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
from typing import List
55

66
class Solution:
7-
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
7+
def uniquePathsWithObstacles(self, obstacle_grid: List[List[int]]) -> int:
88
# if start point has obstacle, there's no path
9-
if obstacleGrid[0][0] == 1:
9+
if obstacle_grid[0][0] == 1:
1010
return 0
11-
obstacleGrid[0][0] = 1
12-
m = len(obstacleGrid)
13-
n = len(obstacleGrid[0])
11+
obstacle_grid[0][0] = 1
12+
m = len(obstacle_grid)
13+
n = len(obstacle_grid[0])
1414
for i in range(1, m):
15-
if obstacleGrid[i][0] == 1:
16-
obstacleGrid[i][0] = 0
15+
if obstacle_grid[i][0] == 1:
16+
obstacle_grid[i][0] = 0
1717
else:
18-
obstacleGrid[i][0] = obstacleGrid[i - 1][0]
18+
obstacle_grid[i][0] = obstacle_grid[i - 1][0]
1919
for j in range(1, n):
20-
if obstacleGrid[0][j] == 1:
21-
obstacleGrid[0][j] = 0
20+
if obstacle_grid[0][j] == 1:
21+
obstacle_grid[0][j] = 0
2222
else:
23-
obstacleGrid[0][j] = obstacleGrid[0][j - 1]
23+
obstacle_grid[0][j] = obstacle_grid[0][j - 1]
2424
for i in range(1, m):
2525
for j in range(1, n):
26-
if obstacleGrid[i][j] == 1:
27-
obstacleGrid[i][j] = 0
26+
if obstacle_grid[i][j] == 1:
27+
obstacle_grid[i][j] = 0
2828
else:
29-
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]
30-
return obstacleGrid[m - 1][n - 1]
29+
obstacle_grid[i][j] = obstacle_grid[i - 1][j] + obstacle_grid[i][j - 1]
30+
return obstacle_grid[m - 1][n - 1]

src/main/python/g0001_0100/s0068_text_justification/Solution0068.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import List
55

66
class Solution:
7-
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
7+
def fullJustify(self, words: List[str], max_width: int) -> List[str]:
88
# Trying to gauge the number of lines so the list doesn't need to resize
99
output = []
1010
# Setting string capacity also
@@ -18,18 +18,18 @@ def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
1818
# tracking line length + #words
1919
num_words_on_line += 1
2020
# checking if the next word causes an overflow
21-
if line_total + num_words_on_line + len(words[i + 1]) > maxWidth:
21+
if line_total + num_words_on_line + len(words[i + 1]) > max_width:
2222
# if only one word fits on the line...
2323
if num_words_on_line == 1:
2424
# append word
2525
sb.append(words[i])
2626
# pad right with spaces
27-
while line_total < maxWidth:
27+
while line_total < max_width:
2828
sb.append(' ')
2929
line_total += 1
3030
else:
3131
# of extra spaces
32-
extra_sp = (maxWidth - line_total) % (num_words_on_line - 1)
32+
extra_sp = (max_width - line_total) % (num_words_on_line - 1)
3333
# Creating the line
3434
for j in range(start_word, start_word + num_words_on_line - 1):
3535
# appending the word
@@ -39,7 +39,7 @@ def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
3939
sb.append(' ')
4040
extra_sp -= 1
4141
# appending the rest of the required spaces
42-
max_spaces = max(0, (maxWidth - line_total) // (num_words_on_line - 1))
42+
max_spaces = max(0, (max_width - line_total) // (num_words_on_line - 1))
4343
sb.append(' ' * max_spaces)
4444
# appending the last word of the line
4545
sb.append(words[start_word + num_words_on_line - 1])
@@ -57,11 +57,11 @@ def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
5757
for i in range(start_word, len(words)):
5858
line_total += len(words[i])
5959
sb.append(words[i])
60-
if line_total < maxWidth:
60+
if line_total < max_width:
6161
sb.append(' ')
6262
line_total += 1
6363
# padding right side with spaces
64-
while line_total < maxWidth:
64+
while line_total < max_width:
6565
sb.append(' ')
6666
line_total += 1
6767
# add the final line to output list

0 commit comments

Comments
 (0)