File tree 1 file changed +66
-0
lines changed
1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isNumber(String s) {
3
+ boolean digitseen =false, eseen = false, dotseen=false;
4
+ int countPlusMinus = 0;
5
+
6
+ for(int i=0;i<s.length();i++){
7
+ char ch = s.charAt(i);
8
+
9
+ //digit
10
+ if(Character.isDigit(ch)){
11
+ digitseen = true;
12
+ }
13
+ //minus/plus
14
+ else if(ch =='+' || ch == '-'){
15
+ if(countPlusMinus == 2){
16
+ return false;
17
+ }
18
+
19
+ if(i>0 && (s.charAt(i-1) != 'e' && s.charAt(i-1) != 'E')){
20
+ return false;
21
+ }
22
+
23
+ if(i == s.length()-1){
24
+ return false;
25
+ }
26
+
27
+ countPlusMinus++;
28
+ }
29
+ //dot
30
+ else if(ch == '.'){
31
+ if(eseen || dotseen){
32
+ return false;
33
+ }
34
+
35
+ if(i == s.length() -1 && !digitseen){
36
+ return false;
37
+ }
38
+
39
+ dotseen = true;
40
+ }
41
+
42
+ //e/E
43
+ else if(ch == 'e' || ch == 'E'){
44
+ if(eseen || !digitseen || i == s.length()-1){
45
+ return false;
46
+ }
47
+
48
+ eseen = true;
49
+ }
50
+ else{
51
+ return false;
52
+ }
53
+
54
+ }
55
+
56
+ return true;
57
+ }
58
+ }
59
+
60
+ import java.util.regex.*;
61
+ class Solution {
62
+ public boolean isNumber(String s) {
63
+ return Pattern.matches("^([+-]?(((\\d+\\.\\d*)|(\\.\\d+))|(\\d+))([eE][+-]?\\d+)?)$", s);
64
+ }
65
+
66
+ }
You can’t perform that action at this time.
0 commit comments