Skip to content

Commit 7c61bdc

Browse files
author
fupengfei
committed
12/18 COMMIT
1 parent b6b37a7 commit 7c61bdc

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed

Leetcode/16. 3Sum Closest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*3Sum Closest:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
2+
Return the sum of the three integers. You may assume that each input would have exactly one solution.*/
3+
class Solution {
4+
public:
5+
int threeSumClosest(vector<int>& nums, int target) {
6+
int n = nums.size();
7+
if(n < 3) return 0;
8+
sort(nums.begin(), nums.end());
9+
int res = nums[0] + nums[1] + nums[2];
10+
int i, j, k;
11+
int val;
12+
for (i = 0; i < n - 2; ++i) {
13+
j = i + 1;
14+
k = n - 1;
15+
while (j < k) {
16+
val = nums[i] + nums[j] + nums[k];
17+
if (abs(val - target) < abs(res - target)) {
18+
res = val;
19+
}
20+
if (val < target) {
21+
++j;
22+
} else if (val > target) {
23+
--k;
24+
} else {
25+
return target;
26+
}
27+
}
28+
}
29+
return res;
30+
}
31+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*Letter Combinations of a Phone Number:Given a digit string, return all possible letter combinations that the number could represent.*/
2+
class Solution {
3+
public:
4+
Solution() {
5+
letters.push_back("");
6+
letters.push_back("");
7+
letters.push_back("abc");
8+
letters.push_back("def");
9+
letters.push_back("ghi");
10+
letters.push_back("jkl");
11+
letters.push_back("mno");
12+
letters.push_back("pqrs");
13+
letters.push_back("tuv");
14+
letters.push_back("wxyz");
15+
}
16+
vector<string> letterCombinations(string digits) {
17+
vector<string> res;
18+
if (digits.size() == 0) {
19+
return res;
20+
}
21+
string s = "";
22+
dfs(s, digits, res);
23+
return res;
24+
}
25+
private:
26+
vector<string> letters;
27+
28+
void dfs(string &s, string &digits, vector<string> &res) {
29+
int idx = s.size();
30+
if (idx == digits.size()) {
31+
res.push_back(s);
32+
return;
33+
}
34+
35+
int i;
36+
int d = digits[idx] - '0';
37+
for (i = 0; i < letters[d].size(); ++i) {
38+
s.push_back(letters[d][i]);
39+
dfs(s, digits, res);
40+
s.pop_back();
41+
}
42+
}
43+
};

Leetcode/18. 4Sum.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*4Sum:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
2+
Find all unique quadruplets in the array which gives the sum of target.*/
3+
class Solution {
4+
public:
5+
vector<vector<int>> fourSum(vector<int>& nums, int target) {
6+
vector<vector<int>> result;
7+
int n = nums.size();
8+
if(n < 4) return result;
9+
sort(nums.begin(), nums.end());
10+
for(int i = 0; i < n-3; ++i){
11+
if (i > 0 && nums[i] == nums[i - 1])
12+
continue;
13+
vector<int> n(nums.begin() + i + 1, nums.end());
14+
vector<vector<int>> ret = threeSum(n, target - nums[i]); //基于3sum
15+
for (auto v : ret)
16+
{
17+
v.insert(v.begin(), nums[i]);
18+
result.push_back(v);
19+
}
20+
}
21+
return result;
22+
}
23+
vector<vector<int>> threeSum(vector<int> &num, int target) {
24+
vector<vector<int>> result;
25+
auto n = num.size();
26+
size_t i;
27+
for (i = 0; i < n; ++i) {
28+
if (i > 0 && num[i] == num[i - 1])
29+
continue;
30+
int a = num[i];
31+
int low = i + 1, high = n - 1;
32+
while (low < high) {
33+
int b = num[low];
34+
int c = num[high];
35+
if (a + b + c == target) {
36+
vector<int> v = {a, b, c};
37+
result.push_back(v);
38+
while (low + 1 < n && num[low] == num[low + 1]) low++;
39+
while (high - 1 >= 0 && num[high] == num[high - 1]) high--;
40+
low++;
41+
high--;
42+
} else if (a + b + c > target) {
43+
while (high - 1 >= 0 && num[high] == num[high - 1]) high--;
44+
high--;
45+
} else {
46+
while (low + 1 < n && num[low] == num[low + 1]) low++;
47+
low++;
48+
}
49+
}
50+
}
51+
return result;
52+
}
53+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*Remove Nth Node From End of List:Given a linked list, remove the nth node from the end of list and return its head.*/
2+
/**
3+
* Definition for singly-linked list.
4+
* struct ListNode {
5+
* int val;
6+
* ListNode *next;
7+
* ListNode(int x) : val(x), next(NULL) {}
8+
* };
9+
*/
10+
//要求只遍历一趟,需要两个指针p1、p2,p1先走n步,然后p1、p2一起走,直到p1到达链表结尾,此时p2就是需要删除的节点.
11+
class Solution {
12+
public:
13+
ListNode* removeNthFromEnd(ListNode* head, int n) {
14+
int cnt = n + 1;
15+
ListNode* p1 = head;
16+
while (p1 != NULL && cnt > 0) {
17+
p1 = p1->next;
18+
--cnt;
19+
}
20+
ListNode *tmp;
21+
if (cnt > 0) {
22+
tmp = head;
23+
head = head->next;
24+
delete tmp;
25+
return head;
26+
}
27+
ListNode *p2 = head;
28+
while (p1 != NULL) {
29+
p1 = p1->next;
30+
p2 = p2->next;
31+
}
32+
tmp = p2->next;
33+
p2->next = tmp->next;
34+
delete tmp;
35+
return head;
36+
}
37+
};

0 commit comments

Comments
 (0)