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:
|
class Solution:
|
||||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
def twoSum(self, nums: list[int], target: int) -> list[int]:
|
||||||
seen = {}
|
seen = {}
|
||||||
|
|
||||||
for i, x in enumerate(nums):
|
for i, x in enumerate(nums):
|
||||||
@@ -12,7 +11,7 @@ class Solution:
|
|||||||
seen[x] = i
|
seen[x] = i
|
||||||
return []
|
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 i in range(len(nums)):
|
||||||
for j in range(i + 1, len(nums)):
|
for j in range(i + 1, len(nums)):
|
||||||
if nums[i] + nums[j] == target:
|
if nums[i] + nums[j] == target:
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
from typing import ClassVar
|
||||||
|
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
SYMBOLS = {
|
SYMBOLS: ClassVar[dict[str, int]] = {
|
||||||
"I": 1,
|
"I": 1,
|
||||||
"V": 5,
|
"V": 5,
|
||||||
"X": 10,
|
"X": 10,
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
from typing import List
|
|
||||||
|
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def longestCommonPrefix(self, strs: List[str]) -> str:
|
def longestCommonPrefix(self, strs: list[str]) -> str:
|
||||||
if len(strs) == 0:
|
if len(strs) == 0:
|
||||||
return ""
|
return ""
|
||||||
return self.horizontal_scanning(strs)
|
return self.horizontal_scanning(strs)
|
||||||
|
|
||||||
def vertical_scanning(self, strs: List[str]) -> str:
|
def vertical_scanning(self, strs: list[str]) -> str:
|
||||||
prefix = []
|
prefix = []
|
||||||
ref_string = strs[0]
|
ref_string = strs[0]
|
||||||
|
|
||||||
@@ -19,7 +18,7 @@ class Solution:
|
|||||||
|
|
||||||
return "".join(prefix)
|
return "".join(prefix)
|
||||||
|
|
||||||
def horizontal_scanning(self, strs: List[str]) -> str:
|
def horizontal_scanning(self, strs: list[str]) -> str:
|
||||||
prefix = strs[0]
|
prefix = strs[0]
|
||||||
|
|
||||||
for string in strs[1:]:
|
for string in strs[1:]:
|
||||||
@@ -28,18 +27,18 @@ class Solution:
|
|||||||
|
|
||||||
return prefix
|
return prefix
|
||||||
|
|
||||||
def transpose_matrix(self, strs: List[str]) -> str:
|
def transpose_matrix(self, strs: list[str]) -> str:
|
||||||
prefix = []
|
prefix = []
|
||||||
transposed = list(zip(*strs))
|
transposed = list(zip(*strs))
|
||||||
|
|
||||||
for i, row in enumerate(transposed):
|
for i, row in enumerate(transposed):
|
||||||
if not len(set(row)) <= 1:
|
if not len(set(row)) <= 1:
|
||||||
return "".join(prefix)
|
return "".join(prefix)
|
||||||
prefix.append(transposed[i][0])
|
prefix.append(row[0])
|
||||||
|
|
||||||
return "".join(prefix)
|
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):
|
def is_common_prefix(strs, k):
|
||||||
prefix = strs[0][:k]
|
prefix = strs[0][:k]
|
||||||
for string in strs[1:]:
|
for string in strs[1:]:
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
from typing import Optional, Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
# Definition for singly-linked list (expanded).
|
# Definition for singly-linked list (expanded).
|
||||||
class ListNode:
|
class ListNode:
|
||||||
def __init__(self, val: Any = 0, next=None):
|
def __init__(self, val: Any = 0, next=None):
|
||||||
@@ -19,7 +21,7 @@ class ListNode:
|
|||||||
|
|
||||||
|
|
||||||
class Solution:
|
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()
|
dummy = ListNode()
|
||||||
current = dummy
|
current = dummy
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
from typing import List
|
|
||||||
|
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def removeDuplicates(self, nums: List[int]) -> int:
|
def removeDuplicates(self, nums: list[int]) -> int:
|
||||||
if not nums:
|
if not nums:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
from typing import List
|
|
||||||
|
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def removeElement(self, nums: List[int], val: int) -> int:
|
def removeElement(self, nums: list[int], val: int) -> int:
|
||||||
w = 0
|
w = 0
|
||||||
|
|
||||||
for i in range(len(nums)):
|
for i in range(len(nums)):
|
||||||
|
|||||||
+2
-3
@@ -7,9 +7,8 @@ class Solution:
|
|||||||
N = len(needle)
|
N = len(needle)
|
||||||
|
|
||||||
for i in range(L - N + 1):
|
for i in range(L - N + 1):
|
||||||
if haystack[i] == needle[0]:
|
if haystack[i] == needle[0] and haystack[i:i+N] == needle:
|
||||||
if haystack[i:i+N] == needle:
|
return i
|
||||||
return i
|
|
||||||
|
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from typing import List
|
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def searchInsert(self, nums: List[int], target: int) -> int:
|
def searchInsert(self, nums: list[int], target: int) -> int:
|
||||||
if not nums:
|
if not nums:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
@@ -14,10 +14,9 @@ class Solution:
|
|||||||
if start is None and char != " ":
|
if start is None and char != " ":
|
||||||
# found start
|
# found start
|
||||||
start = i
|
start = i
|
||||||
if start is not None:
|
if start is not None and char == " ":
|
||||||
if char == " ":
|
# found end
|
||||||
# found end
|
return i - start
|
||||||
return i - start
|
|
||||||
return len(s) - start if start is not None else 0
|
return len(s) - start if start is not None else 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user