This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def longestPalindrome(self, s: str) -> str: | |
| if len(s) < 2: | |
| return s | |
| start = 0 | |
| end = 0 | |
| for i in range(len(s)): | |
| len1 = self.expandAroundCen ter(s, i, i) # Odd-length palindromes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import List | |
| class Solution: | |
| def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: | |
| if len(nums1) > len(nums2): | |
| nums1, nums2 = nums2, nums1 | |
| m, n = len(nums1), len(nums2) | |
| left, right = 0, m | |
| half_len = (m + n + 1) // 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def convert(self, s: str, numRows: int) -> str: | |
| if numRows == 1: | |
| return s | |
| row_arr = [""] * numRows | |
| row_idx = 1 | |
| going_up = True | |
| for ch in s: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def reverse(self, x: int) -> int: | |
| # Integer.MAX_VALUE = 2147483647 (end with 7) | |
| # Integer.MIN_VALUE = -2147483648 (end with -8 ) | |
| MIN = -2147483648 # -2^31, | |
| MAX = 2147483647 # 2^31 - 1 | |
| res = 0 | |
| while x: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def myAtoi(self, str: str) -> int: | |
| value, state, pos, sign = 0, 0, 0, 1 | |
| if len(str) == 0: | |
| return 0 | |
| while pos < len(str): | |
| current_char = str[pos] | |
| if state == 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def productExceptSelf(self, nums: List[int]) -> List[int]: | |
| res = [1] * (len(nums)) | |
| prefix = 1 | |
| for i in range(len(nums)): | |
| res[i] = prefix | |
| prefix *= nums[i] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | |
| count = {} | |
| freq = [[] for i in range(len(nums) + 1)] | |
| for n in nums: | |
| count[n] = 1 + count.get(n, 0) | |
| for n, c in count.items(): | |
| freq[c].append(n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def isValidSudoku(self, board: List[List[str]]) -> bool: | |
| cols = collections.defaultdict(set) | |
| rows = collections.defaultdict(set) | |
| squares = collections.defaultdict(set) # key = (r /3, c /3) | |
| for r in range(9): | |
| for c in range(9): | |
| if board[r][c] == ".": | |
| continue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def longestConsecutive(self, nums: List[int]) -> int: | |
| numSet = set(nums) | |
| longest = 0 | |
| for n in nums: | |
| if (n-1) not in numSet: | |
| length = 1 | |
| while (length + n) in numSet: | |
| length +=1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def isPalindrome(self, s: str) -> bool: | |
| l, r = 0, len(s) - 1 | |
| while l < r: | |
| while l < r and not self.alphaNum(s[l]): | |
| l += 1 | |
| while r > l and not self.alphaNum(s[r]): | |
| r -= 1 | |
| if s[l].lower() != s[r].lower(): |
OlderNewer