【面試題】給定一個僅包含字母和數字(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");
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章