Leetcode: 1-5 questions code, leetcode1-5

Source: Internet
Author: User

Leetcode: 1-5 questions code, leetcode1-5

The following are the first five questions that have been flushed during this period of time. They are all solutions that you may think of. They may not be the optimal solution. They are just sorted out to facilitate optimization and improvement in the future.

1. Two Sum:

class Solution:    # @return a tuple, (index1, index2)    def twoSum(self, num, target):        dict = {}        for i in xrange(len(num)):            if dict.get(target-num[i], None) == None:                dict[num[i]] = i            else:                return (dict[target-num[i]] , i )

2. Add Two Numbers:

class Solution(object):    def addTwoNumbers(self, l1, l2):        """        :type l1: ListNode        :type l2: ListNode        :rtype: ListNode        """        more=0        l3=ListNode(0)                l3.val=l1.val+l2.val+more        if l3.val>=10:            more=1        else:            more=0        l3.val=l3.val%10        l1_temp=l3        l1=l1.next        l2=l2.next                while(l1 and l2):            temp=ListNode(0)            temp.val=l1.val+l2.val+more            if temp.val>=10:                more=1            else:                more=0            temp.val=temp.val%10                        l1_temp.next=temp            l1_temp=temp            l1=l1.next            l2=l2.next                if((l1 is None )and( l2 is None)):            if more==1:                temp=ListNode(0)                temp.val=1                l1_temp.next=temp            return l3                    elif(l1 and ( l2 is None)):            while(l1):                temp=ListNode(0)                temp.val=more+l1.val                if temp.val>=10:                    more=1                else:                    more=0                temp.val=temp.val%10                l1_temp.next=temp                l1_temp=temp                l1=l1.next            if more==1:                temp=ListNode(0)                temp.val=1                l1_temp.next=temp            return l3                elif(l2 and ( l1 is None)):            while(l2):                temp=ListNode(0)                temp.val=more+l2.val                if temp.val>=10:                    more=1                else:                    more=0                temp.val=temp.val%10                l1_temp.next=temp                l1_temp=temp                l2=l2.next            if more==1:                temp=ListNode(0)                temp.val=1                l1_temp.next=temp            return l3

3. Longest Substring Without Repeating Characters:

class Solution(object):    def lengthOfLongestSubstring(self, s):        """        :type s: str        :rtype: int        """        new_str = ''        max = 0        for ch in s:            if not ch in new_str:                new_str += ch            else:                max = len(new_str) if len(new_str) > max else max                idx = new_str.find(ch)                new_str = new_str[idx+1:] + ch        max = len(new_str) if len(new_str) > max else max        return max

4. Median of Two Sorted Arrays:

Class Solution (object): def findMedianSortedArrays (self, nums1, nums2): "": type nums1: List [int]: type nums2: List [int]: rtype: float "end1 = len (nums1)-1 end2 = len (nums2)-1 if len (nums1) = 0: num = len (nums2) if num % 2: # num is an odd return float (nums2 [num/2]) else: median = num/2 return (float (nums2 [median-1]) + nums2 [median]) /2 elif len (nums2) = 0: num = len (nums1) if num % 2: # num is an odd median = num/2 return float (nums1 [median]) else: median = num/2 return (float (nums1 [median-1]) + nums1 [median])/2 elif nums1 [end1] <nums2 [0]: nums1.extend (nums2) num = len (nums1) if num % 2: # num is an odd median = num/2 return float (nums1 [median]) else: median = num/2 return (float (nums1 [median-1]) + nums1 [median])/2 elif nums1 [0]> nums2 [end2]: nums2.extend (nums1) num = len (nums2) if num % 2: # num is an odd return float (nums2 [num/2]) else: median = num/2 return (float (nums2 [median-1]) + nums2 [median])/2 else: I = 0 j = 0 new = [] while I <len (nums1) and j <len (nums2): if nums1 [I] <nums2 [j]: new. append (nums1 [I]) I = I + 1 elif nums1 [I] = nums2 [j]: new. append (nums1 [I]) new. append (nums1 [I]) I = I + 1 j = j + 1 else: new. append (nums2 [j]) j = j + 1 while I <len (nums1): new. append (nums1 [I]) I = I + 1 while j <len (nums2): new. append (nums2 [j]) j = j + 1 num = len (new) if num % 2: # num is an odd return float (new [num/2]) else: median = num/2 return (float (new [median-1]) + new [median])/2

5. Longest Palindromic Substring:

class Solution(object):        def longestPalindrome(self, s):        """        :type s: str        :rtype: str        """        if len(s)==1:            return s        elif len(s)==2 and s[0]==s[1]:            return s        else:            s_fin1=""            s_fin2=""                        index_l=0            index_r=1            max_length=0            left=-1            right=-1            flag=0            while index_r<len(s):                i=0                length=0                while index_l-i>=0 and index_r+i<len(s) and s[index_l-i]==s[index_r+i]:                    i=i+1                    length=length+2                if length>max_length:                    max_length=length                    left=index_l                    right=index_r                index_l=index_l+1                index_r=index_r+1            if max_length:                begin=left-max_length/2+1                end=right+max_length/2                flag=2                s_fin1=s[begin:end]                                    index=1            max_length=1            center=-1            flag=0            while index<(len(s)-1):                i=1                length=1                while index-i>=0 and index+i<len(s) and s[index-i]==s[index+i]:                    i=i+1                    length=length+2                if length>max_length:                    max_length=length                    center=index                index=index+1            if max_length>1:                begin=center-(max_length-1)/2                end=center+(max_length-1)/2+1                flag=1                s_fin2=s[begin:end]                            if len(s_fin1)>len(s_fin2):                return s_fin1            else:                return s_fin2

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.