leetcode-python-day1

1. 两数之和 - 力扣(LeetCode)

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标

方法一:两个for循环,1400ms

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        if nums is None or len(nums)==0:
            return 0
        for index1 in range(len(nums)):
            for index2 in range(index1+1,len(nums)):
                if nums[index1]+nums[index2]==target:
                    return [index1,index2]

solution= Solution()
nums = [2,7,11,15]
target = 9
result = solution.twoSum(nums,target)
print(result)

方法二:取一个数,执行target-nums[i],判断余数是否在数组内,311ms。

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for i in range(len(nums)):
            res = target - nums[i]
            if res in nums[i+1:]:  #不直接使用nums避免res和nums[i]重复
                #在列表中,index() 方法会返回指定元素首次出现的索引
                return [i,nums[i+1:].index(res)+i+1]


solution= Solution()
nums = [2,7,11,15]
target = 9
result = solution.twoSum(nums,target)
print(result)

11. 盛最多水的容器 - 力扣(LeetCode)

给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。返回容器可以储存的最大水量。

解析:设置双指针light和left,在指针移动过程中要满足light<left,因为当light=left时不再构成容器

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        l,r = 0,len(height)-1
        max_area = 0
        area = 0
        while l<r:
            width = r-l
            area = width *min(height[l],height[r])
            max_area = max(area,max_area)
        #让短的线向高的线靠近
            if height[l]<height[r]:
                l+=1
            else:
                r-=1
        return max_area

solution = Solution()
height=[1,8,6,2,5,4,8,3,7]
result = solution.maxArea(height)
print(result)

26. 删除有序数组中的重复项 - 力扣(LeetCode)

给你一个 非严格递增排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。

  if nums is None:
      return 0
  count = 1  # 计数器,初始化为1,因为至少有一个元素不重复  
  for i in range(1,len(nums)):
      if nums[i-1]!=nums[i]:
      #满足则该元素与前一个元素不重复
           nums[count]=nums[i]
           count+=1
   return count

27. 移除元素 - 力扣(LeetCode)

给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        if nums is None or len(nums) ==0:
            return 0
        l,r = 0,len(nums)-1 #一开始,l指向最左边,R指向最右边
        while l < r:
            while l<r and nums[l] != val: 
                l = l+1
            while l<r and nums[r] == val:
                r = r-1
            nums[l],nums[r] = nums[r],nums[l]
        return l if nums[l] == val else l+1

       
  
# 示例使用  
solution = Solution()
nums = [3, 2, 2, 3]  
val = 3  
new_len=solution.removeElement(nums, val)    
print(nums[:new_len])

283. 移动零 - 力扣(LeetCode)

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

分析:先计算nums的长度,设置一个索引index=0,遍历nums,将nums[i]不为0的数赋值给

num[index],直到遍历完整个数组,再将缺少的元素用0在末尾填满。

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        index = 0
        for num in nums:
            if num!=0:
                nums[index]=num
                index+=1
        number = len(nums) - index
        for i in range(index,len(nums)):
            nums[i]=0

solution = Solution()  # 创建Solution类的实例 
nums=[0,1,0,3,12]
solution.moveZeroes(nums)  # 调用类实例的方法,就地修改nums
print(nums)

485. 最大连续 1 的个数 - 力扣(LeetCode)

给定一个二进制数组 nums , 计算其中最大连续 1 的个数

class Solution(object):
    def findMaxConsecutiveOnes(self,nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return 0
        count,result=0,0
        for num in nums:
            if num==1:
                count+=1
            else:
                result =max(count,result)
                count = 0
        result =max(count,result)
        return result
    
solution = Solution()  # 创建Solution类的实例  
nums = [1, 1, 0, 0, 1, 1, 1]  
number = solution.findMaxConsecutiveOnes(nums)  # 调用类实例的方法  
print(number)  # 输出结果

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值