Skip to content

Commit c1ceca1

Browse files
authored
Merge pull request #336 from CodeHarborHub/dev-3
Update contets of Add Two Numbers Solution and others
2 parents 3c42e48 + 2a676d8 commit c1ceca1

File tree

2 files changed

+332
-127
lines changed

2 files changed

+332
-127
lines changed

dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: two-sum
3-
title: Two Sum Problem (LeetCode)
3+
title: Two Sum Solution
44
sidebar_label: 0001 - Two Sum
55
tags:
66
- Two Sum
@@ -13,11 +13,7 @@ tags:
1313
description: "This is a solution to the Two Sum problem on LeetCode."
1414
---
1515

16-
## Problem Description
17-
18-
| Problem Statement | Solution Link | LeetCode Profile |
19-
| :------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------- |
20-
| [Two Sum on LeetCode](https://leetcode.com/problems/two-sum/) | [Two Sum Solution on LeetCode](https://leetcode.com/problems/two-sum/solutions/4958021/two-sum-problem-solution-using-hash-table-ts-js-java-py-cpp-recommended-solutions) | [Ajay Dhangar](https://leetcode.com/ajaydhangar49/) |
16+
In this page, we will solve the Two Sum problem using three different approaches: brute force, hash table, and two-pointer technique. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, C++, and more.
2117

2218
## Problem Description
2319

@@ -29,21 +25,21 @@ You can return the answer in any order.
2925

3026
### Examples
3127

32-
**`Example 1:`**
28+
**Example 1:**
3329

3430
```plaintext
3531
Input: nums = [2,7,11,15], target = 9
3632
Output: [0,1]
3733
```
3834

39-
**`Example 2:`**
35+
**Example 2:**
4036

4137
```plaintext
4238
Input: nums = [3,2,4], target = 6
4339
Output: [1,2]
4440
```
4541

46-
**`Example 3:`**
42+
**Example 3:**
4743

4844
```plaintext
4945
Input: nums = [3,3], target = 6
@@ -69,7 +65,8 @@ The problem can be solved using a brute force approach, a hash table, or the two
6965

7066
<Tabs>
7167
<tabItem value="Brute Force" label="Brute Force">
72-
### Approach 1: Brute Force (Naive)
68+
69+
### Approach 1: Brute Force (Naive)
7370

7471
The brute force approach is simple. We iterate through each element `nums[i]` and check if there is another element `nums[j]` such that `nums[i] + nums[j] == target`. If we find such a pair, we return the indices `[i, j]`.
7572

@@ -109,6 +106,7 @@ function twoSumProblem() {
109106

110107
<Tabs>
111108
<TabItem value="JavaScript" label="JavaScript" default>
109+
<SolutionAuthor name="@ajay-dhangar"/>
112110
```javascript
113111
function twoSum(nums, target) {
114112
for (let i = 0; i < nums.length; i++) {
@@ -124,7 +122,8 @@ function twoSumProblem() {
124122
```
125123

126124
</TabItem>
127-
<TabItem value="TypeScript" label="TypeScript">
125+
<TabItem value="TypeScript" label="TypeScript">
126+
<SolutionAuthor name="@ajay-dhangar"/>
128127
```typescript
129128
function twoSum(nums: number[], target: number): number[] {
130129
for (let i = 0; i < nums.length; i++) {
@@ -140,7 +139,8 @@ function twoSumProblem() {
140139
```
141140

142141
</TabItem>
143-
<TabItem value="Python" label="Python">
142+
<TabItem value="Python" label="Python">
143+
<SolutionAuthor name="@ajay-dhangar"/>
144144
```python
145145
class Solution:
146146
def twoSum(self, nums: List[int], target: int) -> List[int]:
@@ -153,7 +153,8 @@ function twoSumProblem() {
153153
```
154154

155155
</TabItem>
156-
<TabItem value="Java" label="Java">
156+
<TabItem value="Java" label="Java">
157+
<SolutionAuthor name="@ajay-dhangar"/>
157158
```java
158159
class Solution {
159160
public int[] twoSum(int[] nums, int target) {
@@ -171,7 +172,8 @@ function twoSumProblem() {
171172
```
172173

173174
</TabItem>
174-
<TabItem value="C++" label="C++">
175+
<TabItem value="C++" label="C++">
176+
<SolutionAuthor name="@ajay-dhangar"/>
175177
```cpp
176178
class Solution {
177179
public:
@@ -247,6 +249,7 @@ function twoSumProblem() {
247249

248250
<Tabs>
249251
<TabItem value="JavaScript" label="JavaScript" default>
252+
<SolutionAuthor name="@ajay-dhangar"/>
250253
```javascript
251254
function twoSum(nums, target) {
252255
const numMap = new Map();
@@ -264,7 +267,8 @@ function twoSumProblem() {
264267
```
265268

266269
</TabItem>
267-
<TabItem value="TypeScript" label="TypeScript">
270+
<TabItem value="TypeScript" label="TypeScript">
271+
<SolutionAuthor name="@ajay-dhangar"/>
268272
```typescript
269273
function twoSum(nums: number[], target: number): number[] {
270274
const numMap = new Map<number, number>();
@@ -282,7 +286,8 @@ function twoSumProblem() {
282286
```
283287

284288
</TabItem>
285-
<TabItem value="Python" label="Python">
289+
<TabItem value="Python" label="Python">
290+
<SolutionAuthor name="@ajay-dhangar"/>
286291
```python
287292
class Solution:
288293
def twoSum(self, nums: List[int], target: int) -> List[int]:
@@ -297,7 +302,8 @@ function twoSumProblem() {
297302
```
298303

299304
</TabItem>
300-
<TabItem value="Java" label="Java">
305+
<TabItem value="Java" label="Java">
306+
<SolutionAuthor name="@ajay-dhangar"/>
301307
```java
302308
class Solution {
303309
public int[] twoSum(int[] nums, int target) {
@@ -317,7 +323,8 @@ function twoSumProblem() {
317323
```
318324

319325
</TabItem>
320-
<TabItem value="C++" label="C++">
326+
<TabItem value="C++" label="C++">
327+
<SolutionAuthor name="@ajay-dhangar"/>
321328
```cpp
322329
class Solution {
323330
public:
@@ -405,6 +412,7 @@ function twoSumProblem() {
405412

406413
<Tabs>
407414
<TabItem value="JavaScript" label="JavaScript" default>
415+
<SolutionAuthor name="@ajay-dhangar"/>
408416
```javascript
409417
function twoSum(nums, target) {
410418
const sortedNums = nums.map((num, index) => [num, index]);
@@ -429,8 +437,10 @@ function twoSumProblem() {
429437
```
430438

431439
</TabItem>
432-
<TabItem value="TypeScript" label="TypeScript">
433-
```typescript
440+
<TabItem value="TypeScript" label="TypeScript">
441+
<SolutionAuthor name="@ajay-dhangar"/>
442+
443+
```ts
434444
function twoSum(nums: number[], target: number): number[] {
435445
const sortedNums = nums.map((num, index) => [num, index]);
436446
sortedNums.sort((a, b) => a[0] - b[0]);
@@ -454,8 +464,9 @@ function twoSumProblem() {
454464
```
455465

456466
</TabItem>
457-
<TabItem value="Python" label="Python">
458-
```python
467+
<TabItem value="Python" label="Python">
468+
<SolutionAuthor name="@ajay-dhangar"/>
469+
```py
459470
class Solution:
460471
def twoSum(self, nums: List[int], target: int) -> List[int]:
461472
sorted_nums = sorted(enumerate(nums), key=lambda x: x[1])
@@ -475,7 +486,8 @@ function twoSumProblem() {
475486
```
476487

477488
</TabItem>
478-
<TabItem value="Java" label="Java">
489+
<TabItem value="Java" label="Java">
490+
<SolutionAuthor name="@ajay-dhangar"/>
479491
```java
480492
class Solution {
481493
public int[] twoSum(int[] nums, int target) {
@@ -506,7 +518,9 @@ function twoSumProblem() {
506518
```
507519

508520
</TabItem>
509-
<TabItem value="C++" label="C++">
521+
<TabItem value="C++" label="C++">
522+
<SolutionAuthor name="@ajay-dhangar"/>
523+
510524
```cpp
511525
class Solution {
512526
public:
@@ -562,6 +576,8 @@ The hash table approach is the most efficient and is recommended for large input
562576

563577
:::
564578

565-
## Conclusion
579+
## References
566580

567-
In this tutorial, we learned how to solve the Two Sum problem on LeetCode using different approaches. We discussed the brute force approach, the hash table approach, and the two-pointer approach. We implemented the solutions in JavaScript, TypeScript, Python, Java, and C++. We also analyzed the time and space complexity of each approach and compared them to determine the best approach for this problem. The hash table approach is the most efficient and is recommended for large inputs.
581+
- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/two-sum/)
582+
- **Solution Link:** [Two Sum Solution on LeetCode](https://leetcode.com/problems/two-sum/solutions/4958021/two-sum-problem-solution-using-hash-table-ts-js-java-py-cpp-recommended-solutions)
583+
- **Authors LeetCode Profile:** [Ajay Dhangar](https://leetcode.com/ajaydhangar49/)

0 commit comments

Comments
 (0)