-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path65.valid-number.js
99 lines (94 loc) · 1.74 KB
/
65.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* @lc app=leetcode id=65 lang=javascript
*
* [65] Valid Number
*/
// @lc code=start
/**
* @param {string} s
* @return {boolean}
*/
/*
these are the valid states and inputs to the finite state machine
states = {
START: 0,
SIGN: 1,
POINT: 2,
INTEGER: 3,
FRACTION: 4,
EXP: 5,
EXP_SIGN: 6,
EXP_INT: 7,
};
inputs = {
DIGIT: 1,
SIGN: 2,
POINT: 3,
E: 4,
};*/
//these are the final states of the machine
let final_states = new Set(["INTEGER", "EXP_INT", "FRACTION"]);
//these are the valid machine moves on some input
// machine[currentState][input] spits out the next state of the machine
let machine = {
START: {
DIGIT: "INTEGER",
POINT: "POINT",
SIGN: "SIGN",
},
SIGN: {
POINT: "POINT",
DIGIT: "INTEGER",
},
POINT: {
DIGIT: "FRACTION",
},
INTEGER: {
DIGIT: "INTEGER",
POINT: "FRACTION",
E: ["EXP"],
},
FRACTION: {
DIGIT: "FRACTION",
E: "EXP",
},
EXP: {
DIGIT: "EXP_INT",
SIGN: "EXP_SIGN",
},
EXP_SIGN: {
DIGIT: "EXP_INT",
},
EXP_INT: {
DIGIT: "EXP_INT",
},
};
function convertInput(s) {
if (/^[\+\-]$/.test(s)) {
return "SIGN";
} else if (/^[.]$/.test(s)) {
return "POINT";
} else if (/^[0-9]+$/.test(s)) {
return "DIGIT";
} else if (/^[E]$/i.test(s)) {
return "E";
} else {
return "BOGUS_VALUE";
}
}
var isNumber = function (s) {
let currentState = "START";
let nextState;
let input;
for (let i = 0; i < s.length; i++) {
input = convertInput(s[i]);
nextState = machine[currentState][input];
if (!nextState) return false;
currentState = nextState;
}
return final_states.has(currentState);
};
// @lc code=end
// @after-stub-for-debug-begin
module.exports = isNumber;
// @after-stub-for-debug-end