【面试题】给定一个仅包含字母和数字(0-9)的字符串,要求找到最长递增的数字串

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

bool isDigit(char c)
{
	if(c >= '0' && c<='9')
	{
		return true;
	}
	else return false;
}

void findMaxSeq(char* s)
{
	char *p;
	char *pt = NULL;
	int len = 0;
	int Max = 0;
	p = s;
	while(*p != '\0')
	{
		if(!isDigit(*p))
		{
			len = 0;
		}
		else if(isDigit(*(p-1)))
		{
			if(*p >= *(p-1))
			{
				len++;
				if(len > Max)
				{
					Max = len;
					pt = p-len+1;
				}
			}
			else
			{
				len = 1;
			}
		}
		else
		{
			len = 1;
		}
		p++;
	}
	if(pt == NULL || Max == 0)
	{
		cout<<"no digit or no MaxSeq"<<endl;
		return;
	}
	for(int i = 0 ; i < Max ; i++)
	{
		cout<<pt[i];
	}

}

void main()
{
	//char* s = "abcd12dfg298679rg13";
	char* s = "123adb6545";
	findMaxSeq(s);
	
	system("pause");
}


你可能感兴趣的:(【面试题】给定一个仅包含字母和数字(0-9)的字符串,要求找到最长递增的数字串)