What does “in constant time” imply?

后端 未结 8 1409
梦谈多话
梦谈多话 2021-01-30 17:21

I work as a programmer, but have no computer science background, so recently I\'ve been following along with the excellent MIT OpenCourseWare intro to Computer Science and Progr

8条回答
  •  灰色年华
    2021-01-30 17:47

    Constant time here means not dependent on number of inputs (not the input itself), and if you aren't allowed for or goto, the only way to make it depend on the number of inputs is by conditionals and recursion. Although you could argue that recursion isn't necessary with some debatable solutions. Eg. (in C)

    if(ReadInput()) DoSomeThing();
    if(ReadInput()) DoSomeThing();
    if(ReadInput()) DoSomeThing();
    if(ReadInput()) DoSomeThing();
    

提交回复
热议问题