<p>我同意sundar nataraj的观点,他说这必须发布到代码评审论坛。在</p>
<p>尽管我看了你的代码。虽然我能够理解您的方法,但我无法理解使用<code>Counter</code>的必要性。在</p>
<ol>
<li><p><code>best_index_hash[start_index] = [(0,start_index)]</code>-这里<code>best_index_hash</code>属于<code>Counter</code>类型。你为什么要给它分配一个列表?</p></li>
<li><p><code>for key_1, value_1 in best_index_hash.most_common(1)</code>-您试图得到<code>largest</code>子序列,为此,您使用<code>most_common</code>作为答案。这不是直观的语义。</p></li>
</ol>
<p>我很想发布一个解决方案,但我会等你编辑代码片段并加以改进。在</p>
<hr/>
<p><strong>附录</strong></p>
<p>为了好玩,我尝试了这个拼图,我在下面展示了我的努力。我不保证正确性/完整性。在</p>
<pre><code>from collections import defaultdict
def max_sub_array_sum(a, s):
if a:
span = defaultdict(lambda : (0,0))
current_total = 0
for i in xrange(len(a)):
current_total = a[i]
for j in xrange (i + 1, len(a)):
current_total += a[j]
x,y = span[current_total]
if j - i > y - x:
span[current_total] = i,j
if s in span:
i, j = span[s]
print "sum=%d,span_length=%d,indices=(%d,%d),sequence=%s" %\
(s, j-i + 1, i, j, str(a[i:j + 1]))
return
print "Could not find a subsequence of sum %d in sequence %s" % \
(s, str(a))
max_sub_array_sum(range(-6, -1), 0)
max_sub_array_sum(None, 0)
max_sub_array_sum([], 0)
max_sub_array_sum(range(6), 15)
max_sub_array_sum(range(6), 14)
max_sub_array_sum(range(6), 13)
max_sub_array_sum(range(6), 0)
</code></pre>