zoukankan      html  css  js  c++  java
  • Java for LeetCode 065 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.

    解题思路:

    本题边界条件颇多"1.", ".34","1.1e+.1"也是正确的。解题方法是先trim一下,然后按照e进行划分,然后分别对e前后进行判断JAVA实现如下:

        public boolean isNumber(String s) {
    		s = s.trim();
    		String[] splitArr = s.split("e");
    		if (s.length() == 0 || s.charAt(0) == 'e'
    				|| s.charAt(s.length() - 1) == 'e' || splitArr.length > 2)
    			return false;
    		for (int k = 0; k < splitArr.length; k++) {
    			String str = splitArr[k];
    			boolean isDecimal = false;
    			if (str.charAt(0) == '-' || str.charAt(0) == '+')
    				str = str.substring(1);
    			if (str.length() == 0)
    				return false;
    			for (int i = 0; i < str.length(); i++) {
    				if ('0' <= str.charAt(i) && str.charAt(i) <= '9')
    					continue;
    				else if (str.charAt(i) == '.' && !isDecimal) {
    					if (k == 0 && str.length() > 1)
    						isDecimal = true;
    					else
    						return false;
    				} else
    					return false;
    			}
    		}
    		return true;
    	}
    
  • 相关阅读:
    Quartz 基本概念及原理
    quartz-2.2.x 快速入门 (1)
    hive踩过的小坑
    spring profile 多环境配置管理
    win10窗口设置眼睛保护色
    优雅地在markdown插入图片
    Using Spring Boot without the parent POM
    isDebugEnabled作用
    Log 日志级别
    为什么要使用SLF4J而不是Log4J
  • 原文地址:https://www.cnblogs.com/tonyluis/p/4507515.html
Copyright © 2011-2022 走看看