LeetCode 002 [Add Two Numbers]

Problem

Add Two Numbers
You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* 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) {
ListNode *result, *temp, *head;
int tmp;
result = (ListNode *)malloc(sizeof(ListNode));
result->next = NULL;
head = (ListNode *)malloc(sizeof(ListNode));
head->next = NULL;
int mark = 0, add;
temp = result;
while (1)
{
head = (ListNode *)malloc(sizeof(ListNode));
add = l1->val + l2->val + mark;
temp->val = add % 10;
mark = add / 10;
l1 = l1->next;
l2 = l2->next;
if (l1 == NULL && l2 == NULL)
{
if (mark > 0)
{
temp->next = head;
temp = head;
temp->val = mark;
temp->next = NULL;
return result;
}
temp = NULL;
return result;
}
else if (l1 == NULL)
{
l1 = (ListNode *)malloc(sizeof(ListNode));
l1->val = 0;
l1->next = NULL;
temp->next = head;
temp = head;
temp->next = NULL;
}
else if (l2 == NULL)
{
l2 = (ListNode *)malloc(sizeof(ListNode));
l2->val = 0;
l2->next = NULL;
temp->next = head;
temp = head;
temp->next = NULL;
}
else
{
temp->next = head;
temp = head;
temp->next = NULL;
}
}
}
};

备注

  1. 对C++链表操作不熟;
  2. 可以用递归解决;

LeetCode 001 [Two Sum]

Problem

Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target)
{
vector<int> num = numbers, result;
int sum;
sort(num.begin(), num.end());
vector<int>::iterator it1 = num.begin(), it2 = num.end() - 1;
for (it1 = num.begin(), it2 = num.end() - 1; it1 < it2;)
{
sum = *it1 + *it2;
if (sum == target)
{
return Index(numbers, *it1, *it2);
}
else if (sum < target)
{
it1++;
}
else
{
it2--;
}
}
return result;
}

vector<int> Index(vector<int> numbers, int num1, int num2)
{
int temp;
vector<int> index;
if (num1 > num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
for (int i = 0; i < numbers.size(); i++)
{
if (num1 == numbers[i])
{
index.push_back(i + 1);
}
else if (num2 == numbers[i])
{
index.push_back(i + 1);
}
}
return index;
}
};

备注

  1. 对C++面向对象编程不熟,需要继续学习;
  2. 对问题描述没有搞清楚;
  3. 可以考虑用HashMap解决,但还不会用。