Commit 99cd22bf authored by jiang feng's avatar jiang feng
Browse files

Merge branch 'tmp1' into 'main'

Tmp1

See merge request !1
parents e1017a70 491d8ff6
'''
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
'''
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
mapSet = {}
start, result = 0, 0
for end in range(len(s)):
if s[end] in mapSet:
start = max(mapSet[s[end]], start)
result = max(result, end-start+1)
mapSet[s[end]] = end+1
return result
\ No newline at end of file
'''
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
'''
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
dp = [[0 for _ in range(len(s))] for _ in range(len(s))]
maxLength, result = 1, ""
for index in range(len(s)):
dp[index][index] = 1
result = s[index]
length = 2
while length <= len(s):
index_i = 0
while index_i < len(s) - length + 1:
index_j = index_i + length -1
if length == 2 and s[index_i] == s[index_j]:
dp[index_i][index_j] = 1
maxLength = max(maxLength, 2)
result = s[index_i:index_j+1]
elif s[index_i] == s[index_j] and dp[index_i+1][index_j-1]:
dp[index_i][index_j] = 1
if length > maxLength:
maxLength = length
result = s[index_i:index_j+1]
index_i += 1
length += 1
return result
# Space: O(N^2)
# Time: O(N^2)
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
def expand(s, left, right):
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return right-left-1
start, end = 0, 0
for index in range(len(s)):
even_len = expand(s, index, index+1)
odd_len = expand(s, index, index)
length = max(even_len, odd_len)
if length > (end-start):
start = index - (length-1)/2
end = index +length/2
return s[start:end+1]
\ No newline at end of file
'''
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
'''
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
left, right, maxArea = 0, len(height) - 1, 0
while left < right:
maxArea = max(maxArea, min(height[left], height[right])*(right-left))
if height[left] < height[right]:
left += 1
else:
right -= 1
return maxArea
# Space : O(1)
# Time: O(N)
\ No newline at end of file
'''
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
'''
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
x, y = len(nums1), len(nums2)
low , high = 0, x
while low <= high:
partitionx = (low+high)/2
partitiony = (x+y+1)/2 - partitionx
if partitionx == 0:
maxLeftX = float('-inf')
else:
maxLeftX = nums1[partitionx-1]
if partitionx == x:
minRightX = float('inf')
else:
minRightX = nums1[partitionx]
if partitiony == 0:
maxLeftY = float('-inf')
else:
maxLeftY = nums2[partitiony-1]
if partitiony == y:
minRightY = float('inf')
else:
minRightY = nums2[partitiony]
if maxLeftX <= minRightY and maxLeftY <= minRightX:
if((x+y)%2 == 0):
return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY))/2.0
else:
return max(maxLeftY, maxLeftX)
elif maxLeftX > minRightY:
high = partitionx - 1
else:
low = partitionx + 1
print Solution().findMedianSortedArrays([1,2], [3, 4])
\ No newline at end of file
class Solution:
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
str = str.strip()
number = ""
for x in str:
if x.isalpha() and number == "":
return 0
elif x.isalpha():
break
elif x == ".":
break
elif x == " ":
break
elif (x == "+" or x == "-") and number == "":
number = number + x
elif (x == "+" or x == "-") and number != "":
break
elif (x == "+" or x == "-") and (number[-1] == "+" or number[-1] == "-"):
return 0
elif (x == "+" or x == "-") and ("+" in number or "-" in number):
break
elif x.isdigit():
number = number + x
if number == "" or number == "+" or number == "-":
return 0
else:
if int(number) > ((2**31)-1):
return (2**31)-1
elif int(number) < -(2**31):
return -(2**31)
else:
return int(number)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment