leetcode-4.14[445. 两数相加 II、798. 得分最高的最小轮调、面试题 01.02. 判定是否互为字符重排](python解法)

题目1在这里插入图片描述

题解1

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
     def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        stack1 = []
        stack2 = []
        # 创建一个虚假节点
        dummy = ListNode(-1)

        def push_stack(p, stack):
            while p:
                stack.append(p.val)
                p = p.next
        # 利用列表创建两个后进先出栈        
        push_stack(l1, stack1)
        push_stack(l2, stack2)
        # 记录进位
        carry = 0
        while stack1 or stack2 or carry:
            tmp1, tmp2 = 0, 0
            if stack1:
                tmp1 = stack1.pop()
            if stack2:
                tmp2 = stack2.pop()
            carry, mod = divmod(tmp1 + tmp2 + carry, 10)
            # 头插法
            new_node = ListNode(mod)
            new_node.next = dummy.next
            dummy.next = new_node
        return dummy.next

题目2

在这里插入图片描述

题解2

from collections import deque, defaultdict

class Solution:
    # def bestRotation(self, A: List[int]) -> int:
    #     dq = deque()
    #     res = -1
    #     max_temp = 0
    #     now_temp = 0

    #     for i in A:
    #         dq.append(i)
            
    #     for i in range(len(dq)):
    #         for j in range(len(dq)):
    #             if dq[j] <= j:
    #                 now_temp += 1
    #         if now_temp > max_temp:
    #             res = i
    #             max_temp = now_temp
    #         now_temp = 0
    #         temp = dq.popleft()
    #         dq.append(temp)
    #     return res

    def bestRotation(self, A):
        #score[k]:表示移动K步后,当前分数应该加几分,正数为加,负数为扣
        score = [0] * len(A)
        for i in range(len(A)):
            score[(i + len(A) - A[i] + 1) % len(A)] -= 1
        for i in range(1, len(A)):
            score[i] += score[i - 1] + 1
        return score.index(max(score))

题目3

在这里插入图片描述

题解3

class Solution:
    def CheckPermutation(self, s1: str, s2: str) -> bool:
        # 集合对称差集
        return not bool(set(s1) ^ set(s2))

附上题目链接:

题目1链接
题目2链接
题目3链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值