refactor(practice): modernize type annotations and fix ruff linting in LeetCode solutions
Brain Structural Validation / Validate Markdown Structure & Links (push) Failing after 4s
Brain Structural Validation / Validate Markdown Structure & Links (push) Failing after 4s
- Update typing.List to built-in list[T] - Annotate SYMBOLS dictionary with ClassVar in 0013-roman-to-integer - Combine nested if conditions in 0028 and 0058 solutions
This commit is contained in:
1 parent
88c5408e0a
commit
ff59aa44af
9 files changed
+25
-26
No files matched your search
@@ -1,8 +1,7 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
def twoSum(self, nums: list[int], target: int) -> list[int]:
|
||||
seen = {}
|
||||
|
||||
for i, x in enumerate(nums):
|
||||
@@ -12,7 +11,7 @@ class Solution:
|
||||
seen[x] = i
|
||||
return []
|
||||
|
||||
def brute_force(self, nums: List[int], target: int) -> List[int]:
|
||||
def brute_force(self, nums: list[int], target: int) -> list[int]:
|
||||
for i in range(len(nums)):
|
||||
for j in range(i + 1, len(nums)):
|
||||
if nums[i] + nums[j] == target:
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
from typing import ClassVar
|
||||
|
||||
|
||||
class Solution:
|
||||
SYMBOLS = {
|
||||
SYMBOLS: ClassVar[dict[str, int]] = {
|
||||
"I": 1,
|
||||
"V": 5,
|
||||
"X": 10,
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def longestCommonPrefix(self, strs: List[str]) -> str:
|
||||
def longestCommonPrefix(self, strs: list[str]) -> str:
|
||||
if len(strs) == 0:
|
||||
return ""
|
||||
return self.horizontal_scanning(strs)
|
||||
|
||||
def vertical_scanning(self, strs: List[str]) -> str:
|
||||
def vertical_scanning(self, strs: list[str]) -> str:
|
||||
prefix = []
|
||||
ref_string = strs[0]
|
||||
|
||||
@@ -19,7 +18,7 @@ class Solution:
|
||||
|
||||
return "".join(prefix)
|
||||
|
||||
def horizontal_scanning(self, strs: List[str]) -> str:
|
||||
def horizontal_scanning(self, strs: list[str]) -> str:
|
||||
prefix = strs[0]
|
||||
|
||||
for string in strs[1:]:
|
||||
@@ -28,18 +27,18 @@ class Solution:
|
||||
|
||||
return prefix
|
||||
|
||||
def transpose_matrix(self, strs: List[str]) -> str:
|
||||
def transpose_matrix(self, strs: list[str]) -> str:
|
||||
prefix = []
|
||||
transposed = list(zip(*strs))
|
||||
|
||||
for i, row in enumerate(transposed):
|
||||
if not len(set(row)) <= 1:
|
||||
return "".join(prefix)
|
||||
prefix.append(transposed[i][0])
|
||||
prefix.append(row[0])
|
||||
|
||||
return "".join(prefix)
|
||||
|
||||
def binary_search(self, strs: List[str]) -> str:
|
||||
def binary_search(self, strs: list[str]) -> str:
|
||||
def is_common_prefix(strs, k):
|
||||
prefix = strs[0][:k]
|
||||
for string in strs[1:]:
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
from typing import Optional, Any
|
||||
from typing import Any
|
||||
|
||||
|
||||
# Definition for singly-linked list (expanded).
|
||||
class ListNode:
|
||||
def __init__(self, val: Any = 0, next=None):
|
||||
@@ -19,7 +21,7 @@ class ListNode:
|
||||
|
||||
|
||||
class Solution:
|
||||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
|
||||
def mergeTwoLists(self, list1: ListNode | None, list2: ListNode | None) -> ListNode | None:
|
||||
dummy = ListNode()
|
||||
current = dummy
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def removeDuplicates(self, nums: List[int]) -> int:
|
||||
def removeDuplicates(self, nums: list[int]) -> int:
|
||||
if not nums:
|
||||
return 0
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def removeElement(self, nums: List[int], val: int) -> int:
|
||||
def removeElement(self, nums: list[int], val: int) -> int:
|
||||
w = 0
|
||||
|
||||
for i in range(len(nums)):
|
||||
|
||||
+2
-3
@@ -7,9 +7,8 @@ class Solution:
|
||||
N = len(needle)
|
||||
|
||||
for i in range(L - N + 1):
|
||||
if haystack[i] == needle[0]:
|
||||
if haystack[i:i+N] == needle:
|
||||
return i
|
||||
if haystack[i] == needle[0] and haystack[i:i+N] == needle:
|
||||
return i
|
||||
|
||||
return -1
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def searchInsert(self, nums: List[int], target: int) -> int:
|
||||
def searchInsert(self, nums: list[int], target: int) -> int:
|
||||
if not nums:
|
||||
return 0
|
||||
|
||||
|
||||
@@ -14,10 +14,9 @@ class Solution:
|
||||
if start is None and char != " ":
|
||||
# found start
|
||||
start = i
|
||||
if start is not None:
|
||||
if char == " ":
|
||||
# found end
|
||||
return i - start
|
||||
if start is not None and char == " ":
|
||||
# found end
|
||||
return i - start
|
||||
return len(s) - start if start is not None else 0
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user