python数据结构实现(一):数组和链表及相关LeetCode题

本文探讨Python中的数组实现,包括动态扩容和固定容量有序数组,以及如何合并有序数组。同时,讲解了链表的实现,如单链表、循环链表和双向循环链表,并介绍了相关LeetCode题目,如两数之和、三数之和、链表环形检测等。
摘要由CSDN通过智能技术生成

一:数组

1. 线性表的顺序存储结构一般特性:

  • 支持随机访问
  • 占用连续的存储空间,长度不可改变

python本身没有数组结构,虽然列表可以完成数组的基本功能,也可通过嵌套实现多维列表,但是当数据量较大时,速度会变慢。通常我们可以利用array模块或Numpy里封装的array()创建数组。

2. python实现支持动态扩容的数组

在这里,基于Python的列表实现动态数组的创建。

class DynamicArr:
    def __init__(self, capacity=20):
        '''
        构造函数
        :param capacity:数组容量大小,默认为20
        '''
        self._capacity = capacity
        self._length = 0      # 数组的有效长度
        # 基于列表实现,通过赋值None实现底层具有连续的内存空间,容量固定的数组
        self._data = [None] * self._capacity

    def __getitem__(self, index):
        '''使得DynamicArr类实例化对象支持索引随机访问'''
        return self._data[index]

    def __setitem__(self, index, value):
        '''使得DynamicArr类实例化对象支持直接赋值'''
        self._data[index] = value

    def getLength(self):
        '''返回当前数组长度'''
        return self._length

    def getCapacity(self):
        '''返回数组容量'''
        return self._capacity

    def add(self, index, value):
        '''
        往数组增加一个元素
        :param index:待添加元素在数组的目标下标
        :param value:待添加元素的值
        
        '''
        if index < 0 or index > self._length:  # 无效的插入位置
            raise Exception('invaild index: require 0 <= index <= self._size')
        if self._length == self._capacity:  # 数组已满
            # 将数组容量扩充至两倍,比单独扩充一个单位要好,均摊复杂度为O(1)
            self._resize(self._capacity*2)  
            
        for i in range(self._size, index-1, -1):
            self._data[i+1] = self._data[i]
        self._data[index] = value
        self._length += 1

    def addLast(self, value):    # 往数组的末尾添加元素
        return self.add(self._length, value)

    def _resize(self, newCapacity):
        '''
        重构数组的容量
        :param newCapacity:新的容量大小
        
        '''
        newArr = DynamicArr(newCapacity)
        for i in range(self._length):
            newArr.addLast(self._data[i])
        self._capacity = newArr._capacity
        self._data = newArr._data

3.python实现固定容量的有序数组

class StaticOrderArr:
    def __init__(self, capacity=20):    # 数组默认容量为20
        self._capacity = capacity
        self._length = 0
        self._data = [None] * self._capacity

    def __getitem__(self, index):
        return self._data[index]

    def __setitem__(self, index, value):
        '''使数组支持更改操作'''
        if index <= self._length and index >= 0:
            if index == 0 or index == self._length-1:
                self._data[index] = value
            else:
                if self._data[index-1] <= value and self._data[index+1] >= value:
                    self._data[index] = value
                else:
                    raise Exception('set failed: require value meets the demand of order')
        else:
            raise Exception('set failed: invaild index')

    def add(self, value):
        '''
        往有序数组添加值为value的元素
        '''
        if self._length == self._capacity:
            raise Exception('The arrary is full')
        elif self._length == 0:
            self._data[0] = value
            self._length += 1
        else:
            for i in range(self._length-1, -1, -1):
                if self._data[i] > value:
                    self._data[i+1] = self._data[i]
                else:
                    self._data[i+1] = value
                    self._length += 1
                    break
        if self._data[0] > value:
                self._data[0] = value
                self._length += 1
                
    def remove(self, index):
        '''删除有序数组中下标为index的元素'''
        if self._length == 0:
            raise Exception('remove failed: Array is empty')
        if index < 0 or index > self._length:
            raise Exception('invaild index,require
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值