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
# Solution for Valid Palindrome | |
class Solution: | |
def isPalindrome(self, s: str) -> bool: | |
s = s.lower() | |
s_list = list(s) | |
s = ''.join([x for x in s_list if x.isalnum()]) | |
return s == s[::-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
# Solution for Two Sum II - Input Array Is Sorted | |
class Solution: | |
def diff_bw_nums(self, a: int, b: int) -> int: | |
return a-b | |
def twoSum(self, numbers: List[int], target: int) -> List[int]: | |
left = 0 | |
right = len(numbers) - 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
# Solution for Squares of a Sorted Array | |
class Solution: | |
def sortedSquares(self, nums: List[int]) -> List[int]: | |
left = 0 | |
right = len(nums) - 1 | |
result = [] | |
while left <= right: | |
if (nums[left]**2) > (nums[right]**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
# Solution for Roman to Integer | |
class Solution: | |
def romanToInt(self, s: str) -> int: | |
total = 0 | |
num_dict = { | |
'I' : 1, | |
'V' : 5, | |
'X' : 10, | |
'L' : 50, |
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
# Solution for Reverse String | |
class Solution: | |
def reverseString(self, s: List[str]) -> None: | |
""" | |
Do not return anything, modify s in-place instead. | |
""" | |
# case check for single element | |
# if len(s) <= 1: | |
# return |
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
# Solution for Merge Strings Alternately | |
class Solution: | |
def mergeAlternately(self, word1: str, word2: str) -> str: | |
new_string = [] | |
min_length = min(len(word1), len(word2)) | |
if len(word1) != len(word2): | |
if len(word1) > len(word2): | |
temp_string = word1[min_length:] | |
else: |
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
# Solution for Is Subsequence | |
class Solution: | |
def isSubsequence(self, s: str, t: str) -> bool: | |
if s == '': | |
return True | |
s_list = list(s) | |
for char in t: | |
if s_list != [] and char == s_list[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
# Solution for Contains Duplicate | |
class Solution: | |
def containsDuplicate(self, nums: List[int]) -> bool: | |
# import numpy as np | |
# from collections import Counter | |
# counter = Counter(nums) | |
# for x in counter: |
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 numJewelsInStones(self, jewels: str, stones: str) -> int: | |
num_jewels = 0 | |
for char in stones: | |
if char in jewels: | |
num_jewels += 1 | |
return num_jewels |
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 diff_of_num(self, num): | |
if num <= 0: | |
return -num | |
else: | |
return num | |
def findClosestNumber(self, nums): | |
closest = nums[0] |
NewerOlder