Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [1]Two Sum
- class Solution:
- def twoSum(self, nums: List[int], target: int) -> List[int]:
- for i in range(len(nums)):
- x = nums[i+1:]
- for j in range(len(x)):
- if nums[i]+x[j] == target:
- return (i, j+i+1)
- [9]Palindrome
- class Solution:
- def isPalindrome(self, x: int) -> bool:
- a = str(x)
- b = ""
- for i in a:
- b=i+b
- if(a==b):
- return (True)
- [13]Roman to integer
- class Solution:
- def romanToInt(self, s: str) -> int:
- dictionary = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
- x = len(s)
- total = dictionary[s[x-1]]
- for i in range(x-1,0,-1):
- current = dictionary[s[i]]
- prev = dictionary[s[i-1]]
- total += prev if prev >= current else -prev
- return (total)
- [20]Remove Parentheses
- class Solution:
- def isValid(self, s: str) -> bool:
- x = {"(":")","{":"}","[":"]"}
- y = []
- for i in s:
- if(i in x):
- y.append(x[i])
- else:
- if(y and y[-1]==i):
- y.pop()
- else:
- return (False)
- return (False) if(y) else (True)
- [26]Remove Duplicate from sorted array
- class Solution:
- def removeDuplicates(self, nums: List[int]) -> int:
- if (len(nums) == 0 or len(nums)==1):
- return (len(nums))
- j=0
- for i in range(0,len(nums)-1):
- if nums[i] != nums[i+1]:
- nums[j] = nums[i]
- j += 1
- nums[j] = nums[len(nums)-1]
- j += 1
- return (j)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement