65. 有效数字

版权声明:只要梦想一天,只要梦想存在一天,就可以改变自己的处境。 https://blog.csdn.net/dongyanwen6036/article/details/86491653

65. 有效数字

 class Solution {
 public:
	 //考察全面性
	 bool isNumber(string s) {
		 int len = s.length();
		 if (len == 0)return false;
		 
		 int i = 0;
		 while (i < len&&s[i] == ' ')i++;//skip space

		 bool space = false;
		 bool exp = false;
		 bool dot = false;
		 bool num = false;
		 bool neg = false;

		 for (; i < len; i++)
		 {
			 char c = s[i];
			 if (c == ' ')
				 space = true;
			 else if (space == true)//middle exit space 
				 return false;
			 else if ((c == 'e' || c == 'E') && exp == false && num == true)
			 {
				 exp = true;
				 num = false;//以e结尾暂时是false
				 dot = true;//e的后面不可能存在dot
				 neg = false;//e的后面可能是负号,
			 }
			 else if (c == '.'&& dot == false)//.-4
			 {
				 dot = true;
				 neg = true;//后面不可能-
				 //exp = true;//后面可能"46.e3"
			 }
			 else if (c <= '9' && c >= '0')
			 {
				 num = true;
			 }
			 else if ((c == '+' || c == '-') && neg == false && num == false)
			 {
				 neg = true;//开头,E
			 }
			 else
				 return false;
		 }
		 return num;
	 }
 };

猜你喜欢

转载自blog.csdn.net/dongyanwen6036/article/details/86491653