栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

leetcode刷题指南 pdf_leetcode必刷题?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode刷题指南 pdf_leetcode必刷题?

参考的刷题顺序:代码随想录

数组 1.二分查找

题目链接
https://leetcode-cn.com/problems/binary-search/
python代码
代码1:

class Solution(object):
    def search(self, nums, target):
           """
        :type nums: List[int]
        :type target: int
        :rtype: int
        左闭右开区间
        """
        right,left=len(nums),0
        while left < right:
            key=(right+left)/2
            if nums[key] < target:
                left=key+1
            elif nums[key] > target:
                right=key
            else :
                return key
        return -1

代码2

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        左闭右闭区间
        """
        right,left=len(nums)-1,0
        while left<=right:
            key=(right+left)/2
            if nums[key] < target:
                left=key+1
            elif nums[key] > target:
                right=key-1
            else :
                return key
        return -1
c++代码

```cpp
在这里插入代码片
相关题目: 35.搜索插入位置

力扣链接
解法一:暴力解法
c++

class Solution {
public:
    int searchInsert(vector& nums, int target) {
        for (int i = 0; i < nums.size(); i++) {
        // 分别处理如下三种情况
        // 目标值在数组所有元素之前
        // 目标值等于数组中某一个元素
        // 目标值插入数组中的位置
            if (nums[i] >= target) { // 一旦发现大于或者等于target的num[i],那么i就是我们要的结果
                return i;
            }
        }
        // 目标值在数组所有元素之后的情况
        return nums.size(); // 如果target是最大的,或者 nums为空,则返回nums的长度
    }
};

解法二:二分法
python

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        left,right=0,len(nums)-1
        while left<=right:
            mid=(left+right)/2
            if nums[mid]target:
                right=mid-1
            else:
                return mid
        #插在头部
        #下面这部分可直接写成return right+1
        if nums[0]>target:
            return 0
        elif nums[len(nums)-1] 
34. 在排序数组中查找元素的第一个和最后一个位置 

力扣题目

python代码
解法一:

class Solution(object):
    def searchRange(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        #二分查找看是否存在target
        def binlookTarget(nums,target):
            left,right=0,len(nums)-1
            while left<=right:
                mid=(left+right)/2
                if nums[mid] < target:
                    left=mid+1
                elif nums[mid] > target:
                    right=mid-1
                else:
                    return mid
            return -1        
        
        s1=s2=binlookTarget(nums,target)

        if s1==-1:
            return [-1,-1]
        else :
        	#根据找出的target下标左右滑动找出边界
            while s1-1>=0 and nums[s1-1]==target :#注意这两个条件的先后顺序不能反,不然会出现超过list维度的错误
                s1-=1#找出左边界
            while s2+1 
69.x 的平方根 

python代码:
解法一:

class Solution(object):
    def mySqrt(self, x):

        """
        :type x: int
        :rtype: int
        """
      #用二分法来求解
        left,right=0,x
        ans=0
        while left<=right:
            mid=(right+left)/2
            if mid*mid > x:
                right=mid-1
            else: 
                left=mid+1
                ans=mid
        return ans

解法二:
这种情况x=1时要单独讨论,x=1时mid会等于0,不然会输出0

class Solution(object):
    def mySqrt(self, x):

        """
        :type x: int
        :rtype: int
        """
      #用二分法来求解
        left,right=0,x
        if x==1:
            return 1
        else:
            while left<=right:
                mid=(right+left)/2
                if mid*mid > x:
                    right=mid
                    if left==right-1:
                   
                        return left
                elif mid*mid < x:
                    left=mid
                
                    if left==right-1:
                    
                        return left
                else:
                    return mid
367.有效的完全平方数

python代码:

class Solution(object):
    def isPerfectSquare(self, num):
        """
        :type num: int
        :rtype: bool
        """
        left,right=1,num
        while left<=right:
            mid=(left+right)//2
            if mid*mid>num:
                right=mid-1
            elif mid*mid 
2.移除元素 

题目链接
https://leetcode-cn.com/problems/remove-element/submissions/
python代码

法一:暴力求解

踩坑记录
先用python写了下面这段代码:

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        #法一:暴力解法,两层循环,找到后直接覆盖
        k=len(nums)
        for i in range(k):
            if nums[i]==val:
                for j in range(i+1,k):
                    nums[j-1]=nums[j]
                k=k-1
                i=i-1
        return k

发现提交上去是错的,于是在vscode里面找了下原因:

原因:Python在使用for循环的时候不能修改循环中使用的变量
要想改变变量的值的话用while循环

正确python代码

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        k=len(nums)
        i=0
        while i 
法二:双指针 
在这里插入代码片

c++代码

在这里插入代码片
3.二分查找

题目链接
https://leetcode-cn.com/problems/binary-search/
python代码

在这里插入代码片

c++代码

在这里插入代码片
4.二分查找

题目链接
https://leetcode-cn.com/problems/binary-search/
python代码

在这里插入代码片

c++代码

在这里插入代码片
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786630.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号