[LeetCode][JavaScript]Valid Number

https://leetcode.com/problems/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 be ambiguous. You should gather all requirements up front before implementing one.



正则技术哪家强

 1 /**
 2  * @param {string} s
 3  * @return {boolean}
 4  */
 5 var isNumber = function(s) {
 6     s = s.trim();
 7     if(s !== "" && /^[-+]?(\d+\.?|\.\d+)\d*([e|E][-+]?\d+)?$/.test(s)){
 8         return true;
 9     }
10     return false;
11 };

一开始没想清楚,每多一种情况就加一个else,写的乱七八糟,后来发现都可以合并。

 1 /*
 2     s = s.trim();
 3     if(s === ""){
 4         return false;
 5     }else if(/^[-+]?[e|E]/.test(s)){
 6         return false;
 7     }else if(/^[-+]?0$/.test(s)){
 8         return true;
 9     }else if(/^[-+]?[0-9]*$/.test(s)){
10         return true;
11     }else if(/^[-+]?[0-9]+\.[0-9]*$/.test(s)){
12         return true;
13     }else if(/^[-+]?[0-9]*\.[0-9]+$/.test(s)){
14         return true;
15     }else if(/^[-+]?[0-9]*[e|E][-+]?[0-9]+$/.test(s)){
16         return true;
17     }else if(/^[-+]?([0-9]+\.[0-9]*)[e|E][-+]?[0-9]+$/.test(s)){
18         return true;
19     }else if(/^[-+]?([0-9]*\.[0-9]+)[e|E][-+]?[0-9]+$/.test(s)){
20         return true;
21     }
22     return false;
23 */
时间: 2024-10-09 22:38:31

[LeetCode][JavaScript]Valid Number的相关文章

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

LeetCode:Valid Number - 判断字符串中内容是否为数字

1.题目名称 Valid Number(判断字符串中内容是否为数字) 2.题目地址 https://leetcode.com/problems/valid-number/ 3.题目内容 英文:Validate if a given string is numeric. 中文:给出一个字符串,检查这个字符串中内容是否是一个数字 例如:"0"." 0.1"."2e10"是数字,"abc"."1 a"不是数字 4

【leetcode】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 be am

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][JavaScript]Additive Number

Additive Number Additive number is a positive integer whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum o

[LeetCode][JavaScript]Happy Number

Happy Number Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the proc

[LeetCode][JavaScript]Single Number II

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? https://leetcode.com/

[LeetCode][JavaScript]Single Number

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? https://leetcode.com/problems/

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sud