-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathValidNumber.java
84 lines (65 loc) · 3.21 KB
/
ValidNumber.java
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
/*https://leetcode.com/problems/valid-number/*/
class Solution {
public boolean isNumber(String s) {
//first and last characters cannot be exponents, hence return false
if (s.charAt(0) == 'e' || s.charAt(0) == 'E' || s.charAt(s.length()-1) == 'e' || s.charAt(s.length()-1) == 'E') return false;
//split by exponents
String[] tokens = s.split("[Ee]");
//if there are zero tokens or greater than two tokens, return false
if (tokens.length > 2 || tokens.length == 0) return false;
boolean result = true;
//if there is just one token, it can be either a decimal or an integer
if (tokens.length == 1)
result = isDecimal(tokens[0]) || isInteger(tokens[0]);
//if there are two tokens, first token can be a decimal or an integer and second must be an integer
else
result = (isDecimal(tokens[0]) || isInteger(tokens[0])) && isInteger(tokens[1]);
return result;
}
private boolean isDecimal(String s)
{
//decimals cannot be of length zero
if (s.length() == 0) return false;
//decimals cannot contain only sign or dot
if (s.length() == 1 && (s.charAt(0) == '+' || s.charAt(0) == '-' || s.charAt(0) == '.')) return false;
//decimals cannot contain only sign and dot
if (s.length() == 2 && (s.charAt(0) == '+' || s.charAt(0) == '-') && s.charAt(1) == '.') return false;
//if the first character is something apart from sign, dot or digit, return false
if (!(s.charAt(0) == '+' || s.charAt(0) == '-' || s.charAt(0) == '.' || (s.charAt(0) <= '9' && s.charAt(0) >= '0')))
return false;
//mark if dot is encountered
boolean dotEncountered = s.charAt(0) == '.' ? true : false;
//for rest of the characters apart from the first one
for (int i = 1; i < s.length(); ++i)
{
//if the first dot is encountered, mark it
if (s.charAt(i) == '.' && !dotEncountered)
dotEncountered = true;
//if the second dot is encountered, return false
else if (s.charAt(i) == '.' && dotEncountered)
return false;
//if the character is not a digit, return false
else if (!(s.charAt(i) <= '9' && s.charAt(i) >= '0'))
return false;
}
//return true if for loop is uninterrupted
return true;
}
private boolean isInteger(String s)
{
//integers cannot be of length zero
if (s.length() == 0) return false;
//integers cannot contain only sign
if (s.length() == 1 && (s.charAt(0) == '+' || s.charAt(0) == '-')) return false;
//if the first character is something apart from sign or digit, return false
if (!(s.charAt(0) == '+' || s.charAt(0) == '-' || (s.charAt(0) <= '9' && s.charAt(0) >= '0')))
return false;
//for rest of the characters apart from the first one
for (int i = 1; i < s.length(); ++i)
//if the character is not a digit, return false
if (!(s.charAt(i) <= '9' && s.charAt(i) >= '0'))
return false;
//return true if for loop is uninterrupted
return true;
}
}