LeetCode学习笔记-Day2

来源:互联网 发布:千牛for mac 编辑:程序博客网 时间:2024/06/05 18:05

第三题

求字符串中无重复子字符串的最大长度

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        """        max_length = 0        l= len(s)        for i in range(l):            string = s[i]            for j in range(i+1,l):                if s[j] not in string:                    string = string + s[j]                else:                    break            ll = len(string)            if (ll > max_length):                max_length = ll        return max_length

大神的答案

class Solution(object):    def lengthOfLongestSubstring(self, s):        """        :type s: str        :rtype: int        """        max_length = start = 0        usedChar = {}        for i in range(len(s)):            if s[i] in usedChar and start <= usedChar[s[i]]:#在start后面寻找相同的字符                start = usedChar[s[i]] + 1            else:                max_length = max(max_length, i -start + 1)            usedChar[s[i]] = i            return max_length

只需要一次循环,循环时将已经出现的字符及其索引存入字典中,当start后面出现相同的字符时更新start,每一次循环都要更新字典中的值。

第四题

求两个有序列表的中值

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

要求计算复杂度,这题太难了,mark

class Solution(object):    def findMedianSortedArrays(self, nums1, nums2):        """        :type nums1: List[int]        :type nums2: List[int]        :rtype: float        """        m = len(nums1)        n = len(nums2)        if m<n:            l1 = nums1; l2 = nums2        else:            l1 = nums2; l2= nums1            m, n = n, m        imin = 0; imax = m        i = (imin + imax) / 2        j = (m + n + 1) / 2 - i        while (imin < imax):            if i>0 and l1[i-1]>l2[j]:                imax = i - 1; i = (imin + imax) / 2; j = (m + n + 1) / 2 - i            elif j>0 and l2[j-1]>l1[i]:                imin = i + 1; i = (imin + imax) / 2; j = (m + n + 1) / 2 - i            else:                break        if i==0: max_of_left = l2[j-1]        elif j==0: max_of_left = l1[i-1]        else: max_of_left = max(l2[j-1], l1[i-1])        if (m + n) % 2==1: return max_of_left        if i==m: min_of_right = l2[j]        elif j==n: min_of_right = l1[i]        else: min_of_right = min(l1[i], l2[j])        return (max_of_left + min_of_right) / 2.0

主要思路:

将两个列表分别从i,j处分开,[ left_A | right_A ] 、 [ left_B | right_B ] ,中值代表的意义指个数left_A + leftB = right_A + right_B,所以必须满足:

i+j=mi+njormi+nj+1

A[i1]<B[j]andB[j1]<A[i]

原创粉丝点击