0217leetcode brushes 5 python questions

120

Title description:
Given a triangle, find the minimum path sum from top to bottom.
Each step can only move to the adjacent node in the next row. Adjacent nodes here refer to two nodes whose subscript is the same as or equal to the subscript of the node of the previous layer + 1. In other words, if it is located at the subscript i of the current line, then the next step can be moved to the subscript i or i + 1 of the next line.

Example:
Insert picture description here
Answer:

class Solution:
    def minimumTotal(self, triangle: List[List[int]]) -> int:
        for i in range(len(triangle) - 1, 0, -1):
            for j in range(i):
                triangle[i - 1][j] += min(triangle[i][j], triangle[i][j + 1])
        return triangle[0][0]

147

Title description;
insert sort on the linked list.
Starting from the first element, the linked list can be considered partially sorted (indicated in black).
At each iteration, an element (indicated in red) is removed from the input data and inserted in-situ into the sorted linked list.
Insertion sorting algorithm:
Insertion sorting is iterative, moving only one element at a time until all elements can form an ordered output list.
In each iteration, insertion sort only removes an element to be sorted from the input data, finds its proper position in the sequence, and inserts it.
Repeat until all input data is inserted.

Example:
Insert picture description here
Answer:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def insertionSortList(self, head: ListNode) -> ListNode:
        if(head==None or head.next==None):
            return head
        else:
            h=ListNode()
            h.next=head
            ##  增设一个头指针,使得操作一致。
            p=head.next
            head.next=None
            ##  第一个节点不用排序,和后面的节点断开;p指向未排序链表的第一个节点
            s=h
            st=h.next
            ##  利用插入法进行链表排序。增设两个指针,s和st。
            ##  st指向要与p进行比较的节点,s指向st的前一个节点
            while(p!=None):
                if(p.val<s.val):
                    s=h
                    st=h.next       ##  利用上次排序的结果;以减少比较次数
                
                while(st!=None and p.val>st.val):
                    s=st
                    st=st.next
                ##  已经找到位置,进行插入
                s.next=p
                p=p.next
                s.next.next=st
                s=s.next
                
            return h.next

219

Title description:
Given an integer array and an integer k, judge whether there are two different indexes i and j in the array, so that nums [i] = nums [j], and the absolute value of the difference between i and j is at most k .

Example:
Insert picture description here
Answer:

class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        '''
        超时了
        for i in range(len(nums)-1):
            for j in range(i+1,len(nums)):
                if nums[i]==nums[j]:
                    if j-i<=k:
                        return True
        return False
        '''
        #首先是判断是存在num[i]==num[j],存在的情况下才判断|i-j|<=k
        nums_len=len(nums)
        if nums_len<=1:
            return False
        nums_dict={
    
    }
        for i in range(nums_len):
            if nums[i] in nums_dict:
                if i-nums_dict[nums[i]]<=k:
                    return True
            nums_dict[nums[i]]=i
        return False

977

Title description:
Give you an integer array nums sorted in non-decreasing order, and return a new array composed of the square of each number, and it is required to also sort in non-decreasing order.

Example:
Insert picture description here
Answer:

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        for i in range(len(nums)):
            nums[i]=nums[i]*nums[i]
        return sorted(nums)

1550

Title description:
Give you an integer array arr, please judge whether there are three consecutive elements in the array that are all odd numbers: if it exists, please return true; otherwise, return false.

Example:
Insert picture description here
Answer:

class Solution:
    def threeConsecutiveOdds(self, arr: List[int]) -> bool:
        cnt=0
        for i in arr:
            if i%2==1:
                cnt+=1
                if cnt==3:
                    return True
            else:
                cnt=0
        return False

Guess you like

Origin blog.csdn.net/yeqing1997/article/details/113822119