Skip to content

Commit 6590d01

Browse files
committed
reverse integer & zigzag conversion
1 parent 3c060f1 commit 6590d01

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ Solutions for leetcode.
99
#### No.2 [Median of Two Sorted Arrays](analysis/median-of-two-sorted-arrays.md)
1010
#### No.3 [Longest Substring Without Repeating Characters ](analysis/longest-substring-without-repeating-characters.md)
1111
#### No.4 [Add Two Numbers ](analysis/add-two-numbers.md)
12+
#### No.6 [ZigZag Conversion ](analysis/zigzag-conversion.md)
13+
#### No.7 [Reverse Integer ](analysis/reverse-integer.md)
1214
#### No.x [Word Ladder](analysis/word-ladder.md)
1315

analysis/reverse-integer.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
##Reverse Integer [ [sourcecode](../src/ReverseInteger.cpp) | [problem](https://oj.leetcode.com/problems/reverse-integer/) ]
3+
4+
##分析
5+
将一个数字翻转获得新数字。
6+
7+
#### 解法
8+
解法很简单,取模除10,直到为0。
9+
10+
##注意:
11+
1. 100,2000这样的翻过了是1,2
12+
2. 超过int存储值的例如10 0000 0003,反过来30 0000 0001,超过了int可表示范围,溢出然后变成了1
13+
3. 负数情况,按照正数处理,然后在加上符号
14+
15+
16+

analysis/zigzag-conversion.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
##ZigZag Conversion [ [sourcecode](../src/ZigZagConversion.cpp) | [problem](https://oj.leetcode.com/problems/zigzagconversion/) ]
3+
4+
##分析
5+
将字符串按照zigzag形式输出。纯模拟。找规律。
6+
7+
#### 模拟
8+
第一行和最后一行:s[i+2*(nRows - 1) * k],k=0,2,3,,,,
9+
中间的行:s[i+2*(nRows - 1) * k],k=0,2,3,,,,
10+
s[-i+2*(nRows - 1) * k],k=1,2,3,,,,
11+
12+
13+
##注意:
14+
这题着实有些坑,算法很简单,直接模拟就行,根据规律一行一行的扫描输出,关键是TLE和MLE让人蛋疼。
15+
16+
1. 不能使用char数组然后再转化成string,会导致TLE
17+
2. 不能直接新建string然后不断append,会导致Memory Limit Exceeded,估计是string扩展的方式是翻倍的,反正初始化结果string的时候,调用resize预先分配好空间即可。
18+
3. 考虑nRows为1的情况,直接返回s
19+
20+
21+

src/ReverseInteger.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
class Solution {
5+
public:
6+
int reverse(int x) {
7+
int y = abs(x);
8+
int ans = 0;
9+
while(y ) {
10+
ans = ans * 10 + (y % 10);
11+
y /= 10;
12+
}
13+
if (x < 0)
14+
ans = -ans;
15+
return ans;
16+
}
17+
};
18+
int main(int argc, char *argv[]) {
19+
20+
}

src/ZigZagConversion.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
class Solution {
5+
public:
6+
string convert(string s, int nRows) {
7+
if (nRows == 1) {
8+
return s;
9+
}
10+
int len = s.length();
11+
string result;
12+
result.resize(len,0);
13+
int p = 0, j =0;
14+
int step = 2 * (nRows-1);
15+
for (int i = 0; i< nRows; i++) {
16+
if (i == 0 || i == nRows -1) {
17+
j = i;
18+
while(j < len){
19+
result[p++] = s[j];
20+
j += step;
21+
}
22+
} else {
23+
j = i;
24+
int k = step - i;
25+
while(j < len || k < len){
26+
if (j < len)
27+
result[p++] = s[j];
28+
if (k < len)
29+
result[p++] = s[k];
30+
31+
j += step;
32+
k+=step;
33+
}
34+
35+
}
36+
}
37+
return result;
38+
}
39+
};
40+
int main(int argc, char *argv[]) {
41+
Solution s;
42+
cout << s.convert("PAYPALISHIRING", 3);
43+
}

0 commit comments

Comments
 (0)