private int Foo(int[] arr, int startIndex, int endIndex) { int totalMaxCount = 1; int currentMaxCount = 1; int t = arr[0]; for (int i = startIndex + 1; i < endIndex; i++) { if (arr[i] > t) { currentMaxCount++; if (currentMaxCount > totalMaxCount) { totalMaxCount = currentMaxCount; } } else if (arr[i] < t) { currentMaxCount = 1;
} t = arr[i]; } return totalMaxCount; }int a = Foo(arr, 0, arr.Length);
public void FetMaxLength() { int[] intArray = { 1, 2, 2, 4, 5, 10, 7, 8, 9, 50, 1, 2, 2, 5, 2, 7, 2, 2, 10, 11, 20 }; int intLength = 1;//当前长度 int j = 1;//以前最大长度 for (int i = 0; i < intArray.Length; i++) { if (i != 0) { //熟悉连续,长度+1 if (intArray[i] - intArray[i - 1] == 1) { intLength++; } //数字不连续,计算出前面连续数字的长度 else { //如果此次连续数字长度大于以前最大长度 if (intLength >= j) { //最大长度为 j = intLength; } //当前数字长恢复为1 intLength = 1; } } } int maxLength = j; }
{
int totalMaxCount = 1;
int currentMaxCount = 1;
int t = arr[0];
for (int i = startIndex + 1; i < endIndex; i++)
{
if (arr[i] > t)
{
currentMaxCount++;
if (currentMaxCount > totalMaxCount)
{
totalMaxCount = currentMaxCount;
}
}
else if (arr[i] < t)
{
currentMaxCount = 1;
}
t = arr[i];
}
return totalMaxCount;
}int a = Foo(arr, 0, arr.Length);
{
int[] intArray = { 1, 2, 2, 4, 5, 10, 7, 8, 9, 50, 1, 2, 2, 5, 2, 7, 2, 2, 10, 11, 20 };
int intLength = 1;//当前长度
int j = 1;//以前最大长度
for (int i = 0; i < intArray.Length; i++)
{
if (i != 0)
{
//熟悉连续,长度+1
if (intArray[i] - intArray[i - 1] == 1)
{
intLength++;
}
//数字不连续,计算出前面连续数字的长度
else
{
//如果此次连续数字长度大于以前最大长度
if (intLength >= j)
{
//最大长度为
j = intLength;
}
//当前数字长恢复为1
intLength = 1;
}
}
}
int maxLength = j;
}
比如2 3 4 3 2 3就是aabba
然后string.split(b)
求出分离后的最长的string长度即可
{
static void Main(string[] args)
{
int[] arr = new int[] { 5, 4, 3, 2, 3 }; Function(arr); Console.ReadKey();
} public static void Function(int[] arr)
{
int startIndex = 0, tmpStartIndex = 0;
int maxLength = 0, tmpMaxLength = 0;
bool isGrowing = false; for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] - arr[i + 1] < 0)
{
// now growing
if (isGrowing == false) // first enter growing queue
{
isGrowing = true;
tmpStartIndex = i;
tmpMaxLength = 2;
}
else // already in a growing queue
{
tmpMaxLength++;
}
}
else
{
// growing queue end
isGrowing = false;
} // save the current growing queue info if it is longer than before
if (tmpMaxLength > maxLength)
{
maxLength = tmpMaxLength;
startIndex = tmpStartIndex;
}
} // 输出最大增长子数组的起始位置和长度
Console.WriteLine("StartIndex: {0}, Length: {1}", startIndex, maxLength);
}
}