File tree 4 files changed +78
-0
lines changed
4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1
1
* .exec
2
2
* .swp
3
3
a.out
4
+ t.cpp
Original file line number Diff line number Diff line change 2
2
3
3
class Solution {
4
4
public:
5
+ /*
5
6
int lengthOfLongestSubstring(string s)
6
7
{
7
8
int ret = 0;
@@ -26,6 +27,31 @@ class Solution {
26
27
ret = i - subStrBegin;
27
28
return ret;
28
29
}
30
+ */
31
+ int lengthOfLongestSubstring (string s)
32
+ {
33
+ int ret = 0 ;
34
+ int subStrBegin = 0 ;
35
+ int lastPosOfChar[256 ]; // 用已出现过的字符索引的表, 下标为字符,值为出现位置
36
+ for (int i = 0 ; i < 256 ; ++i)
37
+ lastPosOfChar[i] = -1 ;
38
+
39
+ const int size = s.size ();
40
+ for (int i = 0 ; i < size; ++i)
41
+ {
42
+ int ch = s[i];
43
+ if (lastPosOfChar[ch] >= subStrBegin) // 已经出现过且在子本字符串内
44
+ {
45
+ if (ret < i - subStrBegin)
46
+ ret = i - subStrBegin ;
47
+ subStrBegin = lastPosOfChar[ch] + 1 ;
48
+ }
49
+ lastPosOfChar[ch] = i;
50
+ }
51
+ if (ret < size - subStrBegin)
52
+ ret = size - subStrBegin;
53
+ return ret;
54
+ }
29
55
};
30
56
31
57
Original file line number Diff line number Diff line change
1
+ #include " test.h"
2
+
3
+ class Solution {
4
+ public:
5
+ int myAtoi (string str)
6
+ {
7
+ int64_t ret = 0 ;
8
+
9
+ int sign = 1 ;
10
+
11
+ bool discarding = true ;
12
+
13
+ for (char ch : str)
14
+ {
15
+ if (discarding && (ch == ' ' || ch == ' 0' ))
16
+ continue ;
17
+
18
+ if (discarding && ch == ' -' )
19
+ {
20
+ sign = -1 ;
21
+ }
22
+ else if (discarding && ch == ' +' )
23
+ {
24
+ }
25
+ else if (ch >= ' 0' && ch <= ' 9' )
26
+ {
27
+ ret *= 10 ;
28
+ ret += (ch - ' 0' );
29
+ }
30
+ else
31
+ {
32
+ break ;
33
+ }
34
+
35
+ discarding = false ;
36
+
37
+ if (ret > 0x7fffffff )
38
+ return sign == 1 ? 0x7fffffff : 0x80000000 ;
39
+ }
40
+
41
+ return sign * ret;
42
+ }
43
+ };
44
+
45
+
46
+ int main ()
47
+ {
48
+ Solution s;
49
+ cout << s.myAtoi (" -123b" ) << endl;
50
+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ targets = \
6
6
5_longest_palindromic_substring.exec\
7
7
6_zigzag_conversion.exec\
8
8
7_reverse_interger.exec\
9
+ 8_string_to_interger.exec\
9
10
10
11
11
12
CC = g++
You can’t perform that action at this time.
0 commit comments