简体   繁体   English

python索引如何影响o-notation的运行时?

[英]how does python indexing affect runtime of o-notation?

I am new to O-notation and am trying to find the worst-case runtime for some of my codes.我是 O 符号的新手,正在尝试为我的一些代码找到最坏情况的运行时间。 The only issue is that I'm confused on how O-notation runs with indexing and appending so I thought I'd ask for help with the following sample codes:唯一的问题是我对 O-notation 如何与索引和附加一起运行感到困惑,所以我想我会寻求以下示例代码的帮助:

def sums_1(L):
  n = len(L)
  tot = 0
  M = []
  for i in L[:n//2]:
    M.append(i)
  for i in L[n//2:]:
    M.extend(L)
  return sum(M)

def sums_2(s):
  def help_e(s, pos):
    if pos >= len(s):
      return ''
    return help_e(s, pos+1) + s[pos]
  return help_e(s, 0)

I think both codes would run o(n) times but I wanted some clarification on indexing and how that may affect the runtime, thanks!我认为这两个代码都会运行 o(n) 次,但我想澄清索引以及这可能如何影响运行时,谢谢!

在这里你有几乎所有 python 数据结构操作的 big-o 符号的 wiki 文件: https : //wiki.python.org/moin/TimeComplexity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM