第一题
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
for(int i=0;i<nums.size();i++)
{
for(int j=i+1;j<nums.size();j++)
{
if(nums[i]+nums[j]==target)
{
res.push_back(i);
res.push_back(j);
}
}
}
return res;
}
};
第二题
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int num = 0;
ListNode* RES=new ListNode(0);
ListNode* res = RES;
// l1 = l1->next;
// l2 = l2->next;
while (l1||l2)
{
int count=0;
if (l1&& l2)
{
count = l1->val + l2->val;
l1 = l1->next;
l2 = l2->next;
}
else if (l1&& !l1)
{
count = l1->val;
l1 = l1->next;
}
else if (!l1 && l2)
{
count = l2->val;
l2 = l2->next;
}
int val = (count + num) % 10;
res->next = new ListNode(val);
res = res->next;
num = (count + num) / 10;
}
if (num != 0)
{
res->next = new ListNode(num);
}
return RES->next;
}
};