LeetCode---【和的操作】

目录

两数之和

我的答案

python 复制代码
class Solution(object):
    def twoSum(self, nums, target):
        leng=len(nums)
        for i in range(leng):
            for j in range(leng):
                if nums[i]+nums[j]==target and i!=j:
                    return [i,j]

在b站up那里学到的【然后自己复写】

  • 忘记怎么取list的第二个值了。【利用range的特性】
python 复制代码
class Solution(object):
    def twoSum(self, nums, target):
        leng=len(nums)
        for i in range(0,leng):
            for j in range(i+1,leng):
                if nums[i]+nums[j]==target and i!=j:
                    return [i,j]
  • 哈希表【空间换时间】
python 复制代码
class Solution(object):
    def twoSum(self, nums, target):
        mapping = {}
        for i in range(0, len(nums)):
            mapping[nums[i]] = i
        for j in range(0, len(nums)):
            diff = target - nums[j]
            if (diff in mapping and mapping[diff] != j):
                return [j,mapping[diff]];

和为 K 的子数组

在b站up那里学到的【然后自己复写】

python 复制代码
class Solution(object):
    def subarraySum(self, nums, k):
        count = 0
        dic={}
        dic[0] = 1
        cur_sum = 0
        for num in nums:
            cur_sum = cur_sum + num
            if cur_sum-k in dic:# 很巧的思路
                count=count+dic[cur_sum-k]
            if cur_sum in dic:
                dic[cur_sum] +=1
            else:
                dic[cur_sum] = 1
        return count        

三数之和

在b站up那里学到的【然后自己复写】

python 复制代码
class Solution(object):
    def threeSum(self, nums):
        resol=[]
        nums.sort()
        for i in range(len(nums)):
            cur = i
            if nums[cur]>0:
                return resol
            if cur>0 and nums[cur-1] == nums[cur]:
                continue # 去重
            left = cur+1
            right = len(nums)-1
            while left<right:
                total = nums[cur]+nums[left]+nums[right]
                if total >0:
                    right-=1
                elif total<0:
                    left+=1
                else:
                    resol.append([nums[cur],nums[left],nums[right]])
                    while left<right and nums[right-1]==nums[right]:# 去重
                        right-=1
                    while left<right and nums[left+1]==nums[left]:# 去重
                        left+=1
                    right-=1
                    left+=1               
        return resol

两数相加【链表】

我的半路答案:没有看到是链表

  • 我还没有写完,只是想半路测一下是否正确。
  • 关于链表的话,我相对还是弱一点,下次巩固。
python 复制代码
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        resl=[]
        cur=0
        len1=len(l1)
        len2=len(l2)
        for i in range(min(len1,len2)):
            total=l1[i]+l2[i]+cur
            if total>10:
                cur=total/10
                resl.append(total%10)
            else:
                resl.append(total)
        return resl

在b站up那里学到的【复写失败后整理】

python 复制代码
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        total = 0
        next1 = 0
        dummy = ListNode()
        cur = dummy#############必须得要一个临时变量,因为后面输出是从头开始输出
        while (l1 != None and l2 != None):
            total = l1.val + l2.val + next1
            cur.next = ListNode(total % 10)
            next1 = total // 10
            cur = cur.next##################不要忘记
            l1 = l1.next
            l2 = l2.next
        
        while l1 != None:
            total = l1.val + next1
            cur.next = ListNode(total % 10)
            next1 = total // 10
            cur = cur.next##################不要忘记
            l1 = l1.next
        
        while l2 != None:
            total = l2.val + next1
            cur.next = ListNode(total % 10)
            next1 = total // 10
            cur = cur.next##################不要忘记
            l2 = l2.next
        
        if next1 != 0:
            cur.next = ListNode(next1)
        
        return dummy.next
相关推荐
数字化脑洞实验室1 小时前
如何理解不同行业AI决策系统的功能差异?
大数据·人工智能·算法
阿郎_20111 小时前
python自动化脚本-简化留言
python·自动化
人邮异步社区1 小时前
推荐几本学习计算机语言的书
java·c语言·c++·python·学习·golang
小白菜又菜4 小时前
Leetcode 3370. Smallest Number With All Set Bits
算法·leetcode·职场和发展
星谷罗殇5 小时前
(七)TRPO 算法 & PPO 算法
算法·机器学习
gfdgd xi5 小时前
GXDE 内核管理器 1.0.1——修复bug、支持loong64
android·linux·运维·python·ubuntu·bug
递归不收敛5 小时前
专属虚拟环境:Hugging Face数据集批量下载(无登录+国内加速)完整指南
人工智能·笔记·git·python·学习·pycharm
我是小邵5 小时前
主流数据分析工具全景对比:Excel / Python / R / Power BI / Tableau / Qlik / Snowflake
python·数据分析·excel
国服第二切图仔6 小时前
Rust开发之使用Trait对象实现多态
开发语言·算法·rust
Yolo566Q6 小时前
Python驱动的无人机生态三维建模与碳储/生物量/LULC估算全流程实战技术
开发语言·python·无人机