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

数据结构 python

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

数据结构 python

临近考试 刷课后给的leetcode题 题目详细不再说 详情看leetcode官网

题库 - 力扣 (LeetCode) 全球极客挚爱的技术成长平台https://leetcode.cn/problemset/all/

leetcode1-两数之和

暴力迭代 和减数 然后全部查找

'''
给定一个整数数组与目标值,求数组中和为目标值的两个整数并返回数组下标
'''
class Solution:
    def twoSum(self,nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        #用len()方法取得nums列表长度
        n = len(nums)
        #x从0到n取值(不包括n)
        for x in range(n):
            a = target - nums[x]
            #用in关键字查询nums列表中是否有a
            if a in nums:
                #用index函数取得a的值在nums列表中的索引
                y = nums.index(a)
                #假如x=y,那么就跳过,否则返回x,y
                if x == y:
                    continue
                else:
                    return x,y
                    break
            else :
                continue
a=Solution()
nums=[2,7,11,15]
target=9
print(a.twoSum(nums,target))

leetcode9-回文数

 依然暴力

class Soultion():
    def isPalinedrome(self,x):
        x=int(x)
        if x<0:
            return False
        else:
            l=str(x)
            n=len(l)
            for x in range(int(n / 2)):
                if l[x] == l[n-x-1]:
                    continue
                else:
                    return False
                    break
            return True
a=Soultion()
print(a.isPalinedrome(1211))

线性表和链表 个人感觉就是指针的使用

Leetcode27-移除元素 

设计两个指针 快指针标记下标找值,快指针指值

class Solution:
    def removeElement(self, nums, val):
        # 快慢指针
        if not nums:
            return 0
        # 排序
        nums.sort()
        slow, fast = 0, 0
        # fast越界即停止
        while fast < len(nums):
            if nums[fast] != val:
                # 将不重复的值slow加一,即为所求的长度
                nums[slow] = nums[fast]
                slow += 1
            fast += 1
        return slow

a=Solution()
nums=[3,2,2,3]
val=3
print(a.removeElement(nums,val))

 Leetcode26-删除排序数组中的重复项

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

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

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