Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added LeetCode problem 1,2 #2450

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions leetcode/DIRECTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

nageshnnazare marked this conversation as resolved.
Show resolved Hide resolved
# LeetCode

### LeetCode Algorithm

| # | Title| Solution | Difficulty |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------- | ---------- |
| 1 | [Two Sum](https://leetcode.com/problems/two-sum)| [C++](./src/1.cpp) | Easy |
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers)| [C++](./src/2.cpp) | Medium |
69 changes: 69 additions & 0 deletions leetcode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 📚 Contributing 📚

We're glad you're interested in adding C++ LeetCode solutions to the repository.\
Here we'll be explaining how to contribute to LeetCode solutions properly.

## 💻 Cloning/setting up the project 💻

First off, you'll need to fork the repository [**here**](https://github.com/TheAlgorithms/C-Plus-Plus/fork).\
Then, you'll need to clone the repository to your local machine.

```bash
git clone https://github.com/your-username/C-Plus-Plus.git
```

After that, you'll need to create a new branch for your solution.

```bash
git checkout -b solution/your-solution-name
```

## 📝 Adding a new solution 📝

All LeetCode problems can be found [**here**](https://leetcode.com/problemset/all/).\
If you have a solution to any of these problems (which are not being **repeated**, that's great! Here are the steps:

1. Add a new file in `leetcode/src` with the number of the problem.
- For example: if the problem's number is 98, the filename should be `98.cpp`.
2. Provide a small description of the solution at the top of the file. A function should go below that. For example:

```c
/**
* Return an vector of vector of size returnSize.
* The sizes of the vectors are returned as returnColumnSizes vector.
* Note: Both returned vector and columnSizes vector must be dynamically allocated, assume caller calls free.
*/
```

3. Do not provide a `main` function. Use the required standalone functions instead.
4. Doxygen documentation isn't used in LeetCode solutions. Simple/small documentation or comments should be fine.
5. Don't include libraries/headers such as `iostream`. Your file should be the solution to the problem only.
realstealthninja marked this conversation as resolved.
Show resolved Hide resolved

> **Note**
> 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.

## 📦 Committing your changes 📦

Once you're done with adding a new LeetCode solution, it's time we make a pull request.

1. First, stage your changes.

```bash
git add leetcode/src/98.cpp # Use `git add .` to stage all changes.
```

2. Then, commit your changes.

```bash
git commit -m "feat: add LeetCode problem 98" -m "Commit description" # Optional
```

3. Finally, push your changes to your forked repository.

```bash
git push origin solution/your-solution-name:solution/your-solution-name
```

4. You're done now! You just have to make a [**pull request**](https://github.com/TheAlgorithms/C-Plus-Plus/compare). 🎉

If you need any help, don't hesitate to ask and join our [**Discord server**](https://the-algorithms.com/discord)! 🙂
24 changes: 24 additions & 0 deletions leetcode/src/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
*/

class Solution {
public:
std::vector<int> twoSum(std::vector<int>& nums, int target) {
std::unordered_map<int, int> mp; // value:index
std::vector<int> res;

for (int i = 0; i < nums.size(); i++) {
int diff = target - nums[i];

if (mp.find(diff) != mp.end()) {
res.push_back(mp[diff]);
res.push_back(i);
return res;
}
mp[nums[i]] = i;
}
return res;
}
};
37 changes: 37 additions & 0 deletions leetcode/src/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* You are given two non - empty linked lists representing two non - negative integers.
* The digits are stored in reverse order, and each of their nodes contains a single digit.
* Add the two numbersand return the sum as a linked list.
*/

/*
* Definition for singly-linked list.
* 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 Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0;
ListNode head;
ListNode* sum = &head;

while (l1 || l2) {
int val = carry + (l1 ? l1->val : 0) + (l2 ? l2->val : 0);
carry = val / 10;
sum->next = new ListNode(val % 10);
sum = sum->next;
l1 = (l1 ? l1->next : nullptr);
l2 = (l2 ? l2->next : nullptr);
}
if (carry) {
sum->next = new ListNode(carry);
}

return head.next;
}
};