Skip to content

Commit f3c609f

Browse files
committed
forget to add new files
1 parent 4ed761d commit f3c609f

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

analysis/add-two-numbers.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
##Add Two Numbers [ [sourcecode](../src/AddTwoNumbers.cpp) | [problem](https://oj.leetcode.com/problems/add-two-numbers/) ]
3+
4+
##分析
5+
题目意思是将两个用链表表示的两个数相加,返回用链表表示的结果。而且数的表示方法从最低位开始,进一步降低了难度。
6+
例如:3544的链表表示方法是4->4->5->3->null。
7+
8+
#### 直接相加
9+
从链表头开始,一位一位的模拟加法运算,注意保留进位,在下一位相加时得加上来自低位的进位即可。
10+
例如: 3->4 + 9->3
11+
3+9=12 : 所以个位是2,进位是1
12+
4+3=7,加上进位 =8,所以十位为8
13+
结果是1->8
14+
15+
16+
##注意:
17+
99+99这样的情况会产生更高位
18+
19+
20+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
##Longest Substring Without Repeating Characters [ [sourcecode](../src/LongestSubstringWithoutRepeatingCharacters.cpp) | [problem](https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/) ]
3+
4+
##分析
5+
找字符串的不包含相同字符的最长字串。
6+
7+
#### 简单动态规划
8+
len[i]表示以s[i]结尾的不包含相同字符的最长字串长度。
9+
len[i]计算方法:
10+
11+
* 如果s[i-1]结尾的最长字串中包含s[i],那么len[i]=i- last_position[s[i]]
12+
* 否则就是s[i-1]结尾的字符串长度加1, len[i] = len[i-1] + 1
13+
14+
##注意:
15+
这题没什么坑,就是注意一下初始化条件。last_position初始化为-1, len数组初始化为0
16+
17+
18+

src/AddTwoNumbers.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
3+
class Solution {
4+
public:
5+
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
6+
ListNode *p = new ListNode(0);
7+
ListNode *head = p;
8+
int num = 0;
9+
while (l1 || l2) {
10+
p->next = new ListNode(0);
11+
p = p->next;
12+
13+
if (l1) {
14+
num += l1->val;
15+
l1 = l1->next;
16+
}
17+
if (l2) {
18+
num += l2->val;
19+
l2 = l2->next;
20+
}
21+
22+
if (num >= 10){
23+
p->val = num % 10;
24+
num = num/10;
25+
} else {
26+
p->val = num;
27+
num = 0;
28+
}
29+
}
30+
31+
if (num > 0) {
32+
p->next = new ListNode(0);
33+
p = p->next;
34+
p->val = num;
35+
}
36+
return head->next;
37+
}
38+
};
39+
int main(int argc, char *argv[]) {
40+
// Example
41+
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
42+
// Output: 7 -> 0 -> 8
43+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
class Solution {
5+
public:
6+
int lengthOfLongestSubstring(string s) {
7+
if ( s == "" )
8+
return 0;
9+
10+
// 记录每一个字符至今位置最后出现的位置
11+
int charPos[256];
12+
13+
// 开始扫描前,每个字符出现位置为-1
14+
for(int i= 0 ;i<256; i++)
15+
charPos[i] = -1;
16+
17+
// 开始扫描前,最长字串为0
18+
int maxLen = 0;
19+
// 开始扫描前,前一个字符结尾的最长字串长度为0
20+
int lastLen = 0;
21+
22+
// 从第一个开始
23+
for (int i = 0; i < s.length(); i++) {
24+
/**
25+
* 以s[i]结尾的最长字串长度计算方法:
26+
* 如果s[i-1]结尾的最长字串中包含s[i],那么len[i]=i- last_position[s[i]]
27+
* 否则就是s[i-1]结尾的字符串长度加1, len[i] = len[i-1] + 1
28+
*/
29+
if (charPos[s[i]] >= i - lastLen) {
30+
lastLen = i - charPos[s[i]];
31+
} else {
32+
lastLen ++;
33+
}
34+
cout<<lastLen<<endl;
35+
if (maxLen < lastLen)
36+
maxLen = lastLen;
37+
charPos[s[i]] = i;
38+
}
39+
return maxLen;
40+
}
41+
};
42+
int main(int argc, char *argv[]) {
43+
Solution s;
44+
cout << s.lengthOfLongestSubstring("ruowzgiooobpple");
45+
46+
}

0 commit comments

Comments
 (0)