Skip to content

Commit 6281380

Browse files
committed
add 46-48
1 parent 782ed66 commit 6281380

File tree

4 files changed

+171
-1
lines changed

4 files changed

+171
-1
lines changed

CombinationSum.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
3+
4+
The same repeated number may be chosen from C unlimited number of times.
5+
6+
Note:
7+
All numbers (including target) will be positive integers.
8+
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
9+
The solution set must not contain duplicate combinations.
10+
11+
For example, given candidate set 2,3,6,7 and target 7,
12+
A solution set is:
13+
[7]
14+
[2, 2, 3]
15+
*/
16+
class Solution {
17+
public:
18+
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
19+
vector<vector<int> > result;
20+
vector<int> com;
21+
if(candidates.empty())
22+
return result;
23+
sort(candidates.begin(), candidates.end());
24+
sumHelper(candidates, target, result, com, 0, 0);
25+
return result;
26+
}
27+
void sumHelper(vector<int> &candidates, int target, vector<vector<int> > &result, vector<int> &com, int sum, int k)
28+
{
29+
if(sum > target)
30+
return;
31+
else if(sum == target)
32+
result.push_back(com);
33+
else
34+
{
35+
for(int i = k; i < candidates.size(); ++i)
36+
{
37+
com.push_back(candidates[i]);
38+
sumHelper(candidates, target, result, com, sum+candidates[i], i);
39+
com.pop_back();
40+
}
41+
}
42+
}
43+
};

MultiplyStrings.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Given two numbers represented as strings, return multiplication of the numbers as a string.
3+
4+
Note: The numbers can be arbitrarily large and are non-negative.
5+
*/
6+
class Solution {
7+
public:
8+
string multiply1(string num1, string num2) {
9+
reverse(num1.begin(), num1.end());
10+
reverse(num2.begin(), num2.end());
11+
string result(num1.size()+num2.size()+1, '0');
12+
for(int i = 0; i < num1.size(); ++i)
13+
{
14+
int carry = 0;
15+
int n1 = num1[i] - '0';
16+
for(int j = 0; j < num2.size(); ++j)
17+
{
18+
int n2 = num2[j] - '0';
19+
int mul = n1*n2 + carry + (result[i+j]-'0');
20+
carry = mul/10;
21+
result[i+j] = mul%10 + '0';
22+
}
23+
if(carry > 0)
24+
result[i+num2.size()] = carry + '0';
25+
}
26+
reverse(result.begin(), result.end());
27+
int k = 0;
28+
while(k < result.size() && result[k] == '0')
29+
++k;
30+
if(k == result.size())
31+
return "0";
32+
return result.substr(k);
33+
}
34+
};
35+
36+
class Solution {
37+
public:
38+
string multiply2(string num1, string num2) {
39+
string result(num1.size()+num2.size(), '0');
40+
for(int i = num1.size()-1; i >= 0; --i)
41+
{
42+
int carry = 0;
43+
for(int j = num2.size()-1; j >= 0; --j)
44+
{
45+
int mul = carry + (num1[i]-'0') * (num2[j]-'0') + result[i+j+1]-'0';
46+
carry = mul/10;
47+
result[i+j+1] = mul%10 + '0';
48+
}
49+
result[i] += carry;
50+
}
51+
while(result.size() > 1 && result[0] == '0')
52+
result.erase(result.begin());
53+
return result;
54+
}
55+
};

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,19 @@ Solve problems in LeetCode http://oj.leetcode.com/.
211211
45. Search for a Range
212212

213213
Be careful of index bound!
214-
L points to the index of first target, R points to the next index of last target.
214+
L points to the index of first target, R points to the next index of last target.
215+
216+
46. Combination Sum
217+
218+
DFS. The start index is the same as current due to the repeated element.
219+
220+
47. Multiple Strings
221+
222+
Reverse two strings, multiple one by one.
223+
OR do as what we do to multiple two numbers.
224+
Be careful about index of the result.
225+
226+
48. Reorder List
227+
228+
Reverse the last half of the list, then insert each node to thr first half one by one.
229+

ReorderList.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
3+
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
4+
5+
You must do this in-place without altering the nodes' values.
6+
7+
For example,
8+
Given {1,2,3,4}, reorder it to {1,4,2,3}.
9+
*/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* struct ListNode {
14+
* int val;
15+
* ListNode *next;
16+
* ListNode(int x) : val(x), next(NULL) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
void reorderList(ListNode *head) {
22+
if(!head || !head->next || !head->next->next)
23+
return;
24+
ListNode *fast = head, *slow = head;
25+
while(fast->next && fast->next->next)
26+
{
27+
fast = fast->next->next;
28+
slow = slow->next;
29+
}
30+
// reverse last half
31+
ListNode *half = reverse(slow);
32+
fast = head;
33+
while(half)
34+
{
35+
ListNode *tmp1 = fast->next;
36+
ListNode *tmp2 = half->next;
37+
fast->next = half;
38+
half->next = tmp1;
39+
half = tmp2;
40+
fast = tmp1;
41+
}
42+
}
43+
ListNode *reverse(ListNode *root)
44+
{
45+
ListNode *node = root->next, *next = NULL;
46+
root->next = NULL;
47+
while(node->next)
48+
{
49+
ListNode *tmp = node->next;
50+
node->next = next;
51+
next = node;
52+
node = tmp;
53+
}
54+
node->next = next;
55+
return node;
56+
}
57+
};

0 commit comments

Comments
 (0)