Leetcode brush question log 5.0

Table of contents

Foreword:

1. Adding two numbers

2. The longest substring without repeating characters

 3. Integer inversion

4. Delete the last N node of the linked list 


Foreword:

        Today I will continue to share the questions I have done recently, and now let's enter our happy time of brushing questions! (Programming language Python3.0, difficulty: medium)

1. Adding two numbers

You are given two non-empty linked lists representing two non-negative integers. Each of them is stored in reverse order, and each node can only store one digit.

Please add two numbers and return a linked list representing the sum in the same form.

You can assume that neither number starts with a zero other than the number zero.

 

Example:  code implementation:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1, l2):
        li1=[]
        li2=[]
        p1=l1
        while p1:
            li1.append(str(p1.val))
            p1=p1.next
        p2=l2
        while p2:
            li2.append(str(p2.val))
            p2=p2.next
        li1=li1[::-1]
        li2=li2[::-1]
        a=str(int(''.join(li1))+int(''.join(li2)))[::-1]
        li3=[]
        for i in a:
            li3.append(int(i))
        l3 = ListNode(li3[0])
        cur=l3
        for i in range(1,len(li3)):
            p=ListNode(li3[i])
            cur.next=p
            cur=p
        return l3

 Problem-solving idea: For this question, we can create two lists as the data storage containers of the two linked lists given in the title, and cycle through the two linked lists in turn, put the data in them into li1 and li2, and then follow the The requirements of the title are reversed, and then the data in the two lists are integrated into an integer and added, and then the result is put into the list li3 and reversed. The following is to create a linked list, and sequentially put the data in li3 Just store it in the list

2. The longest substring without repeating characters

Given a string  s , please find the length of the longest substring that does not contain repeated characters. 

class Solution:
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        st = {}
        i, ans = 0, 0
        for j in range(len(s)):
            if s[j] in st:
                i = max(st[s[j]], i)
            ans = max(ans, j - i + 1)
            st[s[j]] = j + 1
        return ans

Problem-solving idea: first create a dictionary as the number of positions of each character (starting from 1), then loop through the string, use each character as a key, and then store the number of positions in the dictionary as a value. When the character in the dictionary is already in the dictionary, it means that it has started to repeat again. At this time, it is necessary to recount the number of the longest discontinuous string. At this time, i should be the same as the previous position and the position at this time. Take the maximum, and at the same time the longest discontinuous string is to take the maximum length of the last ans and the (j+1)-i statistics at this time. The ans returned after the last traversal is the maximum discontinuous string length.

 3. Integer inversion

Given a 32-bit signed integer x, return the result of inverting the numeric portion of x.

Returns 0 if the inverted integer exceeds the range [−231, 231 − 1] for a 32-bit signed integer.

Assume the environment does not allow storing 64-bit integers (signed or unsigned).

class Solution:
    def reverse(self, x):
        if x==0:
            return 0
        s=str(x)
        li=[]
        fu=[]
        for i in s:
            li.append(i)
        if li[0]=='-':
            fu.append(li[0])
            li=li[1:]
        if li[len(li)-1]=='0':
            li=li[:len(li)-1]
        xx=int(''.join(fu+li[::-1]))
        if xx>2**31-1 or xx<-(2**31):
            return 0
        return xx

 Problem-solving idea: This kind of problem can be solved by classifying and discussing. If you enter 0, then return 0. Convert the number into a string and put it in the list, and then judge the number. If there is a negative sign, just use the negative sign. Bring it up and put it into the list fu, and then slice the list li; if the last number in the list li is 0, then remove the 0 (slice processing as well), and finally we only need to separate the list fu and li (in reverse order ) are spliced ​​together, and then judge whether the number xx is within the required range, and finally output the result.

4. Delete the last N node of the linked list 

Given a linked list, delete the penultimate  n node of the linked list, and return the head node of the linked list. 

 

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head, n):
        count=0
        p=head
        while p:
            count+=1
            p=p.next
        if count==0 or count==1:
            head=None
            return head
        target=count+1-n
        if target==1:
            head=head.next
            return head
        c=1
        q=head
        while c+1<target:
            q=q.next
            c+=1
        q.next=q.next.next
        return head

 Problem-solving ideas: First of all, we need to count how many nodes in this linked list are recorded as count, and delete the penultimate n-th node according to the requirements of the title. In fact, it is to delete the count+1-n-th node. The following is a classification discussion. Yes, if you want to delete the first node, then let the head node move one bit behind, and then return. If you want to delete the second node and above, then you need to loop and find For the previous node to be deleted, let this node point to the next node to be deleted. Finally return the head node.

 Well, that's all for today, see you in the next issue!

Share a wallpaper:

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/130658530