Advertisement
PeenB

Assignment from TonInwZaaa

Nov 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. ##[1]Two Sum
  2. class Solution:
  3.     def twoSum(self, nums: List[int], target: int) -> List[int]:
  4.         for i in range(len(nums)):
  5.             x = nums[i+1:]
  6.             for j in range(len(x)):
  7.                 if nums[i]+x[j] == target:
  8.                     return (i, j+i+1)
  9.  
  10.  
  11. ##[9]Palindrome
  12. class Solution:
  13.     def isPalindrome(self, x: int) -> bool:
  14.         a = str(x)
  15.         b = ""
  16.         for i in a:
  17.             b=i+b
  18.             if(a==b):
  19.                 return (True)
  20.  
  21.  
  22. ##[13]Roman to integer
  23. class Solution:
  24.     def romanToInt(self, s: str) -> int:
  25.         dictionary = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
  26.         x = len(s)
  27.         total = dictionary[s[x-1]]
  28.         for i in range(x-1,0,-1):
  29.             current = dictionary[s[i]]
  30.             prev = dictionary[s[i-1]]
  31.             total += prev if prev >= current else -prev
  32.         return (total)
  33.  
  34.  
  35. ##[20]Remove Parentheses
  36. class Solution:
  37.     def isValid(self, s: str) -> bool:
  38.         x = {"(":")","{":"}","[":"]"}
  39.         y = []
  40.         for i in s:
  41.             if(i in x):
  42.                 y.append(x[i])
  43.             else:
  44.                 if(y and y[-1]==i):
  45.                     y.pop()
  46.                 else:
  47.                     return (False)
  48.         return (False) if(y) else (True)
  49.  
  50.  
  51.  
  52. ##[26]Remove Duplicate from sorted array
  53. class Solution:
  54.     def removeDuplicates(self, nums: List[int]) -> int:
  55.         if (len(nums) == 0 or len(nums)==1):
  56.             return (len(nums))
  57.         j=0
  58.         for i in range(0,len(nums)-1):
  59.             if nums[i] != nums[i+1]:
  60.                 nums[j] = nums[i]
  61.                 j += 1
  62.  
  63.         nums[j] = nums[len(nums)-1]
  64.         j += 1
  65.         return (j)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement