From ff59aa44afe06fbb33a06db03626d239f439b2b5 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Thu, 10 Sep 2026 08:55:56 -0500 Subject: [PATCH] refactor(practice): modernize type annotations and fix ruff linting in LeetCode solutions - 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 --- practice/LeetCode/Solutions/0001-two-sum.py | 5 ++--- .../LeetCode/Solutions/0013-roman-to-integer.py | 5 ++++- .../Solutions/0014-longest_common_prefix.py | 13 ++++++------- .../Solutions/0021-merge-two-sorted-lists.py | 6 ++++-- .../0026-remove-duplicates-from-sorted-array.py | 3 +-- practice/LeetCode/Solutions/0027-remove-element.py | 3 +-- ...the-index-of-the-first-occurrence-in-a-string.py | 5 ++--- .../Solutions/0035-search-insert-position.py | 4 ++-- .../LeetCode/Solutions/0058-length-of-last-word.py | 7 +++---- 9 files changed, 25 insertions(+), 26 deletions(-) diff --git a/practice/LeetCode/Solutions/0001-two-sum.py b/practice/LeetCode/Solutions/0001-two-sum.py index d227449..7ff8d0b 100644 --- a/practice/LeetCode/Solutions/0001-two-sum.py +++ b/practice/LeetCode/Solutions/0001-two-sum.py @@ -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: diff --git a/practice/LeetCode/Solutions/0013-roman-to-integer.py b/practice/LeetCode/Solutions/0013-roman-to-integer.py index f40cd9b..1c9139d 100644 --- a/practice/LeetCode/Solutions/0013-roman-to-integer.py +++ b/practice/LeetCode/Solutions/0013-roman-to-integer.py @@ -1,5 +1,8 @@ +from typing import ClassVar + + class Solution: - SYMBOLS = { + SYMBOLS: ClassVar[dict[str, int]] = { "I": 1, "V": 5, "X": 10, diff --git a/practice/LeetCode/Solutions/0014-longest_common_prefix.py b/practice/LeetCode/Solutions/0014-longest_common_prefix.py index 46d3df1..4c1d305 100644 --- a/practice/LeetCode/Solutions/0014-longest_common_prefix.py +++ b/practice/LeetCode/Solutions/0014-longest_common_prefix.py @@ -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:]: diff --git a/practice/LeetCode/Solutions/0021-merge-two-sorted-lists.py b/practice/LeetCode/Solutions/0021-merge-two-sorted-lists.py index f67102f..471b813 100644 --- a/practice/LeetCode/Solutions/0021-merge-two-sorted-lists.py +++ b/practice/LeetCode/Solutions/0021-merge-two-sorted-lists.py @@ -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 diff --git a/practice/LeetCode/Solutions/0026-remove-duplicates-from-sorted-array.py b/practice/LeetCode/Solutions/0026-remove-duplicates-from-sorted-array.py index cc567d3..b400583 100644 --- a/practice/LeetCode/Solutions/0026-remove-duplicates-from-sorted-array.py +++ b/practice/LeetCode/Solutions/0026-remove-duplicates-from-sorted-array.py @@ -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 diff --git a/practice/LeetCode/Solutions/0027-remove-element.py b/practice/LeetCode/Solutions/0027-remove-element.py index e7c25c0..9825a80 100644 --- a/practice/LeetCode/Solutions/0027-remove-element.py +++ b/practice/LeetCode/Solutions/0027-remove-element.py @@ -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)): diff --git a/practice/LeetCode/Solutions/0028-find-the-index-of-the-first-occurrence-in-a-string.py b/practice/LeetCode/Solutions/0028-find-the-index-of-the-first-occurrence-in-a-string.py index b6a939b..289ebb4 100644 --- a/practice/LeetCode/Solutions/0028-find-the-index-of-the-first-occurrence-in-a-string.py +++ b/practice/LeetCode/Solutions/0028-find-the-index-of-the-first-occurrence-in-a-string.py @@ -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 diff --git a/practice/LeetCode/Solutions/0035-search-insert-position.py b/practice/LeetCode/Solutions/0035-search-insert-position.py index 9d5056e..2ee061b 100644 --- a/practice/LeetCode/Solutions/0035-search-insert-position.py +++ b/practice/LeetCode/Solutions/0035-search-insert-position.py @@ -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 diff --git a/practice/LeetCode/Solutions/0058-length-of-last-word.py b/practice/LeetCode/Solutions/0058-length-of-last-word.py index cfef115..c8d376b 100644 --- a/practice/LeetCode/Solutions/0058-length-of-last-word.py +++ b/practice/LeetCode/Solutions/0058-length-of-last-word.py @@ -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