Leetcode-swordは、Offer20の値を表す文字列を指します

タイトル:https : //leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/

回答:

1.レイジーメソッド

public boolean isNumber(String s){

        if(s.endsWith( "f")|| s.endsWith( "F")|| s.endsWith( "d")|| s.endsWith( "D"))はfalseを返します。

        {を試す

            Double.valueOf(s);

            trueを返します。

        }キャッチ(例外例){

            falseを返します。

        }

    }

2.完全一致

 public boolean isNumber(String s){

       if(s == null || s.length()== 0)はfalseを返します。

        ブールbeforeNum = false、beforeDot = false、beforeE = false;

        char [] chars = s.trim()。toCharArray();

        for(int i = 0; i <chars.length; i ++){

            if(chars [i]> = '0' && chars [i] <= '9')beforeNum = true;

            else if(chars [i] == '。'){

                if(beforeDot || beforeE)はfalseを返します。

                beforeDot = true;

            } else if(chars [i] == 'E' || chars [i] == 'e'){

                if(beforeE ||!beforeNum)はfalseを返します。

                beforeE = true;

                beforeNum = false;

            } else if(chars [i] == '-' || chars [i] == '+'){

                if(i!= 0 && chars [i-1]!= 'e' && chars [i-1]!= 'E')return false;

            }そうしないと{

                falseを返します。

            }

        }

        beforeNumを返します。

    }

おすすめ

転載: blog.csdn.net/wuqiqi1992/article/details/108366712