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
相关推荐
大怪v27 分钟前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
数据智能老司机1 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i2 小时前
django中的FBV 和 CBV
python·django
c8i2 小时前
python中的闭包和装饰器
python
惯导马工2 小时前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农4 小时前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了4 小时前
AcWing学习——双指针算法
c++·算法
moonlifesudo5 小时前
322:零钱兑换(三种方法)
算法
这里有鱼汤5 小时前
小白必看:QMT里的miniQMT入门教程
后端·python
TF男孩15 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列