Skip to content

Commit e1dc213

Browse files
committed
palindromic and atoi
1 parent 6590d01 commit e1dc213

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
##Longest Palindromic Substring [ [sourcecode(中心扩展)](../src//LongestPalindromicSubstring.cpp) | [problem](https://oj.leetcode.com/problems//longest-palindromic-substring/) ]
3+
4+
##分析
5+
找最长回文字串。
6+
7+
#### 最笨的方法,暴力枚举,复杂度O(n^3)
8+
找出每一个子串,然后判定子串是否是回文,如果是回文则记录长度,然后将最长的输出。
9+
10+
```
11+
for i < strlen
12+
for j < strlen
13+
判断substring(i, j-i)是否是回文
14+
```
15+
#### 中心扩展法 复杂度O(n^2)
16+
所谓中心扩展,就是以字符串中每一个字符为中心,搜索以它为中心的字符串长度,并记录,之后输出最长的。
17+
这个方法有个地方需要注意的就是,奇数回文串和偶数回文串需要分开考虑。
18+
例如abaabba, 其中,
19+
aba是奇数回文串,对于这种,直接以b为中心,然后分别往左和往右扩展,直到左右不相等即可。
20+
abba是偶数回文串,对于这种,以两个b为中心,然后向两边扩展,直到左右不相等。
21+
22+
#### DP算法 复杂度O(n^2)
23+
P[i,j]=0表示子串[i,j]不是回文串。P[i,j]=1表示子串[i,j]是回文串。
24+
25+
初始化:P[i,i]=1
26+
dp方程:
27+
28+
P[i,j] = P[i+1,j-1], if(s[i]==s[j])
29+
P[i,j] = 0 , if(s[i]!=s[j])
30+
31+
32+
#### Manacher算法 复杂度O(n)
33+
34+
35+
36+
37+
38+

analysis/string-to-integer-atoi.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@file string-to-integer-atoi/.md
2+
@author Brian
3+
@version 1.0
4+
@date 2014-12-07
5+
6+
7+
##Two Sum [ [sourcecode](../src/StringToIntegerAtoi.cpp) | [problem](https://oj.leetcode.com/problems/string-to-integer-atoi/) ]
8+
9+
##分析
10+
atoi的题目虽然在面试中已经面烂了,但是自己写这道题的时候还是提交了将近10次才过。问题的根源在于未在写代码之前分析清楚各种边界情况。
11+
12+
###坑:
13+
1. 首先,前导空格处理
14+
2. 正负号处理,可以有正负号
15+
3. 溢出处理
16+
17+
其中前两个想到了就比较简单,关键第3个如何处理。在leetcode里的设定是溢出时返回最接近的值。
18+
19+

src/LongestPalindromicSubstring.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
class Solution {
5+
public:
6+
string longestPalindrome(string s) {
7+
int maxLen = 0;
8+
int len = 0;
9+
int start = 0;
10+
int slen = s.length();
11+
// 处理aba的情况
12+
for (int i = 0; i < slen; i++){
13+
len = 0;
14+
int j = 0;
15+
for (j = 1; j < std::min(slen-i, i+1); j++) {
16+
if (s[i+j] != s[i-j]) {
17+
break;
18+
}
19+
}
20+
j -= 1;
21+
if (j*2+1 > maxLen) {
22+
maxLen = j*2+1;
23+
start = i - j;
24+
}
25+
}
26+
// 处理abba的情况
27+
for (int i = 1; i < slen; i++){
28+
if (s[i] == s[i-1]) {
29+
len = 0;
30+
int j = 0;
31+
for (j = 1; j < std::min(i,slen-i); j++) {
32+
if (s[i-1-j] != s[i+j]) {
33+
break;
34+
}
35+
}
36+
j -= 1;
37+
if (j*2+2 > maxLen) {
38+
maxLen = j*2+2;
39+
start = i - 1 - j;
40+
}
41+
}
42+
43+
}
44+
//cout<<start<<" "<<maxLen<<endl;
45+
return s.substr(start, maxLen);
46+
47+
}
48+
};
49+
int main(int argc, char *argv[]) {
50+
Solution s;
51+
cout<<s.longestPalindrome("aaaaaa");
52+
}

src/StringToIntegerAtoi.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Brian
3+
* 2014.12.7
4+
*/
5+
#include <iostream>
6+
7+
using namespace std;
8+
class Solution {
9+
public:
10+
int atoi(const char *str) {
11+
int len = strlen(str);
12+
if (str == NULL || len == 0) {
13+
return 0;
14+
}
15+
long long res = 0;
16+
int factor = 1;
17+
int i = 0;
18+
while (i < len && str[i] == ' ') {
19+
i++;
20+
}
21+
if (str[i] == '+') {
22+
factor = 1;
23+
i++;
24+
} else if (str[i] == '-') {
25+
factor = -1;
26+
i++;
27+
}
28+
29+
for (; i < strlen(str); i++) {
30+
if (str[i] >= '0' && str[i] <= '9'){
31+
res = res * 10 + str[i] - '0';
32+
if (res * factor > INT_MAX) {
33+
res = INT_MAX;
34+
break;
35+
}
36+
if (res * factor < INT_MIN) {
37+
res = -INT_MIN;
38+
break;
39+
}
40+
}
41+
else
42+
break;
43+
}
44+
45+
return res * factor ;
46+
}
47+
};
48+
49+
int main(int argc, char *argv[]) {
50+
Solution s;
51+
cout << s.atoi("-9223372036854775809");
52+
}

0 commit comments

Comments
 (0)