题目一:链表
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int carry = 0;
ListNode *root = new ListNode(0), *n = root;
while (l1 != NULL || l2 != NULL || carry) {
int v1 = 0, v2 = 0;
if (l1 != NULL) {
v1 = l1->val;
l1 = l1->next;
}
if (l2 != NULL) {
v2 = l2->val;
l2 = l2->next;
}
int val = (v1 + v2 + carry) % 10;
carry = (v1 + v2 + carry) >= 10 ? 1 : 0;
ListNode *cur = new ListNode(val);
n->next = cur;
n = n->next;
}
return root->next;
}
};
题目二:最长不重复子串
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int freq[256] = { 0 };//记录是否出现
int l = 0, r = -1;
int res = 0;
while (r+1<s.size())
{
if (freq[s[r + 1]] == 0)//没出现过 标记为出现 后移r
{
freq[s[r + 1]] = 1;
r++;
}
else //出现过 清除l所在位置字符 后移l
{
freq[s[l]] = 0;
l++;
}
res = max(res, r - l + 1);
}
return res;
}
};