-
Notifications
You must be signed in to change notification settings - Fork 2
/
biao-shi-shu-zhi-de-zi-fu-chuan-lcof.rs
121 lines (112 loc) · 3.72 KB
/
biao-shi-shu-zhi-de-zi-fu-chuan-lcof.rs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
enum Status {
Start,
PreBlank, // 前空格
Sign, // 符号位
Integer, // 数字
PointWithInteger, // 前面带数字的小数点
PointWithoutInteger, // 前面不带数字的小数点
IntegerAfterPoint, // 小数点后面的数字
E, // E or e
IntegerAfterE, // E后面的数字
SignAfterE, // E 后面的符号
LastInteger, // 最后的数字
PostBlank, // 后空格
Invalid, // 无效的
}
impl Status {
fn new() -> Status {
Status::Start
}
fn transform(&mut self, x: u8) {
use Status::*;
match self {
Start => match x {
b' ' => *self = PreBlank,
b'+' | b'-' => *self = Sign,
b'0'..=b'9' => *self = Integer,
b'.' => *self = PointWithoutInteger,
_ => *self = Invalid,
},
PreBlank => match x {
b' ' => *self = PreBlank,
b'+' | b'-' => *self = Sign,
b'0'..=b'9' => *self = Integer,
b'.' => *self = PointWithoutInteger,
_ => *self = Invalid,
},
Sign => match x {
b'0'..=b'9' => *self = Integer,
b'.' => *self = PointWithoutInteger,
_ => *self = Invalid,
},
Integer => match x {
b' ' => *self = PostBlank,
b'0'..=b'9' => *self = Integer,
b'.' => *self = PointWithInteger,
b'E' | b'e' => *self = E,
_ => *self = Invalid,
},
PointWithInteger => match x {
b' ' => *self = PostBlank,
b'0'..=b'9' => *self = IntegerAfterPoint,
b'E' | b'e' => *self = E,
_ => *self = Invalid,
},
PointWithoutInteger => match x {
b'0'..=b'9' => *self = IntegerAfterPoint,
_ => *self = Invalid,
},
IntegerAfterPoint => match x {
b' ' => *self = PostBlank,
b'0'..=b'9' => *self = IntegerAfterPoint,
b'E' | b'e' => *self = E,
_ => *self = Invalid,
},
E => match x {
b'0'..=b'9' => *self = IntegerAfterE,
b'+' | b'-' => *self = SignAfterE,
_ => *self = Invalid,
},
IntegerAfterE => match x {
b' ' => *self = PostBlank,
b'0'..=b'9' => *self = IntegerAfterE,
b'+' | b'-' => *self = SignAfterE,
_ => *self = Invalid,
},
SignAfterE => match x {
b'0'..=b'9' => *self = LastInteger,
_ => *self = Invalid,
},
LastInteger => match x {
b' ' => *self = PostBlank,
b'0'..=b'9' => *self = LastInteger,
_ => *self = Invalid,
},
PostBlank => match x {
b' ' => *self = PostBlank,
_ => *self = Invalid,
},
Invalid => {}
}
}
fn is_valid(&self) -> bool {
use Status::*;
match self {
Integer | PointWithInteger | IntegerAfterPoint | LastInteger | PostBlank
| IntegerAfterE => true,
_ => false,
}
}
}
struct Solution;
impl Solution {
pub fn is_number(s: String) -> bool {
let mut status = Status::new();
for &i in s.as_bytes() {
status.transform(i);
}
status.is_valid()
}
}