test commit

swadhinbiswas committed 2 years ago
74b485a
1 parent 4ac0736
commit 74b485a
7 files changed
+102 -0
▼Longestcommonprifix.py
+11-0
1
+from typing import List
2
+
3
+class Solution:
4
+ def longestCommonPrefix(self, strs: List[str]) -> str:
5
+ if not strs:
6
+ return ""
7
+ for i in range(len(strs[0])):
8
+ for string in strs[1:]:
9
+ if i >= len(string) or string[i] != strs[0][i]:
10
+ return strs[0][:i]
11
+ return strs[0]
0
12
▼Palindrome Number.py
+24-0
1
+class Solution:
2
+ def isPalindrome(self, x: int) -> bool:
3
+ if x < 0:
4
+ return False
5
+ while x > 0:
6
+ if x % 10 != x // 10 ** (len(str(x)) - 1):
7
+ return False
8
+ x = (x % 10 ** (len(str(x)) - 1)) // 10
9
+ return True
10
+
11
+
12
+
13
+if __name__ == "__main__":
14
+ #testcase1
15
+ s = Solution()
16
+ a=s.isPalindrome(121)
17
+ b=s.isPalindrome(-121)
18
+ c=s.isPalindrome(10)
19
+ d=s.isPalindrome(1000021)
20
+ print(a) #True
21
+ print(b) #False
22
+ print(c) #False
23
+ print(d) #False
24
+
0
25
▼Romantointeger.py
+20-0
1
+class Solution:
2
+ def romanToInt(self, s: str) -> int:
3
+ roman = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
4
+ num = 0
5
+ for i in range(len(s)):
6
+ if i > 0 and roman[s[i]] > roman[s[i - 1]]:
7
+ num += roman[s[i]] - 2 *roman[s[i - 1]]
8
+ else:
9
+ num += roman[s[i]]
10
+ return num
11
+
12
+if __name__ == "__main__":
13
+ #testcase1
14
+ s = Solution()
15
+ a=s.romanToInt("III")
16
+ b=s.romanToInt("IV")
17
+ c=s.romanToInt("IX")
18
+ d=s.romanToInt("LVIII")
19
+ e=s.romanToInt("MCMXCIV")
20
+
0
21
▼marge2shortedlist.py
+5-0
1
+from typing import Optional,ListNode
2
+
3
+class Solution:
4
+ def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
5
+ pass
0
6
▼problem(two sum).py
+15-0
1
+from typing import List
2
+class Solution:
3
+ def twoSum(self, nums: List[int], target: int) -> List[int]:
4
+ d = {}
5
+ for i, num in enumerate(nums):
6
+ if target - num in d:
7
+ return [d[target - num], i]
8
+ d[num] = i
9
+ return []
10
+
11
+if __name__ == "__main__":
12
+ s = Solution()
13
+ print(s.twoSum([2, 7, 11, 15], 9)) # [0, 1]
14
+ print(s.twoSum([3, 2, 4], 6)) # [1, 2]
15
+ print(s.twoSum([3, 3], 6)) # [0, 1]
0
16
▼removeduplict.py
+0-0
0
0
▼validparentatis.py
+27-0
1
+class Solution:
2
+ def isValid(self, s: str) -> bool:
3
+ stack=[]
4
+ openbracket={
5
+ '[':1,'{':2,'(':3,
6
+ }
7
+ closebracket={
8
+ ']':1,'}':2,')':3,
9
+ }
10
+
11
+ for i in s:
12
+ if i not in closebracket:
13
+ stack.append(i)
14
+ else:
15
+ if len(stack)>0:
16
+ pop_brack=stack.pop()
17
+ if closebracket[i]==openbracket[pop_brack]:
18
+ continue
19
+ else:
20
+ return False
21
+ else:
22
+ return False
23
+
24
+ if len(stack)==0:
25
+ return True
26
+ else:
27
+ return False
0
28