Skip to content

Commit e333bf4

Browse files
committed
leetcode
1 parent 902b1d0 commit e333bf4

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

leetcode/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,5 +525,3 @@ h[n] = 2 * sum( h[i] * h[n-i] ) (1<= i < n )
525525
* [Minimum Window Substring](https://oj.leetcode.com/problems/minimum-window-substring/) 字符串滑动窗口, 维护4个变量: left 左端点 ; right 右端点 ; cur[256] 各字符当前窗口内的计数值; curlen 当前窗口内目标串字符的个数。 时间复杂度O(N). 类似的题: [Substring with Concatenation of All Words ](https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/), [Longest Substring Without Repeating Characters ](https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/) .
526526
527527
528-
529-

leetcode/a.out

-621 Bytes
Binary file not shown.

leetcode/valid-number.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <string>
4+
#include <cstring>
5+
6+
using namespace std;
7+
8+
9+
class Solution {
10+
public:
11+
12+
bool nativeInt(const char *s, int l ,int r){
13+
if(l > r) return false;
14+
for(; l <= r ; ++ l)
15+
if(!isdigit(s[l])) return false;
16+
return true;
17+
}
18+
19+
bool isInt(const char *s, int l, int r){
20+
if(l > r) return false;
21+
if(s[l] == '-' || s[l] == '+') ++l;
22+
if(l > r) return false;
23+
return nativeInt(s, l , r);
24+
}
25+
26+
bool isDecimal(const char *s, int l, int r){
27+
int i, dot = -1;
28+
if(l > r) return false;
29+
if(s[l] == '-' || s[l] == '+') ++l;
30+
for(i = l ; i <= r ; ++ i) if(s[i] == '-' || s[i] == '+') return false;
31+
if(l > r) return false;
32+
for(i = l ; i <= r ; ++ i) if(s[i] == '.') { dot = i ; break; }
33+
if(dot == -1) return nativeInt(s, l , r);
34+
if(dot == l) return nativeInt(s, l+1, r);
35+
if(dot == r) return nativeInt(s, l, r-1);
36+
return isInt(s, l, dot-1) && isInt(s, dot+1, r);
37+
}
38+
39+
bool isNumber(const char *s) {
40+
int i , l, r, n = strlen(s), e = -1;
41+
l = 0 ; r = n - 1 ;
42+
while(l <= r && s[l] == ' ') ++l;
43+
while(l <= r && s[r] == ' ') --r;
44+
if(l > r ) return false;
45+
for(i = l ;i <= r ; ++ i) if(s[i] == 'e') { e = i; break; }
46+
if(e == -1){
47+
return isDecimal(s, l, r);
48+
}
49+
return isDecimal(s, l, e-1) && isInt(s, e+1, r);
50+
}
51+
};
52+
53+
54+
int main(){
55+
char s[][100] = {
56+
"0",
57+
" -0.1 ",
58+
" +0.1 ",
59+
" ++0.1 ",
60+
"abc",
61+
"1 a",
62+
"2e10",
63+
"2e-10",
64+
"2ee-10",
65+
};
66+
Solution sol;
67+
for(int i = 0 ; i < 9 ; ++ i)
68+
cout << sol.isNumber(s[i]) << endl;
69+
return 0 ;
70+
}

0 commit comments

Comments
 (0)