码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode_Valid Number

时间:2015-05-16 10:34:17      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:valid number

描述:

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

思路:

这道题目还是挺复杂的,复杂的原因是题目并没有给出有效数字的定义,比如说   .1  ,2.1e10.2 ,2e3.2 和2.1e5 有效,但10.1.1非法而这些数字的判断又是很麻烦的  

代码:

public boolean isNumber(String s) {
		s = s.trim();
		if (s.startsWith("+"))
			s = s.substring(1);
		else if (s.startsWith("-"))
			s = s.substring(1);
		boolean isInterger = false;
		boolean isFloater = false;
		boolean isSciencer = false;
		if (isInt(s))
			isInterger = true;
		int indexFloat = -1;
		int indexScience1 = -1;
		int indexScience2 = -1;
		indexFloat = s.indexOf('.');
		indexScience1 = s.indexOf('e');
		indexScience2 = s.indexOf('E');
		String nums[] = null;
		if (indexFloat != -1) {
			int index1=s.indexOf('.');
			int index2=s.lastIndexOf('.');
			if(index1!=index2)
				isFloater=false;
			else 
				isFloater = isFloat(s);
		}
		if (indexScience1 + indexScience2 != -2) {
			if (indexScience1 != -1)
			{
				int index1=s.indexOf('e');
				int index2=s.lastIndexOf('e');
				if(index1!=index2)
					isSciencer=false;
				else
				{
					nums = s.split("e");
					isSciencer = isScientific(nums);
				}
			}
				
			if (indexScience2 != -1)
			{
				int index1=s.indexOf('E');
				int index2=s.lastIndexOf('E');
				if(index1!=index2)
					isSciencer=false;
				else
				{
					nums = s.split("E");
					isSciencer = isScientific(nums);
				}
					
			}
				
			
		}
		if (isFloater || isInterger || isSciencer)
			return true;
		return false;
	}

	public boolean isScientific(String num[]) // 是否为有效的科学计数
	{
		if(num.length<2)
			return false;
		if (num[1].startsWith("+") || num[1].startsWith("-"))
			num[1] = num[1].substring(1);
		boolean flag1 = num[0].contains(".");
		if (flag1)
			flag1 = isFloat(num[0]);
		else
			flag1 = isInt(num[0]);
		boolean flag2 = isInt(num[1]);
		if (flag1 && flag2)
			return true;
		return false;
	}

	public boolean isFloatic(String str[]) //是否为有效的浮点数
	{
		if (isInt(str[0]) && isInt(str[1]))
			return true;
		return false;
	}

	public boolean isFloat(String s) //是否为浮点数
	{
		boolean isTrue = false;
		String nums[] = null;
		if (s.startsWith("."))
			isTrue = isInt(s.substring(1));
		else if(s.endsWith("."))
			isTrue = isInt(s.substring(0,s.length()-1));
		else {
			nums = s.split("\\.");
			if (nums.length != 2)
				return false;
			isTrue = isInt(nums[0]) && isInt(nums[1]);
		}
		return isTrue;
	}

	public boolean isInt(String str) //是否为整数
	{
		if (str == null || str.length() == 0)
			return false;
		boolean isLeg = true;
		char ch;
		for (int i = 0; i < str.length(); i++) {
			ch = str.charAt(i);
			if (ch > '9' || ch < '0') {
				isLeg = false;
				break;
			}
		}
		return isLeg;
	}


leetcode_Valid Number

标签:valid number

原文地址:http://blog.csdn.net/mnmlist/article/details/45765857

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!