js版Leetcode接受率最低题valid-number

问题

原题地址: https://leetcode.com/problems/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.

JS 一句话版 通过结果

/**
 * @param {string} s
 * @return {boolean}
 */
var isNumber = function (s) {
  return /^\s*[+-]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)(e[+-]?[0-9]+)?\s*$/.test(s);
};

js版Leetcode接受率最低题valid-number

https://sh.gg/posts/2015/leetcode-valid-number/

作者

Willin Wang

发布于

2015-05-15

更新于

2022-04-10

许可协议

评论