记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
- 7/31 143. 重排链表
- 8/1 2681. 英雄的力量
- 8/2 822. 翻转卡片游戏
- 8/3 722. 删除注释
- 8/4 980. 不同路径 III
- 8/5
- 8/6
7/31 143. 重排链表
快慢指针找到链表中间位置
反转后半截链表
再将两段链表拼接
class ListNode(object):def __init__(self, val=0, next=None):self.val = valself.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:returnemp = ListNode()emp.next = headslow,fast = emp,empend = fastwhile fast:slow = slow.nextend = fastfast = fast.nextif not fast:breakend=fastfast = fast.next#反转 slow <-endpre = slownow = slow.nextwhile now!=end:n = now.nextnow.next = prepre=nownow=nnow.next = preleft = headright = endwhile left!=right:nl = left.nextleft.next = rightleft = nlif left==right:breaknr=right.nextright.next=leftright=nrleft.next=None
8/1 2681. 英雄的力量
顺序不影响结果 将nums排序
枚举每个元素作为子序列最小值的贡献
def sumOfPower(nums):""":type nums: List[int]:rtype: int"""mod=10**9+7nums.sort()ans = 0p = 0for x in nums[::-1]:ans = (ans+(x*x%mod)*x)%modans = (ans+x*p)%modp = (p*2+x*x)%modreturn 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]]=1ans = 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 = Falsecur = ""ans = []for s in source:n = len(s)if not rm:cur +=s[0]i = 1while i<n:c = s[i-1:i+1]if rm:if c=="*/":i+=1if i<n:cur+=s[i]rm=False else: if c=="//":cur = cur[:-1]breakelif c=="/*":cur = cur[:-1]rm=Truei+=2continuecur+=s[i]i+=1if 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)-1mem = {}sx,sy = 0,0path = 0for 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,jdef dfs(x,y,path):if x<0 or x>=n or y<0 or y>=m or path>>(x*m+y)&1:return 0path |= 1<<(x*m+y)if grid[x][y]==2:v=path==finishmem[(x,y,path)]=vreturn vv = dfs(x-1,y,path)+dfs(x,y-1,path)+dfs(x+1,y,path)+dfs(x,y+1,path)mem[(x,y,path)]=vreturn vreturn dfs(sx,sy,path)
8/5
8/6