This file contains 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 isMatch(self, s: str, p: str) -> bool: | |
matched = [[False for _ in range(len(p) + 1)] for _ in range(len(s) + 1)] | |
matched[0][0] = True | |
for i in range(len(s) + 1): | |
for j in range(1, len(p) + 1): | |
pattern = p[j - 1] | |
if pattern == '.': |
This file contains 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, x: int) -> bool: | |
n = len(str(x)) // 2 | |
if len(str(x)) % 2 != 0: | |
n = len(str(x)) // 2 | |
return str(x)[:n] == str(x)[:n:-1] | |
elif len(str(x)) % 2 == 0: | |
return str(x)[:n] == str(x)[n:][::-1] | |
else: |
This file contains 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: | |
str = str.strip() | |
isNegative = False | |
if str and str[0] == '-': | |
isNegative = True | |
if str and (str[0] == '+' or str[0] == '-'): | |
str = str[1:] | |
if not str: |
This file contains 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: | |
isNegative = x < 0 | |
x = abs(x) | |
reversed = 0 | |
while x != 0: | |
reversed = reversed * 10 + x % 10 | |
x //= 10 | |
This file contains 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 | |
zigzag = [[] for _ in range(numRows)] | |
row = 0 | |
direction = -1 | |
for c in s: |
This file contains 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: | |
longest_str = '' | |
n = [len(s) - 1] | |
for diff in range(1, len(s)): | |
n.append(n[0] + diff) | |
n.append(n[0] - diff) | |
for i in n: |
This file contains 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 findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: | |
nums1.extend(nums2) | |
nums1 = sorted(nums1) | |
if len(nums1) % 2 == 0: | |
n = len(nums1) // 2 | |
return (nums1[n - 1] + nums1[n]) / 2 | |
else: | |
n = (len(nums1) // 2) + 1 |
This file contains 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 lengthOfLongestSubstring(self, s: str) -> int: | |
mapper = {} | |
start = 0 | |
longest = 0 | |
for i, c in enumerate(s): | |
if c in mapper and mapper[c] >= start: | |
start = mapper[c] + 1 | |
else: |
This file contains 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 ListNode(): | |
def __init__(self, x): | |
self.val = x | |
self.next = None | |
class Solution: | |
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: | |
prev = result = ListNode(None) | |
n = 0 | |
This file contains 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 twoSum(self, nums: List[int], target: int) -> List[int]: | |
container = {} | |
for i, num in enumerate(nums): | |
if target - num in container: | |
return [container[target - num], i] | |
container[num] = i | |
return |