Added two solutions to longest palindrome and median of two sorted arrays
This commit is contained in:
21
longestPalindrome.py
Normal file
21
longestPalindrome.py
Normal file
@@ -0,0 +1,21 @@
|
||||
#https://leetcode.com/problems/longest-palindromic-substring/submissions/
|
||||
|
||||
class Solution:
|
||||
def longestPalindrome(self, s: str) -> str:
|
||||
longestSub = ""
|
||||
for base in range(len(s)):
|
||||
for length in range(len(s)-base):
|
||||
print(base, length, s[base:len(s)-length])
|
||||
if s[base:len(s)-length] == s[base:len(s)-length][::-1]:
|
||||
if len(s[base:len(s)-length]) > len(longestSub):
|
||||
if base == 0 and length == len(s):
|
||||
return s[base:len(s)-length]
|
||||
longestSub = s[base:len(s)-length]
|
||||
return longestSub
|
||||
|
||||
def main():
|
||||
solution = Solution()
|
||||
print(solution.longestPalindrome("1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
17
medianOfTwoSortedArrays.py
Normal file
17
medianOfTwoSortedArrays.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# https://leetcode.com/problems/median-of-two-sorted-arrays/
|
||||
|
||||
class Solution:
|
||||
def findMedianSortedArrays(self, nums1: list, nums2: list) -> float:
|
||||
nums1.extend(nums2)
|
||||
nums1.sort()
|
||||
if len(nums1) % 2 != 0:
|
||||
return nums1[int(len(nums1)/2)]
|
||||
else:
|
||||
return (nums1[int(len(nums1)/2)] + nums1[int(len(nums1)/2)-1]) / 2
|
||||
|
||||
def main():
|
||||
solution = Solution()
|
||||
print(solution.findMedianSortedArrays([],[2]))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user