forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid Number.js
66 lines (55 loc) · 1.77 KB
/
Valid Number.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @param {string} s
* @return {boolean}
*/
var isNumber = function(s) {
const n = s.length;
const CHAR_CODE_UPPER_E = 'E'.charCodeAt(0);
const CHAR_CODE_LOWER_E = 'e'.charCodeAt(0);
const CHAR_CODE_UPPER_A = 'A'.charCodeAt(0);
const CHAR_CODE_UPPER_Z = 'Z'.charCodeAt(0);
const CHAR_CODE_LOWER_A = 'a'.charCodeAt(0);
const CHAR_CODE_LOWER_Z = 'z'.charCodeAt(0);
let sign = '';
let decimal = '';
let exponential = '';
let exponentialSign = '';
let num = '';
for(let i = 0; i < n; i += 1) {
const char = s[i];
const charCode = s.charCodeAt(i);
if(char === '+' || char === '-') {
if(i === n - 1) return false;
if(i === 0) {
sign = char;
continue;
}
if(exponentialSign.length > 0) return false;
if(!(s[i - 1] === 'e' || s[i - 1] === 'E')) return false;
exponentialSign = char;
continue;
}
if(char === '.') {
if(decimal.length > 0) return false;
if(exponential.length > 0) return false;
if(num.length === 0 && i === n - 1) return false;
decimal = char;
continue;
}
if(charCode === CHAR_CODE_UPPER_E || charCode === CHAR_CODE_LOWER_E) {
if(exponential.length > 0) return false;
if(i === n - 1) return false;
if(num.length === 0) return false;
exponential = char;
continue;
}
if(charCode >= CHAR_CODE_UPPER_A && charCode <= CHAR_CODE_UPPER_Z) {
return false;
}
if(charCode >= CHAR_CODE_LOWER_A && charCode <= CHAR_CODE_LOWER_Z) {
return false;
}
num += char;
}
return true;
};