其中涉及到了一个distinct的实现,个人觉得自己写的算法效率不是很高,希望园区里的XD能够给我多提点改正的意见。
'
Power By Endision 2007/4/11
' welcome to click my cnblogs's blog http://endisoft.cnblogs.com
Private Function IsExists() Function IsExists(ByVal strString As String, ByVal strArray() As String) As Boolean
Dim i As Integer
For i = 0 To strArray.Length - 1
If strString = strArray(i) Then
Return True
End If
Next
Return False
End Function
Private Function DistinctArray() Function DistinctArray(ByVal strArray() As String) As Array
Dim i As Integer
Dim j As Integer
Dim strArrayCmp(strArray.Length - 1) As String
j = 0
For i = 0 To strArray.Length - 1
If i = 0 Then
strArrayCmp(0) = strArray(0)
Else
If Not IsExists(strArray(i).Trim, strArrayCmp) Then
j = j + 1
strArrayCmp(j) = strArray(i).Trim
End If
End If
Next
Return strArrayCmp
End Function
从复杂度的角度来说,为N的平方,所以觉得效率不是很好。
' welcome to click my cnblogs's blog http://endisoft.cnblogs.com
Private Function IsExists() Function IsExists(ByVal strString As String, ByVal strArray() As String) As Boolean
Dim i As Integer
For i = 0 To strArray.Length - 1
If strString = strArray(i) Then
Return True
End If
Next
Return False
End Function
Private Function DistinctArray() Function DistinctArray(ByVal strArray() As String) As Array
Dim i As Integer
Dim j As Integer
Dim strArrayCmp(strArray.Length - 1) As String
j = 0
For i = 0 To strArray.Length - 1
If i = 0 Then
strArrayCmp(0) = strArray(0)
Else
If Not IsExists(strArray(i).Trim, strArrayCmp) Then
j = j + 1
strArrayCmp(j) = strArray(i).Trim
End If
End If
Next
Return strArrayCmp
End Function