【LeetCode】- 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.

[ 解法: ]

public class Solution {

    public static boolean isNumber(String s) {
        try {
        	s = s.toLowerCase();
            Double.parseDouble(s);
        }
        catch (Exception e) {
            return false;
        }
        return s.charAt(s.length() -1) != 'f' && s.charAt(s.length() - 1) != 'd';
    }
}

注意:s不能以‘f‘或‘d‘结尾, 因为如果以它们结尾时,使用Double转换不会报错,

但是类似:10.1f, 20.1d 按照题意并不能算作有效数值,所以我们不能放过这些“漏网之鱼”。

[ 拓展: ]

1.float:单精度浮点数

声明为float类型的浮点数时,要在结尾加F或f。

float f1 = 11.11F;   // 正的浮点数

float f2 = -17.15f;  // 负的浮点数

2.double:双精度浮点数

声明为double类型的浮点数时,可在结尾加D或d。当然也可不加,因为浮点类型默认的类型是double。

double d1 = 11.11223D;  // 正的浮点数

double d2 = 11.11333d;  // 正的浮点数

double d3 = -17.15555;  // 负的浮点数

** 这里建议在double数据类型的数后面加上D或d,以便能够和单精度浮点数区分。

时间: 2024-10-15 22:05:58

【LeetCode】- Valid Number(有效数字)的相关文章

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

LeetCode: Valid Number [066]

[题目] 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 ambig

leetCode 65.Valid Number (有效数字)

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

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

LeetCode——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.

[leetcode]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 ambi

LeetCode—*Valid Number

Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true 主要就是判断一个字符串是不是一个数字,这个题目不是很难,主要是要把所有的情况理清楚 首先有一些情况是允许的 1. 前后空格 2

[LeetCode] 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

LeetCode --- 65. Valid Number

题目链接: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 statemen

Valid Number leetcode java

题目: 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 ambigu