1. 兩數之和
給定一個整數數組 nums 和一個目標值 target,請你在該數組中找出和爲目標值的那 兩個 整數,並返回他們的數組下標。
你可以假設每種輸入只會對應一個答案。但是,數組中同一個元素不能使用兩遍。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int>mp;
for(int i = 0; i < nums.size(); i++){
if(mp.find(nums[i]) != mp.end()){
return {mp[nums[i]], i};
}
mp[target-nums[i]] = i;
}
return{0,0};
}
};
15. 三數之和
給你一個包含 n 個整數的數組 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?請你找出所有滿足條件且不重複的三元組。
注意:答案中不可以包含重複的三元組。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i++){
if(nums[i] > 0) break;
if(i > 0 && nums[i] == nums[i-1])continue;
int L = i + 1;
int R = nums.size() - 1;
while(L < R){
int sum = nums[i] + nums[L] + nums[R];
if(sum == 0){
vector<int> level;
level.push_back(nums[i]);
level.push_back(nums[L]);
level.push_back(nums[R]);
res.push_back(level);
while(L < R &&nums[L] == nums[L+1]) ++L;
while(L < R &&nums[R] == nums[R-1]) --R;
L++;
R--;
}else if(sum > 0) R--;
else L++;
}
}
return res;
}
};
18. 四數之和
給定一個包含 n 個整數的數組 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。
注意:
答案中不可以包含重複的四元組
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
if(nums.size() < 4) return res;
int len = nums.size();
sort(nums.begin(), nums.end());
for(int i = 0; i < len -3; i++){
if(i > 0 && nums[i] == nums[i-1]) continue;
for(int j = i+1; j < len - 2; j++){
if(j > i+1 && nums[j] == nums[j-1])continue;
int L = j+1, R =nums.size()-1;
while(L < R){
int sum = nums[i] + nums[j] + nums[L] + nums[R];
if(sum == target){
res.push_back({nums[i], nums[j], nums[L], nums[R]});
while(L < R&&nums[L] == nums[L+1]) L++;
while(L < R&&nums[R-1]== nums[R]) R--;
L++;
R--;
}else if(sum > target) R--;
else L++;
}
}
}
return res;
}
};
445. 兩數相加 II
給你兩個 非空 鏈表來代表兩個非負整數。數字最高位位於鏈表開始位置。它們的每個節點只存儲一位數字。將這兩數相加會返回一個新的鏈表。
你可以假設除了數字 0 之外,這兩個數字都不會以零開頭
/**
* 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) {
stack<int>s1;
stack<int>s2;
while(l1){
s1.push(l1->val);
l1=l1->next;
}
while(l2){
s2.push(l2->val);
l2 = l2->next;
}
ListNode * head = NULL;
int flag = 0;
while(!s1.empty() ||!s2.empty() || flag!= 0){
int sum = 0;
if(!s1.empty()){
sum += s1.top();
s1.pop();
}
if(!s2.empty()){
sum += s2.top();
s2.pop();
}
sum += flag;
ListNode * node = new ListNode(sum%10);
flag = sum/10;
node->next = head;
head = node;
}
return head;
}
};
454. 四數相加 II
給定四個包含整數的數組列表 A , B , C , D ,計算有多少個元組 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
爲了使問題簡單化,所有的 A, B, C, D 具有相同的長度 N,且 0 ≤ N ≤ 500 。所有整數的範圍在 -228 到 228 - 1 之間,最終結果不會超過 231 - 1
輸入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
輸出:
2
解釋:
兩個元組如下:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int>mp;
for(int i = 0; i < A.size(); i++)
{
for(int j = 0; j < B.size(); j++){
int sumAB = A[i] + B[j];
if(mp.find(sumAB)!= mp.end()) mp[sumAB]++;
else mp[sumAB] = 1;
}
}
int res = 0;
for(int i = 0; i < C.size(); i++)
{
for(int j = 0; j < D.size(); j++){
int sumCD = -(C[i] + D[i]);
if(mp.find(sumCD)!= mp.end()) res += mp[sumCD];
}
}
return res;
}
};
```cpp