最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript内置split函数的Big O

SEO心得admin74浏览0评论
本文介绍了javascript内置split函数的Big O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

示例:

var string = "abcde"; var array = string.split(""); // array = ["a", "b", "c", "d", "e"]

此拆分功能的摊销运行时间是多少?另外,如何在javascript中查看此类内置函数的源代码?

What is the amortized running time of this split function? Also, how do I view source code of such built-in functions in javascript?

推荐答案

使用空的定界符参数,split本质上等效于:

With an empty delimiter argument, split is essentially equivalent to:

var len = string.length; var result = Array(len) for (i = 0; i < len; i++) { result[i] = string[i]; }

这是O(len).

使用定界符,它将变为O(string.length * delimiter.length),因为在循环的每个步骤中,它都必须测试delimiter是否匹配.

With a delimiter, it becomes O(string.length * delimiter.length), because at each step in the loop it has to test whether there's a match for delimiter.

发布评论

评论列表(0)

  1. 暂无评论