面试题20. 表示数值的字符串

验证给定的字符串是否可以解释为十进制数字。

例如:

“0” => true
" 0.1 " => true
“abc” => false
“1 a” => false
“2e10” => true
" -90e3 " => true
" 1e" => false
“e3” => false
" 6e-1" => true
" 99e2.5 " => false
“53.5e93” => true
" --6 " => false
“-+3” => false
“95a54e53” => false

说明: 我们有意将问题陈述地比较模糊。在实现代码之前,你应当事先思考所有可能的情况。这里给出一份可能存在于有效十进制数字中的字符列表:

数字 0-9
指数 - “e”
正/负号 - “+”/"-"
小数点 - “.”
当然,在输入中,这些字符的上下文也很重要。

import java.util.*;
class Solution {
    public boolean isNumber(String string){
        if (string == null) return false;
        String str = string.trim();

        if (str.length() < 1) return false;
        if (str.length() == 1){
            if (!Character.isDigit(str.charAt(0)))
                return false;
        }
        int indexe = str.indexOf('e') >= 0 ? str.indexOf('e') : -1;
        int indexE = str.indexOf('E')>= 0 ? str.indexOf('E') : -1;
        int index = Math.max(indexE, indexe);
        if (index >= 0){                  //含有e或E
            if (index == 0 || index == str.length()-1) {
                return false;
            }else{
                String s1 = str.substring(0,index);
                String s2 = str.substring(index+1);
                return isFloat(s1) && isInt(s2);
            }
        }else if (str.indexOf('.') >= 0){ //不含e,含有小数点
            return isFloat(str);
        }else{                            //不含e,也不含小数点
            return isInt(str);
        }
    }
    
    public boolean isInt(String s){  //整数
        if (s.length() == 1 && !Character.isDigit(s.charAt(0))) return false;
        if (s.charAt(0) == '+' || s.charAt(0) == '-'){
            for (int i = 1; i < s.length(); i++) {
                if (!Character.isDigit(s.charAt(i))){
                    return false;
                }
            }
            return true;
        }else{
            for (int i = 0; i < s.length(); i++) {
                if (!Character.isDigit(s.charAt(i))){
                    return false;
                }
            }
            return true;
        }
    }
    public boolean isFloat(String string){  //浮点数
        int numDot = 0;
        if (string.length() == 1 && !Character.isDigit(string.charAt(0))) return false;
        if (string.charAt(0) == '+' || string.charAt(0) == '-'){
            for (int i = 1; i < string.length(); i++) {
                char ch = string.charAt(i);
                if (ch == '.'){
                    numDot++;
                }
                if (numDot == 1 && string.length() < 3) return false;  //"-."
                if (numDot > 1) return false;
                if (ch == '.' && numDot == 1) continue;
                if (!Character.isDigit(ch)) return false;
            }
            return true;
        }else{
            for (int i = 0; i < string.length(); i++) {
                char ch = string.charAt(i);
                if (ch == '.'){
                    numDot++;
                }
                if (numDot > 1) return false;
                if (ch == '.' && numDot == 1) continue;
                if (!Character.isDigit(ch)) return false;
            }
            return true;
        }
    }
}

1481 / 1481 个通过测试用例
状态:通过
执行用时:3 ms
内存消耗:38.4 MB

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值