leetcode高频100题python
以下是leetcode高频100题的Python解答:
1. 两数之和
```python
def twoSum(nums, target):
    num_dict = {}
    for i, num in enumerate(nums):
        complement = target - num
        if complement in num_dict:
            return [num_dict[complement], i]
        num_dict[num] = i
菜鸟教程python3100题
```
2. 盛最多水的容器
```python
def maxArea(height):
    max_area = 0
    left = 0
    right = len(height) - 1
    while left < right:
        area = min(height[left], height[right]) * (right - left)
        max_area = max(max_area, area)
        if height[left] < height[right]:
            left += 1
        else:
            right -= 1
    return max_area
```
3. 无重复字符的最长子串
```python
def lengthOfLongestSubstring(s):
    max_length = 0
    start = 0
    char_dict = {}
    for i, char in enumerate(s):
        if char in char_dict and char_dict[char] >= start:
            start = char_dict[char] + 1
        char_dict[char] = i
        max_length = max(max_length, i - start + 1)
    return max_length
```
4. 寻两个有序数组的中位数
```python
def findMedianSortedArrays(nums1, nums2):
    m = len(nums1)
    n = len(nums2)
    if m > n:
        nums1, nums2, m, n = nums2, nums1, n, m
    i_min = 0
    i_max = m
    half_len = (m + n + 1) // 2
    while i_min <= i_max:
        i = (i_min + i_max) // 2
        j = half_len - i
        if i < m and nums2[j - 1] > nums1[i]:
            i_min = i + 1
        elif i > 0 and nums1[i - 1] > nums2[j]:
            i_max = i - 1
        else:
            if i == 0:
                max_of_left = nums2[j - 1]
            elif j == 0:
                max_of_left = nums1[i - 1]
            else:
                max_of_left = max(nums1[i - 1], nums2[j - 1])
            if (m + n) % 2 == 1:
                return max_of_left
            if i == m:
                min_of_right = nums2[j]
            elif j == n:
                min_of_right = nums1[i]
            else:
                min_of_right = min(nums1[i], nums2[j])
            return (max_of_left + min_of_right) / 2
```
5. 最长回文子串
```python
def longestPalindrome(s):
    if s == s[::-1]:
        return s
    start = 0
    max_length = 1
    for i in range(1, len(s)):
        if i - max_length >= 1 and s[i - max_length - 1: i + 1] == s[i - max_length - 1: i + 1][::-1]:
            start = i - max_length - 1
            max_length += 2
            continue
        if s[i - max_length: i + 1] == s[i - max_length: i + 1][::-1]:
            start = i - max_length
            max_length += 1
    return s[start: start + max_length]
```
以上是leetcode高频100题的Python解答。请注意,这里只提供代码,可能还需要进行边界条件的处理和测试。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。