81.第1组百度面试题
1.一个int数组,里面数据无任何限制,要求求出所有这样的数a[i],
其左边的数都小于等于它,右边的数都大于等于它。
能否只用一个额外数组和少量其它空间实现。
VB.NET codes as below:
Module Module1
Sub Main()
Dim array = {1, 2, 1, 2, 3, 2, 3, 4, 5, 6, 7, 3, 2, 3, 9}
PrintValues(array)
Console.ReadKey()
End Sub
Public Sub PrintValues(ByVal array As Integer())
Dim arrayLength = array.Length
Dim container(0 To arrayLength - 1) As Boolean
Dim leftMax As Integer = array(0)
For i = 0 To arrayLength - 1
If array(i) >= leftMax Then
container(i) = True
leftMax = array(i)
End If
Next
Dim rightMin As Integer = array(arrayLength - 1)
For i = 0 To arrayLength - 1
If array(arrayLength - 1 - i) <= rightMin Then
rightMin = array(arrayLength - 1 - i)
If container(arrayLength - 1 - i) = True Then
Console.WriteLine(array(arrayLength - 1 - i).ToString())
End If
End If
Next
End Sub
End Module