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

[leetcode]Valid Number

时间:2014-10-28 21:52:13      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   java   for   sp   strong   on   

问题描述:

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.


考虑:93.3f, 23.43D , 2341234L等数字均为无效数字,数字中包含的字母只能是e


代码:

public class Valid_Number { //java
	 public boolean isNumber(String s) {
	        if(s == null || s.trim().isEmpty())
	        	return false;
			
	        s = s.trim().toLowerCase();
	        char ch = s.charAt(s.length()-1);
	        if(ch =='f' || ch =='l' || ch =='d')
	        	return false;
	        try{
	        	Double.valueOf(s);
	        	return true;
	        }catch(Exception e){
	        	return false;
	        }
	    }
}


[leetcode]Valid Number

标签:style   blog   io   ar   java   for   sp   strong   on   

原文地址:http://blog.csdn.net/chenlei0630/article/details/40544177

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