LeetCode 每日一题 2023/7/31-2023/8/6

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步


目录

      • 7/31 143. 重排链表
      • 8/1 2681. 英雄的力量
      • 8/2 822. 翻转卡片游戏
      • 8/3 722. 删除注释
      • 8/4 980. 不同路径 III
      • 8/5 21. 合并两个有序链表
      • 8/6 24. 两两交换链表中的节点


7/31 143. 重排链表

快慢指针找到链表中间位置
反转后半截链表
再将两段链表拼接

class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
def reorderList(head):
    """
    :type head: ListNode
    :rtype: None Do not return anything, modify head in-place instead.
    """
    if not head or not head.next or not head.next.next:
        return
    emp = ListNode()
    emp.next = head
    slow,fast = emp,emp
    end = fast
    while fast:
        slow = slow.next
        end = fast
        fast = fast.next
        if not fast:
            break
        end=fast
        fast = fast.next
    #反转 slow <-end
    pre = slow
    now = slow.next
    while now!=end:
        n = now.next
        now.next = pre
        pre=now
        now=n
    now.next = pre
    
    left = head
    right = end
        
    while left!=right:
        nl = left.next
        left.next = right
        left = nl
        if left==right:
            break
        nr=right.next
        right.next=left
        right=nr
    left.next=None



8/1 2681. 英雄的力量

顺序不影响结果 将nums排序
枚举每个元素作为子序列最小值的贡献

def sumOfPower(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    mod=10**9+7
    nums.sort()
    ans = 0
    p = 0
    for x in nums[::-1]:
        ans = (ans+(x*x%mod)*x)%mod
        ans = (ans+x*p)%mod
        p = (p*2+x*x)%mod
    return ans



8/2 822. 翻转卡片游戏

如果正反面数字相同的必定不会是答案
记录正反面相同的数字
在剩余的数字中找最小值

def flipgame(fronts, backs):
    """
    :type fronts: List[int]
    :type backs: List[int]
    :rtype: int
    """
    m = {}
    for i in range(len(fronts)):
        if fronts[i]==backs[i]:
            m[fronts[i]]=1
    ans = float("inf")
    for v in fronts+backs:
        if v not in m:
            ans = min(ans,v)
    return ans if ans!=float("inf") else 0




8/3 722. 删除注释

模拟 rm标记当前是否在/*内
cur为当前字符串

def removeComments(source):
    """
    :type source: List[str]
    :rtype: List[str]
    """
    rm = False
    cur = ""
    ans = []
    for s in source:
        n = len(s)
        if not rm:
            cur +=s[0]
        i = 1
        while  i<n:
            c = s[i-1:i+1]
            if rm:
                if c=="*/":
                    i+=1
                    if i<n:
                        cur+=s[i]
                    rm=False                    
            else:  
                if c=="//":
                    cur = cur[:-1]
                    break
                elif c=="/*":
                    cur = cur[:-1]
                    rm=True
                    i+=2
                    continue
                cur+=s[i]
            i+=1
        if not rm and cur!="":
            ans.append(cur)
            cur=""
    return ans



8/4 980. 不同路径 III

使用二进制数标记每一个坐标是否经过 x,y 可以标记为第mx+y位
finish为所有位置经过后的状态 每一位为1
path为当前经过的位置 初始化将-1标记为已经过
sx,sy记录起始点

def uniquePathsIII(grid):
    """
    :type grid: List[List[int]]
    :rtype: int
    """
    n,m = len(grid),len(grid[0])
    finish = (1<<m*n)-1
    mem = {}
    sx,sy = 0,0
    path = 0
    for i,row in enumerate(grid):
        for j,v in enumerate(row):
            if v<0:
                print(i,j)
                path |= 1<<(i*m+j)
            elif v==1:
                sx,sy=i,j
    def dfs(x,y,path):
        if x<0 or x>=n or y<0 or y>=m or path>>(x*m+y)&1:
            return 0
        path |= 1<<(x*m+y)
        if grid[x][y]==2:
            v=path==finish
            mem[(x,y,path)]=v
            return v
        v = dfs(x-1,y,path)+dfs(x,y-1,path)+dfs(x+1,y,path)+dfs(x,y+1,path)
        mem[(x,y,path)]=v
        return v
    
    return dfs(sx,sy,path)



8/5 21. 合并两个有序链表

双指针依次比较

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def mergeTwoLists(list1, list2):
    """
    :type l1: ListNode
    :type l2: ListNode
    :rtype: ListNode
    """
    ret = ListNode(0)
    node = ret
    while list1 and list2:
        if list1.val<list2.val:
            node.next = list1
            list1 = list1.next
        else:
            node.next =list2
            list2 = list2.next
        node = node.next
    node.next = list1 or list2
    return ret.next



8/6 24. 两两交换链表中的节点

递归 判断是否存在两两节点

class ListNode:
     def __init__(self, x):
         self.val = x
         self.next = None
def swapPairs(head):
    """
    :type head: ListNode
    :rtype: ListNode
    """
    if not head or not head.next:
        return head
    tmp = swapPairs(head.next.next)
    res = head.next
    res.next = head
    head.next = tmp
    return res


你可能感兴趣的:(Exercise,leetcode,算法)