Skip to content

Commit c4e557f

Browse files
committed
feat: add LeetCode problem 1,2
Created new directory for leetcode solutions
1 parent 65efd47 commit c4e557f

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

leetcode/DIRECTORY.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
# LeetCode
3+
4+
### LeetCode Algorithm
5+
6+
| # | Title| Solution | Difficulty |
7+
| ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------- | ---------- |
8+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum)| [C++](./src/1.cpp) | Easy |
9+
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers)| [C++](./src/2.cpp) | Medium |

leetcode/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 📚 Contributing 📚
2+
3+
We're glad you're interested in adding C++ LeetCode solutions to the repository.\
4+
Here we'll be explaining how to contribute to LeetCode solutions properly.
5+
6+
## 💻 Cloning/setting up the project 💻
7+
8+
First off, you'll need to fork the repository [**here**](https://github.com/TheAlgorithms/C-Plus-Plus/fork).\
9+
Then, you'll need to clone the repository to your local machine.
10+
11+
```bash
12+
git clone https://github.com/your-username/C-Plus-Plus.git
13+
```
14+
15+
After that, you'll need to create a new branch for your solution.
16+
17+
```bash
18+
git checkout -b solution/your-solution-name
19+
```
20+
21+
## 📝 Adding a new solution 📝
22+
23+
All LeetCode problems can be found [**here**](https://leetcode.com/problemset/all/).\
24+
If you have a solution to any of these problems (which are not being **repeated**, that's great! Here are the steps:
25+
26+
1. Add a new file in `leetcode/src` with the number of the problem.
27+
- For example: if the problem's number is 98, the filename should be `98.cpp`.
28+
2. Provide a small description of the solution at the top of the file. A function should go below that. For example:
29+
30+
```c
31+
/**
32+
* Return an vector of vector of size returnSize.
33+
* The sizes of the vectors are returned as returnColumnSizes vector.
34+
* Note: Both returned vector and columnSizes vector must be dynamically allocated, assume caller calls free.
35+
*/
36+
```
37+
38+
3. Do not provide a `main` function. Use the required standalone functions instead.
39+
4. Doxygen documentation isn't used in LeetCode solutions. Simple/small documentation or comments should be fine.
40+
5. Don't include libraries/headers such as `iostream`. Your file should be the solution to the problem only.
41+
42+
> **Note**
43+
> There was a requirement to update the `leetcode/DIRECTORY.md` file with details of the solved problem. It's not required anymore. The information about the problem is fetched automatically throughout the LeetCode API.
44+
45+
## 📦 Committing your changes 📦
46+
47+
Once you're done with adding a new LeetCode solution, it's time we make a pull request.
48+
49+
1. First, stage your changes.
50+
51+
```bash
52+
git add leetcode/src/98.cpp # Use `git add .` to stage all changes.
53+
```
54+
55+
2. Then, commit your changes.
56+
57+
```bash
58+
git commit -m "feat: add LeetCode problem 98" -m "Commit description" # Optional
59+
```
60+
61+
3. Finally, push your changes to your forked repository.
62+
63+
```bash
64+
git push origin solution/your-solution-name:solution/your-solution-name
65+
```
66+
67+
4. You're done now! You just have to make a [**pull request**](https://github.com/TheAlgorithms/C-Plus-Plus/compare). 🎉
68+
69+
If you need any help, don't hesitate to ask and join our [**Discord server**](https://the-algorithms.com/discord)! 🙂

leetcode/src/1.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
3+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
4+
*/
5+
6+
class Solution {
7+
public:
8+
std::vector<int> twoSum(std::vector<int>& nums, int target) {
9+
std::unordered_map<int, int> mp; // value:index
10+
std::vector<int> res;
11+
12+
for (int i = 0; i < nums.size(); i++) {
13+
int diff = target - nums[i];
14+
15+
if (mp.find(diff) != mp.end()) {
16+
res.push_back(mp[diff]);
17+
res.push_back(i);
18+
return res;
19+
}
20+
mp[nums[i]] = i;
21+
}
22+
return res;
23+
}
24+
};

leetcode/src/2.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* You are given two non - empty linked lists representing two non - negative integers.
2+
* The digits are stored in reverse order, and each of their nodes contains a single digit.
3+
* Add the two numbersand return the sum as a linked list.
4+
*/
5+
6+
/*
7+
* Definition for singly-linked list.
8+
* struct ListNode {
9+
* int val;
10+
* ListNode *next;
11+
* ListNode() : val(0), next(nullptr) {}
12+
* ListNode(int x) : val(x), next(nullptr) {}
13+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
14+
* };
15+
*/
16+
class Solution {
17+
public:
18+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
19+
int carry = 0;
20+
ListNode head;
21+
ListNode* sum = &head;
22+
23+
while (l1 || l2) {
24+
int val = carry + (l1 ? l1->val : 0) + (l2 ? l2->val : 0);
25+
carry = val / 10;
26+
sum->next = new ListNode(val % 10);
27+
sum = sum->next;
28+
l1 = (l1 ? l1->next : nullptr);
29+
l2 = (l2 ? l2->next : nullptr);
30+
}
31+
if (carry) {
32+
sum->next = new ListNode(carry);
33+
}
34+
35+
return head.next;
36+
}
37+
};

0 commit comments

Comments
 (0)