leetcode100HOT

博主分享了三种不同时间复杂度的解决方案,包括两数之和的迭代和查找优化,以及链表加法问题的实现。讨论了ListNode类在LeetCode中遇到的难点,并提供了逐步解析和自理解析版本。关键词:两数之和、链表操作、时间复杂度优化

本人菜鸟 边刷边进步

在这里插入图片描述
链接:https://leetcode-cn.com/playground/new/empty/

# first  2204ms
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for i in range(len(nums)):
            for j in range(i+1,len(nums)):
                if nums[i] + nums[j] == target:
                    return i,j
# second 412ms
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for index, num in enumerate(nums):
            if target - num in nums and nums.index(target - num) != index:
                return nums.index(target - num), index
# third  12ms
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        dict_ = {}
        for index, num in enumerate(nums):
            if target - num in dict_:
                return dict_[target - num], index
            dict_[num] = index

20220223
在这里插入图片描述
链接 https://leetcode-cn.com/problems/add-two-numbers/
这道题在ListNode()难倒了 想了很久 没弄懂,看看睡觉前能不能弄懂
下午才弄懂,自己写了个简单版

#         摘抄网友的
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        head = curr = ListNode()
        carry = val = 0

        while carry or l1 or l2:
            val = carry

            if l1: l1, val = l1.next, l1.val + val
            if l2: l2, val = l2.next, l2.val + val

            carry, val = divmod(val, 10)
            curr.next = curr = ListNode(val)
        
        return head.next

# my 理解 version   64ms
class Solution:  
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        return_node = None
        # 初始化进位的值
        jinwei = 0
        # 将结果保存起来 方便后面做逆序
        result = []
        while l1 or l2 or jinwei:
            # 这里需要将进位的值赋值给val,加上进位的值
            val = jinwei
            if l1:
                # 取出数据,并且l1指向l1.next
                val = l1.val + jinwei
                l1 = l1.next
            if l2:
                # 取出数据,并且l2指向l2.next
                val = val + l2.val
                l2 = l2.next
            # 获得进位
            jinwei, yushu = divmod(val, 10)
            result.append(yushu)
        for ii in reversed(result):
            # 新建ListNode()结点
            new = ListNode()
            new.val = ii
            new.next = return_node
            # 将return_node指向new结点
            return_node = new
        return return_node
### LeetCode Hot 100 Problems for Practice LeetCode提供了一系列热门练习题,这些题目被广泛认为对于准备技术面试非常有帮助。以下是部分精选的Hot 100问题列表: #### 数组与字符串 - **两数之和 (Two Sum)**: 给定一个整数数组`nums`和一个目标值`target`,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标[^1]。 ```python def twoSum(nums, target): hashmap = {} for i in range(len(nums)): complement = target - nums[i] if complement in hashmap: return [hashmap[complement], i] hashmap[nums[i]] = i ``` - **移动零 (Move Zeroes)**: 将给定数组中的所有零移到数组末尾,同时保持其他元素顺序不变[^2]. ```python class Solution(object): def moveZeroes(self, nums): slow, fast = 0, 0 n = len(nums) while fast < n: if nums[fast] != 0: nums[slow], nums[fast] = nums[fast], nums[slow] slow += 1 fast += 1 return nums ``` #### 链表操作 - **反转链表 (Reverse Linked List)**: 反转单向链表。 ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def reverseList(head): prev = None curr = head while curr is not None: next_temp = curr.next curr.next = prev prev = curr curr = next_temp return prev ``` #### 动态规划 - **爬楼梯 (Climbing Stairs)**: 计算到达楼顶的方法总数。 ```python def climbStairs(n): if n == 1 or n == 2: return n first, second = 1, 2 for _ in range(3, n + 1): third = first + second first = second second = third return second ``` 这些问题涵盖了算法设计的核心概念和技术栈的关键方面,非常适合用来提升编程技能和解决问题的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kui9702

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值