2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
// 进位
int a = 0;
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if(l1==NULL&&l2==NULL&&a==0)return 0;
l1!=NULL?(a+=l1->val,l1=l1->next):l1;
l2!=NULL?(a+=l2->val,l2=l2->next):l2;
ListNode *cur= new ListNode(a%10);
a /=10;
cur->next= addTwoNumbers(l1,l2);
return cur;
}
};
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int m = 200;
bool a[m] = { 0 };
int max_tmp = 0, max_true = 0;
for (int i = 0; i < s.length(); i++) {
a[s[i]] = 1;
max_tmp = 1;
for (int j = i+1; j < s.length(); j++) {
if (a[s[j]] == 1)break;
a[s[j]] = 1;
max_tmp++;
}
if (max_true < max_tmp)
max_true = max_tmp;
//Initializate
for (int k = 0; k < m; k++)
a[k] = 0;
}
return max_true;
}
};