相关疑难解决方法(0)

用于删除字符串数组中重复项的最佳算法

今天在学校,老师要求我们实施重复删除算法.这并不困难,每个人都想出了以下解决方案(伪代码):

for i from 1 to n - 1
    for j from i + 1 to n
        if v[i] == v[j] then remove(v, v[j])    // remove(from, what)
    next j
next i
Run Code Online (Sandbox Code Playgroud)

这个算法的计算复杂性是n(n-1)/2.(我们在高中,我们没有谈过大O,但似乎是O(n^2)).这个解决方案看起来很难看,当然也很慢,所以我试着更快地编写代码:

procedure binarySearch(vector, element, *position)
    // this procedure searches for element in vector, returning
    // true if found, false otherwise. *position will contain the
    // element's place (where it is or where it should be)
end procedure

----

// same type as v
vS = …
Run Code Online (Sandbox Code Playgroud)

string algorithm complexity-theory big-o duplicates

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

algorithm ×1

big-o ×1

complexity-theory ×1

duplicates ×1

string ×1