Added accepted solutions for leetcode number 1 and 2 problems
This commit is contained in:
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
*.dSYM
|
||||
|
||||
main.dSYM/
|
||||
.*/
|
||||
47
1-two-sum.cpp
Normal file
47
1-two-sum.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
struct Pair {
|
||||
int first;
|
||||
int second;
|
||||
|
||||
Pair(int f, int s) : first(f), second(s) {}
|
||||
Pair() : first(0), second(0) {}
|
||||
bool operator>(const Pair& other) const {
|
||||
return first > other.first;
|
||||
}
|
||||
bool operator<(const Pair& other) const {
|
||||
return first < other.first;
|
||||
}
|
||||
};
|
||||
|
||||
class TwoSumSolution {
|
||||
public:
|
||||
std::vector<int> twoSum(std::vector<int>& nums, int target) {
|
||||
std::vector<Pair> numPairs;
|
||||
int i = 0;
|
||||
for (auto num : nums) {
|
||||
numPairs.emplace_back(num, i);
|
||||
i++;
|
||||
}
|
||||
std::sort(numPairs.begin(), numPairs.end());
|
||||
while (true)
|
||||
{
|
||||
int left = 0;
|
||||
int right = static_cast<int>(numPairs.size()) - 1;
|
||||
while (left < right) {
|
||||
int sum = numPairs[left].first + numPairs[right].first;
|
||||
if (sum == target) {
|
||||
return std::vector<int>{numPairs[left].second, numPairs[right].second};
|
||||
} else if (sum < target) {
|
||||
left++;
|
||||
} else {
|
||||
right--;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return std::vector<int>{};
|
||||
}
|
||||
};
|
||||
43
2-add-two-numbers.cpp
Normal file
43
2-add-two-numbers.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode() : val(0), next(nullptr) {}
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
class AddTwoNumbersSolution {
|
||||
public:
|
||||
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
|
||||
|
||||
if (!l1 && !l2) return nullptr;
|
||||
|
||||
ListNode* res = new ListNode(0);
|
||||
ListNode* start = res;
|
||||
ListNode* prev(nullptr);
|
||||
|
||||
int carry = 0;
|
||||
while (l1 || l2) {
|
||||
res->val += (l1? l1->val : 0);
|
||||
res->val += (l2? l2->val : 0);
|
||||
res->val += carry;
|
||||
carry = res->val / 10;
|
||||
if (carry) res->val -= 10;
|
||||
|
||||
res->next = new ListNode(0);
|
||||
prev = res;
|
||||
res = res->next;
|
||||
if (l1) l1 = l1->next;
|
||||
if (l2) l2 = l2->next;
|
||||
}
|
||||
if ((carry == 0) && prev) {
|
||||
delete prev->next;
|
||||
prev->next = nullptr;
|
||||
}
|
||||
if (carry > 0) {
|
||||
res->val++;
|
||||
}
|
||||
return start;
|
||||
}
|
||||
};
|
||||
56
main.cpp
Normal file
56
main.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "1-two-sum.cpp"
|
||||
#include "2-add-two-numbers.cpp"
|
||||
|
||||
int main() {
|
||||
TwoSumSolution twoSumSolution;
|
||||
auto v = std::vector<int>{11, 2, 8, 0, 35, 7, 15};
|
||||
auto res = twoSumSolution.twoSum(v, 9);
|
||||
std::cout << "Result: ";
|
||||
for (auto index : res) {
|
||||
std::cout << index << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
AddTwoNumbersSolution addTwoNumbersSolution;
|
||||
ListNode *l1 = new ListNode(2);
|
||||
l1->next = new ListNode(4);
|
||||
l1->next->next = new ListNode(3);
|
||||
|
||||
ListNode *l2 = new ListNode(5);
|
||||
l2->next = new ListNode(6);
|
||||
l2->next->next = new ListNode(4);
|
||||
auto resList = addTwoNumbersSolution.addTwoNumbers(l1, l2);
|
||||
std::cout << "addTwoNumbers: ";
|
||||
ListNode *listPtr = resList;
|
||||
while (listPtr) {
|
||||
std::cout << listPtr->val << " ";
|
||||
listPtr = listPtr->next;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
ListNode *l3 = new ListNode(9);
|
||||
l3->next = new ListNode(9);
|
||||
l3->next->next = new ListNode(9);
|
||||
l3->next->next->next = new ListNode(9);
|
||||
l3->next->next->next->next = new ListNode(9);
|
||||
l3->next->next->next->next->next = new ListNode(9);
|
||||
l3->next->next->next->next->next->next = new ListNode(9);
|
||||
|
||||
ListNode *l4 = new ListNode(9);
|
||||
l4->next = new ListNode(9);
|
||||
l4->next->next = new ListNode(9);
|
||||
l4->next->next->next = new ListNode(9);
|
||||
|
||||
resList = addTwoNumbersSolution.addTwoNumbers(l3, l4);
|
||||
std::cout << "addTwoNumbers: ";
|
||||
listPtr = resList;
|
||||
while (listPtr) {
|
||||
std::cout << listPtr->val << " ";
|
||||
listPtr = listPtr->next;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user