leetcode day01

链表类:

#88. 合并两个有序数组  //
class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        p1, p2, p = m - 1, n - 1, m + n - 1
        while p2 >= 0:  # nums2 还有要合并的元素
            # 如果 p1 < 0,那么走 else 分支,把 nums2 合并到 nums1 中
            if p1 >= 0 and nums1[p1] > nums2[p2]:
                nums1[p] = nums1[p1]  # 填入 nums1[p1]
                p1 -= 1
            else:
                nums1[p] = nums2[p2]  # 填入 nums2[p1]
                p2 -= 1
            p -= 1  # 下一个要填入的位置


#160 相交链表  //双指针
class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        A,B=headA,headB
        while A!=B:
            A=A.next if A else headB
            B=B.next if B else headA
        return A


# 头插法
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        newhead= ListNode()  #这里应该实例化一个头节点,不然会陷入死循环 
        while head:
            next=head.next
            head.next=newhead.next
            newhead.next=head
            head=next
        return newhead.next

# 合并列表 //递归
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        if not l1: return l2  # 终止条件,直到两个链表都空
        if not l2: return l1
        if l1.val <= l2.val:  # 递归调用
            l1.next = self.mergeTwoLists(l1.next,l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1,l2.next)
            return l2

#27 移除元素  //快慢指针
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        slow=0
        fast=0
        while fast<len(nums):
            if nums[fast]!=val:
                nums[slow]=nums[fast]
                slow+=1
            fast+=1
        return slow

#83 删除重复元素 



#80. 删除有序数组中的重复项 II



#88. 合并两个有序数组



#26 删除有序数组重复项
#即定义两个指针left和right,right从左到右把所有元素扫一遍,将不重复的元素赋给left的下一位

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        left=0
        right=1
        n=len(nums)
        if n<=1:
            return n
        while right<len(nums):
            if nums[right]==nums[right-1]:
                right+=1
            else:
                nums[left+1]=nums[right]
                left+=1
                right+=1
        return left+1

#169 众数
class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        votes,count=0,0
        for num in nums:
            if votes==0:
                x=num
            votes+=1 if num==x else -1
        return x


#括号有效性
class Solution:
    def isValid(self, s: str) -> bool:
        dic = {')':'(',']':'[','}':'{'}
        stack = []
        for i in s:
            if stack and i in dic:
                if stack[-1] == dic[i]: stack.pop()
                else: return False
            else: stack.append(i)
        return not stack


#回文数
class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x<0:
            return False
        cur=0
        num=x
        while(num!=0):
            cur=cur*10+num%10
            num=int(num/10)
        return cur==x

#回文字符 上不得一点台面
        ret = []
        for i in s:
            if i.isalnum():
                ret.append(i.lower())
        return ret == ret[::-1]

#35 二分法 找插入位置
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        left,right=0,len(nums)
        while left<right:
            mid=left+((right-left)>>1)
            if nums[mid]==target:
                return mid
            elif nums[mid]>target:
                right=mid
            else:
                left=mid+1
        return right


def isvalid(s):
    res = []
    dic = {'(':')','[':']','{':'}'}
    for i in s:
        if i in dic:
            res += i
        elif res and i==dic[res[-1]]:
            res.pop()
        else:
            return False
    return True

posted @ 2024-02-20 18:01  yituozhi  阅读(24)  评论(0)    收藏  举报