美文网首页
图解算法数据结构-数组

图解算法数据结构-数组

作者: 不到7不改名 | 来源:发表于2023-11-05 21:18 被阅读0次

文件组合

待传输文件被切分成多个部分,按照原排列顺序,每部分文件编号均为一个 正整数(至少含有两个文件)。传输要求为:连续文件编号总和为接收方指定数字 target 的所有文件。请返回所有符合该要求的文件传输组合列表。

注意,返回时需遵循以下规则:

每种组合按照文件编号 升序 排列;
不同组合按照第一个文件编号 升序 排列。

示例1:

输入:target = 12
输出:[[3, 4, 5]]
解释:在上述示例中,存在一个连续正整数序列的和为 12,为 [3, 4, 5]。

示例2:

输入:target = 18
输出:[[3,4,5,6],[5,6,7]]
解释:在上述示例中,存在两个连续正整数序列的和分别为 18,分别为 [3, 4, 5, 6] 和 [5, 6, 7]。

我的解

我的思路是遍历,将符合条件的放入到列表中

class Solution:
    def fileCombination(self, target: int) -> List[List[int]]:
        res = []
        for i in range(1,target):
            sum = 0
            tmp = []
            while sum < target:
                sum += i
                tmp.append(i)
                i += 1
                if sum == target:
                    res.append(tmp)
        return res
                
Screenshot 2023-10-26 at 23.17.59.png

滑动窗口

使用左边界i,有边界j,当他们的和为s < target时,j+1,s+=j,当s > target时,s-=i,
i +1,当s = target时,res.append()

class Solution:
    def fileCombination(self, target: int) -> List[List[int]]:
        i,j,s,res = 1,2,3,[]
        while i < j:
            if s == target:
                res.append(list(range(i,j+1)))
            if s >= target:
                s -= i
                i += 1
            else:
                j += 1
                s += j
        return res
Screenshot 2023-10-26 at 23.48.16.png

按规则计算统计结果

为了深入了解这些生物群体的生态特征,你们进行了大量的实地观察和数据采集。数组 arrayA 记录了各个生物群体数量数据,其中 arrayA[i] 表示第 i 个生物群体的数量。请返回一个数组 arrayB,该数组为基于数组 arrayA 中的数据计算得出的结果,其中 arrayB[i] 表示将第 i 个生物群体的数量从总体中排除后的其他数量的乘积。
示例1:

输入:arrayA = [2, 4, 6, 8, 10]
输出:[1920, 960, 640, 480, 384]

我的解

我的解:思想在于,我一次性就把所有元素的和求出来,然后每次去处以对应元素位置的值。这里就需要考虑特殊情况,当有1个0时,当有2个及以上0时

class Solution:
    def statisticalResult(self, arrayA: List[int]) -> List[int]:
        sum = 1
        hazero = 0
        tmp = 0
        for i in range(len(arrayA)):
            if arrayA[i] != 0:
                sum *= arrayA[i]
            else:
                hazero += 1
                tmp = i

        
        for i in range(len(arrayA)):
            if hazero > 1:
                arrayA[i] = 0
            elif hazero and i != tmp:
                arrayA[i] = 0
            elif hazero and  i == tmp:
                arrayA[tmp] = sum
            else:
                arrayA[i] = sum // arrayA[i]
        
            
        return arrayA

Screenshot 2023-10-27 at 00.11.44.png

上下三角形

根据表格的主对角线(全为 1 ),可将表格分为 上三角 和 下三角 两部分。分别迭代计算下三角和上三角两部分的乘积,即可 不使用除法 就获得结果。

image.png
class Solution:
    def statisticalResult(self, arrayA: List[int]) -> List[int]:
        arrayB,tmp = [1] * len(arrayA),1

        for i in range(1,len(arrayA)):
            arrayB[i] = arrayB[i-1] * arrayA[i-1]
        for i in range(len(arrayA)-2,-1,-1):
            tmp *= arrayA[i+1]
            arrayB[i] *= tmp
        return arrayB
Screenshot 2023-10-27 at 00.32.46.png

寻找文件副本

设备中存有 n 个文件,文件 id 记于数组 documents。若文件 id 相同,则定义为该文件存在副本。请返回任一存在副本的文件 id。

输入:documents = [2, 5, 3, 0, 5, 0]
输出:0 或 5

我的解

使用临时列表,将元素放入,如有重复就返回

class Solution:
    def findRepeatDocument(self, documents: List[int]) -> int:
        tmp = []
        for i in documents:
            if i not in tmp:
                tmp.append(i)
            else:
                return i
Screenshot 2023-10-30 at 17.38.48.png

接下来,我用字典作为容器,速度提升,同时测试了set,比list好

class Solution:
    def findRepeatDocument(self, documents: List[int]) -> int:
        tmp = {}

        for i in documents:
            if i in tmp:
                return i
            else:
                tmp[i] = 1
Screenshot 2023-10-30 at 17.50.23.png

相关文章

网友评论

      本文标题:图解算法数据结构-数组

      本文链接:https://www.haomeiwen.com/subject/rhcyidtx.html