LeetCode Valid Number

本文介绍了一种判断字符串是否为有效数字的方法,并详细解析了其逻辑结构与关键步骤。通过细致考虑各种边界情况,该方法能够准确判断包括整数、浮点数及科学计数法在内的多种数字形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

终于AC啦,刚开始把题想简单了,这题最关键的是要考虑周全所有的可能,最重要的程序结构,这题把程序的逻辑表现的淋漓尽致。是不是提交比例最低的了,有意思,好题。

bool isNumber(const char *s) {
	if(s==NULL||*s==0)
		return false;
	bool flag = false;// 表示.钱是不是有数字
	while(*s==' ')
		s++;
	if(*s=='-'||*s=='+')
		s++;
	if((*s>'9'||*s<'0')&&*s!='.')
		return false;
	if (*s>='0'&&*s<='9')
	{
		flag = true;
		while(*s>='0'&&*s<='9')
			s++;
		if(*s==0)
			return true;
	}
	if(*s=='.')
	{
		s++;
		if (*s==' ')
		{
			while(*s==' ')
				s++;
			if(*s==0)
			{
				if (flag)
					return true;//小数点之后可以没有数字,但若是有的话,必须是数字	
				else
					return false;
			}
			else
				return false;
		}
		if(*s==0)
		{
			if (flag)
				return true;//小数点之后可以没有数字,但若是有的话,必须是数字	
			else
				return false;
		}
	}
	if (*s>='0'&&*s<='9')
	{
		//if (*s>'9'||*s<'0')
		//	return false;
		flag = true;
		while(*s>='0'&&*s<='9')
			s++;
		if(*s==0)
			return true;
	}
	if (*s=='e'||*s=='E')
	{
		if (!flag)
			return false;
		s++;
		if(*s=='-'||*s=='+')
			s++;
		if (*s>'9'||*s<'0')
			return false;
		while(*s>='0'&&*s<='9')
			s++;
		if(*s==0)
			return true;
	}
	if (*s==' ')
	{
		while(*s==' ')
			s++;
		if(*s==0)
			return true;
	}
	return false;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值