Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表中的对总和:记忆化、设计优化_Python_Design Patterns_Memoization - Fatal编程技术网

Python 列表中的对总和:记忆化、设计优化

Python 列表中的对总和:记忆化、设计优化,python,design-patterns,memoization,Python,Design Patterns,Memoization,从一个整数列表和一个和值中,我必须返回前两个值,它们的出现顺序与和相加 我认为扫描列表的最佳方式是: 等等。到目前为止我说的对吗 然后我用记忆法把出现两次以上的数字切掉 我编写的代码是功能性的,但在更高级的测试中超时。这是: def sum_pairs(ints, s): d={} n2_index = 0 d[ints[0]] = 1 while True: n2_index += 1 if ints[n2_index] not in d.keys():

从一个整数列表和一个和值中,我必须返回前两个值,它们的出现顺序与和相加

我认为扫描列表的最佳方式是:

  • 等等。到目前为止我说的对吗

    然后我用记忆法把出现两次以上的数字切掉

    我编写的代码是功能性的,但在更高级的测试中超时。这是:

    def sum_pairs(ints, s):
    d={}
    n2_index = 0
    d[ints[0]] = 1
    while True:
        n2_index += 1
        if ints[n2_index] not in d.keys():
            d[ints[n2_index]] = 0
            
        if d[ints[n2_index]] == 2:
            if n2_index == len(ints)-1:
                return None
            continue
        for n1_index in range (0, n2_index):
    
            if ints[n1_index] + ints[n2_index] == s:
                return [ints[n1_index], ints[n2_index]]
        
        d[ints[n2_index]] += 1
        
        if n2_index == len(ints)-1:
            return None
    
    如果您能帮助我理解我的错误,以及如何处理此类任务,我将不胜感激。
    干杯

    方法是记住你以前见过的所有数字。这通常是在一个集合中完成的,一个集合给你O(1)(常数)的查找时间,所以你可以很快确定你是否已经看到了一个特定的数字

    在列表中,您可以查看集合中是否看到了
    总和-当前值
    。如果是,则可以输出这两个值,如果不是,则将当前_值添加到集合中并继续

    def sum(ints, s):
        seen = set()
        for current_value in ints:
            if s - current_value in seen:
                 return s-current_value, current_value
            else:
                 seen.add(current_value)
        return None
    
    这可能会更好
             index 1, index 2
    
    index 0,                   index 3
    
            index 1,           index 3
    
                      index 2, index 3
    
    def sum_pairs(ints, s):
    d={}
    n2_index = 0
    d[ints[0]] = 1
    while True:
        n2_index += 1
        if ints[n2_index] not in d.keys():
            d[ints[n2_index]] = 0
            
        if d[ints[n2_index]] == 2:
            if n2_index == len(ints)-1:
                return None
            continue
        for n1_index in range (0, n2_index):
    
            if ints[n1_index] + ints[n2_index] == s:
                return [ints[n1_index], ints[n2_index]]
        
        d[ints[n2_index]] += 1
        
        if n2_index == len(ints)-1:
            return None
    
    def sum(ints, s):
        seen = set()
        for current_value in ints:
            if s - current_value in seen:
                 return s-current_value, current_value
            else:
                 seen.add(current_value)
        return None