Skip to content

Commit 6343f10

Browse files
committed
2 sum
1 parent 00114fb commit 6343f10

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

Diff for: Striver Sheet/Day-7/3Sum.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
3Sum
3+
====
4+
5+
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
6+
7+
Notice that the solution set must not contain duplicate triplets.
8+
9+
Example 1:
10+
Input: nums = [-1,0,1,2,-1,-4]
11+
Output: [[-1,-1,2],[-1,0,1]]
12+
13+
Example 2:
14+
Input: nums = []
15+
Output: []
16+
17+
Example 3:
18+
Input: nums = [0]
19+
Output: []
20+
21+
Constraints:
22+
0 <= nums.length <= 3000
23+
-105 <= nums[i] <= 105
24+
*/
25+
26+
class Solution
27+
{
28+
public:
29+
vector<vector<int>> threeSum(vector<int> &nums)
30+
{
31+
int n = nums.size();
32+
sort(nums.begin(), nums.end());
33+
vector<vector<int>> ans;
34+
35+
int i = 0;
36+
37+
while (i < n - 2)
38+
{
39+
int j = i + 1;
40+
int k = n - 1;
41+
42+
while (j < k)
43+
{
44+
if (nums[j] + nums[k] == -nums[i])
45+
{
46+
ans.push_back({nums[i], nums[j], nums[k]});
47+
while (j < k && nums[j] == nums[j + 1])
48+
j++;
49+
while (j < k && nums[k] == nums[k - 1])
50+
k--;
51+
j++;
52+
k--;
53+
}
54+
else if (nums[j] + nums[k] < -nums[i])
55+
{
56+
while (j < k && nums[j] == nums[j + 1])
57+
j++;
58+
j++;
59+
}
60+
else
61+
{
62+
while (j < k && nums[k] == nums[k - 1])
63+
k--;
64+
k--;
65+
}
66+
}
67+
68+
while (i < n - 1 && nums[i] == nums[i + 1])
69+
i++;
70+
i++;
71+
}
72+
73+
return ans;
74+
}
75+
};
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copy List with Random Pointer
3+
=============================
4+
5+
A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
6+
7+
Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
8+
9+
For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.
10+
11+
Return the head of the copied linked list.
12+
13+
The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
14+
15+
val: an integer representing Node.val
16+
random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.
17+
Your code will only be given the head of the original linked list.
18+
19+
Example 1:
20+
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
21+
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
22+
23+
Example 2:
24+
Input: head = [[1,1],[2,1]]
25+
Output: [[1,1],[2,1]]
26+
27+
Example 3:
28+
Input: head = [[3,null],[3,0],[3,null]]
29+
Output: [[3,null],[3,0],[3,null]]
30+
31+
Example 4:
32+
Input: head = []
33+
Output: []
34+
Explanation: The given linked list is empty (null pointer), so return null.
35+
36+
Constraints:
37+
0 <= n <= 1000
38+
-10000 <= Node.val <= 10000
39+
Node.random is null or is pointing to some node in the linked list.
40+
*/
41+

Diff for: Striver Sheet/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757
- [Flattening a Linked List](https://practice.geeksforgeeks.org/problems/flattening-a-linked-list/1#) - [Cpp Soultion](./Day-6/Flattening%20a%20Linked%20List.cpp)
5858
- [Rotate List](https://leetcode.com/problems/rotate-list/) - [Cpp Soultion](./Day-6/Rotate%20List.cpp)
5959

60+
### Day 7 (2 Pointers)
61+
62+
- [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) - [Cpp Soultion](./Day-7/Copy%20List%20ith%20Random%20Pointer.cpp)
63+
- [3Sum](https://leetcode.com/problems/3sum/) - [Cpp Soultion](./Day-7/3Sum.cpp)
64+
- []() - [Cpp Soultion](./Day-7/.cpp)
65+
- []() - [Cpp Soultion](./Day-7/.cpp)
66+
- []() - [Cpp Soultion](./Day-7/.cpp)
67+
6068
###
6169

6270
- []() - [Cpp Soultion](./Day-/.cpp)

0 commit comments

Comments
 (0)