首页 > 试题广场 >

是否是数字

[编程题]是否是数字
  • 热度指数:14250 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
判断给出的字符串是否是数字
一些例子:
"0"=>true
" 0.1 "=>true
"abc"=>false
"1 b"=>false
"3e10"=>true


为啥允许小数点开头和小数点结尾呢
发表于 2022-11-28 19:22:09 回复(0)
public class Solution {
    public boolean isNumber(String s) {
        if(s.matches(".*[a-zA-Z]$"))
            return false;
        try{
            double s1 = Double.parseDouble(s);
            return true;
        }catch(Exception e){
            return false;
        }
    }
}

发表于 2017-09-04 14:35:17 回复(0)
// 亲测本机上没问题,提交却显示错误,难道是因为我用的是Java8?
public class Solution {
    public boolean isNumber(String s) {
        if (s.charAt(s.length()-1) > '9' || s.charAt(s.length()-1) < '0') return false;
        try {
            Double.valueOf(s);
        } catch (Exception e) {
            return false;
        }

        return true;
    }
}

编辑于 2017-07-02 23:38:52 回复(0)
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 功能说明:LeetCode 65 - Valid Number
*/
public class Solution {
/**
* 判断指定字符串是否为数字
* @param s 字符串
* @return true:是数字 false:不是数字
*/
public boolean isNumber(String s) {
s = s.trim();
if (s.isEmpty()) {
return false;
}

//正则匹配
Pattern p = Pattern.compile(
"^([+-])?((\\d+)(\\.)?(\\d+)?|(\\d+)?(\\.)?(\\d+))(e([+-])?(\\d+))?$");
Matcher m = p.matcher(s);
if (m.find()) {
return true;
}
return false;
}
}

正则匹配

编辑于 2017-03-14 21:43:24 回复(1)

All we need is to have a couple of flags so we can process the string in linear time:

public boolean isNumber(String s) {
    s = s.trim(); boolean pointSeen = false; boolean eSeen = false; boolean numberSeen = false; boolean numberAfterE = true; for(int i=0; i<s.length(); i++) { if('0' <= s.charAt(i) && s.charAt(i) <= '9') {
            numberSeen = true;
            numberAfterE = true;
        } else if(s.charAt(i) == '.') { if(eSeen || pointSeen) { return false;
            }
            pointSeen = true;
        } else if(s.charAt(i) == 'e') { if(eSeen || !numberSeen) { return false;
            }
            numberAfterE = false;
            eSeen = true;
        } else if(s.charAt(i) == '-' || s.charAt(i) == '+') { if(i != 0 && s.charAt(i-1) != 'e') { return false;
            }
        } else { return false;
        }
    } return numberSeen && numberAfterE;
}

We start with trimming.

  • If we see [0-9] we reset the number flags.
  • We can only see . if we didn't see e or ..
  • We can only see e if we didn't see e but we did see a number. We reset numberAfterE flag.
  • We can only see + and - in the beginning and after an e
  • any other character break the validation.

At the and it is only valid if there was at least 1 number and if we did see an e then a number after it as well.

So basically the number should match this regular expression:

[-+]?(([0-9]+(.[0-9]*)?)|.[0-9]+)(e[-+]?[0-9]+)?

发表于 2017-03-12 12:14:05 回复(0)
public class Solution {
    public boolean isNumber(String s) {
		try {
			char c = s.charAt(s.length() - 1);
			if(c == 'f' || c == 'F' || c == 'd' || c == 'D') return false;
			Double d = Double.valueOf(s);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}
}

发表于 2016-11-05 16:46:46 回复(7)