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

题目描述

https://leetcode-cn.com/problems/valid-number

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

例如:

"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

思路:

先判断字符串中有没有e,若有一个以上e则返回false

根据e分割字符串分别求底数是否有效,再判断指数是否有效

class Solution {
    public boolean isNumber(String s) {
        if(s==null||s.length()==0)
        return false;
        //去除两端的空格
        int start=0,end=s.length()-1;
        while(start<s.length()&&s.charAt(start)==' '){
            start++;
        }
        //若全为空格则返回false
        if(start==s.length()){
            return false;
        }
        while(end>=0&&s.charAt(end)==' '){
            end--;
        }
        //判断是不是只有一个e
        if(s.indexOf("e")!=s.lastIndexOf("e")){
            return false;
        }
        s=s.substring(start,end+1);
        //按e分割字符串,不是分割成一部分或者两部分肯定有误
        String[] str=s.split("e");
        if(str.length>2||str.length<1){
            return false;
        }
        String temp=str[0];
        if(temp.equals(".")||temp.equals("")||temp.equals("+")||temp.equals("-")
        ||temp.equals("-.")||temp.equals(".+")){
            return false;
        }
        //底数部分可以有数字;+;-;.
        int flag=1;
        if(temp.charAt(0)=='+'||temp.charAt(0)=='-'){
            temp=temp.substring(1); 
        }
        else if(temp.charAt(0)=='.'||(temp.charAt(0)>='0'&&temp.charAt(0)<='9')){
        }
        else{
            return false;
        }
        for(int i=0;i<temp.length();i++){
                if(flag==1&&temp.charAt(i)=='.'){
                    flag=0;
                    continue;
                }
                else if(temp.charAt(i)>='0'&&temp.charAt(i)<='9'){
                    continue;
                }
                else{
                    return false;
                }
        }
        //如果s只是e,那么返回false
        if(str.length==1&&!s.contains("e")){
            return true;
        }
        if(str.length==1&&s.contains("e")){
            return false;
        }
        //指数只能有数字;+;-
        temp=str[1];
        if(temp.equals(".")||temp.equals("")||temp.equals("+")||temp.equals("-")){
            return false;
        }
        if(temp.charAt(0)=='+'||temp.charAt(0)=='-'){
            temp=temp.substring(1); 
        }
        else if(temp.charAt(0)>='0'&&temp.charAt(0)<='9'){
        }
        else{
            return false;
        }
        for(int i=0;i<temp.length();i++){
                if(temp.charAt(i)>='0'&&temp.charAt(i)<='9'){
                    continue;
                }
                else{
                    return false;
                }
        }
        return true;
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值