Skip to content

Commit db95435

Browse files
authored
Merge pull request ByteByteGoHq#5 from ongshunping/cpp-solutions-two-pointers
Add C++ solutions for Chapter 1 (Two Pointers)
2 parents 5d5c9ed + a82daff commit db95435

10 files changed

+240
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <cctype>
2+
#include <string>
3+
4+
bool isPalindromeValid(std::string s) {
5+
int left = 0;
6+
int right = s.length() - 1;
7+
while (left < right) {
8+
// Skip non-alphanumeric characters from the left.
9+
while (left < right && !std::isalnum(s[left])) {
10+
left++;
11+
}
12+
// Skip non-alphanumeric characters from the right.
13+
while (left < right && !std::isalnum(s[right])) {
14+
right--;
15+
}
16+
// If the characters at the left and right pointers don't
17+
// match, the string is not a palindrome.
18+
if (s[left] != s[right]) {
19+
return false;
20+
}
21+
left++;
22+
right--;
23+
}
24+
return true;
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <algorithm>
2+
#include <vector>
3+
4+
int largestContainer(std::vector<int>& heights) {
5+
int maxWater = 0;
6+
int left = 0;
7+
int right = heights.size() - 1;
8+
while (left < right) {
9+
// Calculate the water contained between the current pair of
10+
// lines.
11+
int water = std::min(heights[left], heights[right]) * (right - left);
12+
maxWater = std::max(maxWater, water);
13+
// Move the pointers inward, always moving the pointer at the
14+
// shorter line. If both lines have the same height, move both
15+
// pointers inward.
16+
if (heights[left] < heights[right]) {
17+
left++;
18+
} else if (heights[left] > heights[right]) {
19+
right--;
20+
} else {
21+
left++;
22+
right--;
23+
}
24+
}
25+
return maxWater;
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <algorithm>
2+
#include <vector>
3+
4+
int largestContainerBruteForce(std::vector<int>& heights) {
5+
int n = heights.size();
6+
int maxWater = 0;
7+
// Find the maximum amount of water stored between all pairs of
8+
// lines.
9+
for (int i = 0; i < n; i++) {
10+
for (int j = i + 1; j < n; j++) {
11+
int water = std::min(heights[i], heights[j]) * (j - i);
12+
maxWater = std::max(maxWater, water);
13+
}
14+
}
15+
return maxWater;
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <algorithm>
2+
#include <string>
3+
4+
std::string nextLexicographicalSequence(std::string s) {
5+
// Locate the pivot, which is the first character from the right that breaks
6+
// non-increasing order. Start searching from the second-to-last position.
7+
int pivot = s.size() - 2;
8+
while (pivot >= 0 && s[pivot] >= s[pivot + 1]) {
9+
pivot --;
10+
}
11+
// If pivot is not found, the string is already in its largest permutation.
12+
// In this case, reverse the string to obtain the smallest permutation.
13+
if (pivot == -1) {
14+
std::reverse(s.begin(), s.end());
15+
return s;
16+
}
17+
// Find the rightmost successor to the pivot.
18+
int rightMostSuccessor = s.size() - 1;
19+
while (s[rightMostSuccessor] <= s[pivot]) {
20+
rightMostSuccessor--;
21+
}
22+
// Swap the rightmost successor with the pivot to increase the lexicographical
23+
// order of the suffix.
24+
std::swap(s[pivot], s[rightMostSuccessor]);
25+
// Reverse the suffix after the pivot to minimize its permutation.
26+
std::reverse(s.begin() + pivot + 1, s.end());
27+
return s;
28+
}

cpp/Two Pointers/pair_sum_sorted.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <vector>
2+
3+
std::vector<int> pairSumSorted(std::vector<int>& nums, int target) {
4+
int left = 0;
5+
int right = nums.size() - 1;
6+
while (left < right) {
7+
int sum = nums[left] + nums[right];
8+
// If the sum is smaller, increment the left pointer, aiming
9+
// to increase the sum toward the target value.
10+
if (sum < target) {
11+
left++;
12+
// If the sum is larger, decrement the right pointer, aiming
13+
// to decrease the sum toward the target value.
14+
} else if (sum > target) {
15+
right--;
16+
// If the target pair is found, return its indexes.
17+
} else {
18+
return {left, right};
19+
}
20+
}
21+
// If no valid pair is found, return an empty vector.
22+
return {};
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <vector>
2+
3+
std::vector<int> pairSumSortedBruteForce(std::vector<int>& nums, int target) {
4+
int n = nums.size();
5+
for (int i = 0; i < n; i++) {
6+
for (int j = i + 1; j < n; j++) {
7+
if (nums[i] + nums[j] == target) {
8+
return {i, j};
9+
}
10+
}
11+
}
12+
return {};
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <algorithm>
2+
#include <vector>
3+
4+
void shiftZerosToTheEnd(std::vector<int>& nums) {
5+
// The 'left' pointer is used to position non-zero elements.
6+
int left = 0;
7+
// Iterate through the array using a 'right' pointer to locate non-zero
8+
// elements.
9+
for (int right = 0; right < nums.size(); right++) {
10+
if (nums[right] != 0) {
11+
std::swap(nums[left], nums[right]);
12+
// Increment 'left' since it now points to a position already occupied
13+
// by a non-zero element.
14+
left++;
15+
}
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <vector>
2+
3+
void shiftZerosToTheEndNaive(std::vector<int>& nums) {
4+
std::vector<int> temp(nums.size(), 0);
5+
int i = 0;
6+
// Add all non-zero elements to the left of 'temp'.
7+
for (int num : nums) {
8+
if (num != 0) {
9+
temp[i] = num;
10+
i++;
11+
}
12+
}
13+
// Set 'nums' to 'temp'.
14+
for (int j = 0; j < nums.size(); j++) {
15+
nums[j] = temp[j];
16+
}
17+
}

cpp/Two Pointers/triplet_sum.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <algorithm>
2+
#include <vector>
3+
4+
// Helper function to find all pairs that sum to a target in a sorted array
5+
std::vector<std::vector<int>> pairSumSortedAllPairs(std::vector<int>& nums, int start, int target);
6+
7+
std::vector<std::vector<int>> tripletSum(std::vector<int>& nums) {
8+
std::vector<std::vector<int>> triplets;
9+
std::sort(nums.begin(), nums.end());
10+
for (int i = 0; i < nums.size(); i++) {
11+
// Optimization: triplets consisting of only positive numbers
12+
// will never sum to 0.
13+
if (nums[i] > 0) {
14+
break;
15+
}
16+
// To avoid duplicate triplets, skip 'a' if it's the same as
17+
// the previous number.
18+
if (i > 0 && nums[i] == nums[i - 1]) {
19+
continue;
20+
}
21+
// Find all pairs that sum to a target of '-a' (-nums[i]).
22+
std::vector<std::vector<int>> pairs = pairSumSortedAllPairs(nums, i + 1, -nums[i]);
23+
for (auto& pair : pairs) {
24+
triplets.push_back({nums[i], pair[0], pair[1]});
25+
}
26+
}
27+
return triplets;
28+
}
29+
30+
std::vector<std::vector<int>> pairSumSortedAllPairs(std::vector<int>& nums, int start, int target) {
31+
std::vector<std::vector<int>> pairs;
32+
int left = start;
33+
int right = nums.size() - 1;
34+
while (left < right) {
35+
int sum = nums[left] + nums[right];
36+
if (sum == target) {
37+
pairs.push_back({nums[left], nums[right]});
38+
left++;
39+
// To avoid duplicate '[b, c]' pairs, skip 'b' if it's the
40+
// same as the previous number.
41+
while (left < right && nums[left] == nums[left - 1]) {
42+
left++;
43+
}
44+
} else if (sum < target) {
45+
left++;
46+
} else {
47+
right--;
48+
}
49+
}
50+
return pairs;
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <algorithm>
2+
#include <set>
3+
#include <vector>
4+
5+
std::vector<std::vector<int>> tripletSumBruteForce(std::vector<int>& nums) {
6+
int n = nums.size();
7+
// Use a hash set to ensure we don't add duplicate triplets.
8+
std::set<std::vector<int>> triplets;
9+
// Iterate through the indexes of all triplets.
10+
for (int i = 0; i < n; i++) {
11+
for (int j = i + 1; j < n; j++) {
12+
for (int k = j + 1; k < n; k++) {
13+
if (nums[i] + nums[j] + nums[k] == 0) {
14+
// Sort the triplet before including it in the
15+
// hash set.
16+
std::vector<int> triplet = {nums[i], nums[j], nums[k]};
17+
std::sort(triplet.begin(), triplet.end());
18+
triplets.insert(triplet);
19+
}
20+
}
21+
}
22+
}
23+
return {triplets.begin(), triplets.end()};
24+
}

0 commit comments

Comments
 (0)