字符串
LC.122 路径加密
class Solution:
def pathEncryption(self, path: str) -> str:
ss = path.split('.')
return ' '.join(ss)
LCR 182.动态口令
class Solution:
def dynamicPassword(self, password: str, target: int) -> str:
return password[target:] + password[:target]
LCR138.有效数字【?】
class Solution:
def validNumber(self, s: str) -> bool:
states = [
{ ' ': 0, 's': 1, 'd': 2, '.': 4 }, # 0. start with 'blank'
{ 'd': 2, '.': 4 } , # 1. 'sign' before 'e'
{ 'd': 2, '.': 3, 'e': 5, ' ': 8 }, # 2. 'digit' before 'dot'
{ 'd': 3, 'e': 5, ' ': 8 }, # 3. 'digit' after 'dot'
{ 'd': 3 }, # 4. 'digit' after 'dot' (‘blank’ before 'dot')
{ 's': 6, 'd': 7 }, # 5. 'e'
{ 'd': 7 }, # 6. 'sign' after 'e'
{ 'd': 7, ' ': 8 }, # 7. 'digit' after 'e'
{ ' ': 8 } # 8. end with 'blank'
]
p = 0 # start with state 0
for c in s:
if '0' <= c <= '9': t = 'd' # digit
elif c in "+-": t = 's' # sign
elif c in "eE": t = 'e' # e or E
elif c in ". ": t = c # dot, blank
else: t = '?' # unknown
if t not in states[p]:
return False
p = states[p][t]
return p in (2, 3, 7, 8)
LCR192.把字符串转换成整数
链表
LCR123. 图书整理I
递归
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if not head:
return []
else:
return self.reverseBookList(head.next) + [head.val]
辅助栈
class Solution:
def reverseBookList(self, head: ListNode) -> List[int]:
stack = []
while head:
stack.append(head.val)
head = head.next
return stack[::-1]
LCR141.训练计划3
class Solution:
def trainningPlan(self, head: ListNode) -> ListNode:
cur, pre = head, None
while cur:
tmp = cur.next # 暂存后继节点 cur.next
cur.next = pre # 修改 next 引用指向
pre = cur # pre 暂存 cur
cur = tmp # cur 访问下一节点
return pre
LCR154.复杂链表的复制
LCR136.删除链表的节点![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/0f200d82acdb482a893ddb03abb2437a.png)
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
dummyHead = ListNode(-1, head)
cur = dummyHead
while cur.next:
if cur.next.val == val:
cur.next = cur.next.next
break
else:
cur = cur.next
return dummyHead.next