Leetcode_数组

1.两数之和
题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
在这里插入图片描述
代码

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        L={}
        l=len(nums)
        for i in range(l):
            y=target-nums[i]
            if y not in L:
                L[nums[i]]=i
            else:
                return [L[y],i]

4. 寻找两个有序数组的中位数
题目描述:给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。你可以假设 nums1 和 nums2 不会同时为空。

在这里插入图片描述
代码

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        L=nums1+nums2
        L.sort()
        l=len(L)
        if l%2:
            return L[l//2]
        else:
            return (L[l//2]+L[l//2-1])/2

11.盛最多水的容器
题目描述:给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
说明:你不能倾斜容器,且 n 的值至少为 2。
在这里插入图片描述
代码:

class Solution:
    def maxArea(self, height: List[int]) -> int:
        l=len(height)
        left,right=0,l-1
        maxarea=0
        while left<right:
            maxarea=max(maxarea,min(height[left],height[right])*(right-left))
            if height[left]<height[right]:
                left+=1
            else:right-=1
        return maxarea

12. 整数转罗马数字
题目描述:罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
在这里插入图片描述
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。

代码

class Solution:
    def intToRoman(self, num: int) -> str:
        dictmap={
            1:'I',
            4:'IV',
            5:'V',
            9:'IX',
            10:'X',
            40:'XL',
            50:'L',
            90:'XC',
            100:'C',
            400:'CD',
            500:'D',
            900:'CM',
            1000:'M'    
        }
        res=''
        for key in sorted(dictmap.keys())[::-1]:
            a=num//key
            if a==0:
                continue
            res+=dictmap[key]*a
            num-=key*a
            if num==0:break
        return res
            

26.删除排序数组中的重复项
题目描述:给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
在这里插入图片描述
在这里插入图片描述
代码:

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        l=len(nums)
        if l==0:return 0
        i=0
        for j in range(1,l):
            if nums[j]!=nums[i]:
                i+=1
                nums[i]=nums[j]
        return i+1

27.移除元素
题目描述:给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
在这里插入图片描述
在这里插入图片描述
代码

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        l=len(nums)
        if l==0 or (l==1 and nums[0]==val):return 0
        if l==1 and nums[0]!=val:
            return 1
        i=l-1
        while i>=0:
            if nums[i]==val:
                nums.remove(nums[i])
            i=i-1
        return len(nums)
            
            

34.在排序数组中查找元素的第一个和最后一个位置
题目描述:给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。你的算法时间复杂度必须是 O(log n) 级别。如果数组中不存在目标值,返回 [-1, -1]。
在这里插入图片描述
代码

class Solution(object):
    def searchRange(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        if not nums:return [-1,-1]
        l=len(nums)
        L=[]
        for i in range(l):
            if nums[i]==target:
                L.append(i)
        if len(L)==0:return [-1,-1]
        return [L[0],L[-1]]
            

53.最大子序和
题目描述:给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
在这里插入图片描述
代码

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        cur=0
        maxsum=nums[0]
        l=len(nums)
        for i in range(l):
            cur+=nums[i]
            maxsum=max(maxsum,cur)
            if cur<0:cur=0
        return maxsum

283.移动零
题目描述:给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
在这里插入图片描述
代码:

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        if not nums or len(nums)==0:return []
        length=len(nums)
        j=0
        for i in range(length):
            if nums[i]!=0:
                tmp=nums[i]
                nums[i]=nums[j]
                nums[j]=tmp
                j+=1
        return nums
  1. 两个数组的交集 II
    题目描述:给定两个数组,编写一个函数来计算它们的交集。
    在这里插入图片描述
    代码:
    自己想的解,不够优
from collections import Counter
class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        nums1_dict=Counter(nums1)
        nums2_dict=Counter(nums2)
        res=[]
        for num in nums1_dict.keys():
            if num in nums2_dict.keys():
                number=min(nums1_dict[num],nums2_dict[num])
                res+=[num]*number
        return res
  1. 有效的字母异位词
    题目描述:给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
    在这里插入图片描述
    代码:
from collections import Counter
class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        s_len=len(s)
        t_len=len(t)
        if s_len!=t_len:return False
        s_dict=Counter(s)
        t_dict=Counter(t)
        for letter in s_dict.keys():
            if letter not in t_dict.keys() or s_dict[letter]!=t_dict[letter]:
                return False
        return True
class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if sorted(s)==sorted(t):
            return True
        else:
            return False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值