-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_65_ValidNumber.cpp
158 lines (138 loc) · 4.77 KB
/
LC_65_ValidNumber.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
https://leetcode.com/problems/valid-number/
65. Valid Number
*/
class Solution {
public:
// decimal number/integer + e/E + integer
bool isNumber_1(string s) {
int n = s.length();
// if e/E is first/last char
if(s[0] == 'e' || s[0] == 'E' || s[n-1] == 'e' || s[n-1] == 'E')
return false;
// check presence of e/E, dots, and other character
int cnt_e = 0, pos_e = s.length();
int cnt_dots=0, pos_dot = s.length()-1; //dot can present in the last also;
int cnt_befDotSign=0, pos_befDotSign=-1;
int cnt_aftDotSign=0, pos_aftDotSign=-1;
int cnt_digits=0;
for(int i=0; i<s.length(); i++)
{
if(s[i] == 'e' or s[i] == 'E')
{
pos_e = i;
cnt_e++;
}
else if(s[i] == '.')
{
pos_dot = i;
cnt_dots++;
}
else if(s[i] == '+' || s[i] == '-')
{
if(cnt_e==0)
{
pos_befDotSign=i;
cnt_befDotSign++;
}
else
{
pos_aftDotSign=i;
cnt_aftDotSign++;
}
}
else if(isdigit(s[i]))
cnt_digits++;
else if(isalpha(s[i])) // other character
return false;
}
// cout<<cnt_e<<" "<<cnt_dots<<" "<<pos_dot<<" "<<pos_e<<endl;
if(cnt_digits==0 || cnt_e > 1 || cnt_dots > 1 || (cnt_dots && pos_dot > pos_e))
return false; // more than atmost one e/E, dots and dot if any should be before e/E
// cout<<cnt_befDotSign<<" 1 "<<cnt_aftDotSign<<endl;
if(cnt_befDotSign>1 || cnt_aftDotSign>1)
return false; // if more sign
// cout<<cnt_befDotSign<<" 2 "<<pos_befDotSign<<endl;
if(cnt_befDotSign && pos_befDotSign!=0)
return false; //sign not in front if present
// cout<<cnt_aftDotSign<<" 3 "<<pos_aftDotSign<<endl;
if(cnt_aftDotSign && pos_aftDotSign!=pos_e+1)
return false; //sign not after e if present
string bef_e = s.substr(0, pos_e); // if e present else whole string
string aft_e = s.substr(pos_e + (cnt_e>0)); // if e present then e+after_string
// cout<<bef_e<<" be\n ";
// cout<<aft_e<<endl;
// if dot present then check for decimal else integer
if(((cnt_dots && is_decimal(bef_e) or is_integer(bef_e)) and is_integer(aft_e)))
return true;
else
return false;
}//end
bool is_integer(string &s)
{
if(s.length()==1 && (s[0]=='+' || s[0]=='-' ))
return false;
for(int i=0; i<s.length(); i++)
{
if(s[i] == '+' or s[i] == '-')
continue;
else if(!isdigit(s[i]))
return false;
}
return true;
}
bool is_decimal(string &s)
{
if(s.length()==1 && (s[0]=='+' || s[0]=='-' || s[0]=='.'))
return false;
for(int i=0; i<s.length(); i++)
{
if(s[i] == '+' or s[i] == '-' or s[i]=='.')
continue;
else if(!isdigit(s[i]))
return false;
}
return true;
}
bool isNumber(string s) {
bool foundDigit = false; //atleast one digit
bool foundPeriod = false; // for period book keeping
bool foundE = false; // for e/E
bool checkSign = true; // check for sign
for(char c: s)
{
// check the sign once, then turns itself off
if(checkSign and (c=='-' or c=='+'))
{
checkSign = false;
continue;
}
checkSign = false;
if(c == '.')
{
if(not foundE and not foundPeriod) // e/E se phle and first time
foundPeriod=true;
else
return false; // e/E ke baad, second time,
continue;
}
if(c == 'e' or c=='E')
{
if(not foundE and foundDigit) // phele e/E nhi mila and ek digit to mila ho
foundE = true;
else
return false;
checkSign = true; // check for sign once again.
foundDigit = false; // check for digit once again.
continue;
}
if(isdigit(c))
{
foundDigit = true;
continue;
}
return false;
}
return foundDigit;
}
};