Skip to content

Commit 3e7f340

Browse files
authored
Improved cpp readmes
1 parent 9b40015 commit 3e7f340

File tree

58 files changed

+1863
-2630
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1863
-2630
lines changed

src/main/cpp/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md

+41-62
Original file line numberDiff line numberDiff line change
@@ -52,70 +52,49 @@ The overall run time complexity should be `O(log (m+n))`.
5252
* `1 <= m + n <= 2000`
5353
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
5454

55-
To solve the Median of Two Sorted Arrays problem in Java using a `Solution` class, we'll follow these steps:
56-
57-
1. Define a `Solution` class with a method named `findMedianSortedArrays`.
58-
2. Calculate the total length of the combined array (m + n).
59-
3. Determine the middle index or indices of the combined array based on its length (for both even and odd lengths).
60-
4. Implement a binary search algorithm to find the correct position for partitioning the two arrays such that elements to the left are less than or equal to elements on the right.
61-
5. Calculate the median based on the partitioned arrays.
62-
6. Handle edge cases where one or both arrays are empty or where the combined length is odd or even.
63-
7. Return the calculated median.
64-
65-
Here's the implementation:
66-
67-
```java
68-
public class Solution {
69-
70-
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
71-
int m = nums1.length;
72-
int n = nums2.length;
73-
int totalLength = m + n;
74-
int left = (totalLength + 1) / 2;
75-
int right = (totalLength + 2) / 2;
76-
return (findKth(nums1, 0, nums2, 0, left) + findKth(nums1, 0, nums2, 0, right)) / 2.0;
77-
}
78-
79-
private int findKth(int[] nums1, int i, int[] nums2, int j, int k) {
80-
if (i >= nums1.length) return nums2[j + k - 1];
81-
if (j >= nums2.length) return nums1[i + k - 1];
82-
if (k == 1) return Math.min(nums1[i], nums2[j]);
83-
84-
int midVal1 = (i + k / 2 - 1 < nums1.length) ? nums1[i + k / 2 - 1] : Integer.MAX_VALUE;
85-
int midVal2 = (j + k / 2 - 1 < nums2.length) ? nums2[j + k / 2 - 1] : Integer.MAX_VALUE;
86-
87-
if (midVal1 < midVal2) {
88-
return findKth(nums1, i + k / 2, nums2, j, k - k / 2);
89-
} else {
90-
return findKth(nums1, i, nums2, j + k / 2, k - k / 2);
91-
}
92-
}
9355

94-
public static void main(String[] args) {
95-
Solution solution = new Solution();
9656

97-
// Test cases
98-
int[] nums1_1 = {1, 3};
99-
int[] nums2_1 = {2};
100-
System.out.println("Example 1 Output: " + solution.findMedianSortedArrays(nums1_1, nums2_1));
57+
## Solution
10158

102-
int[] nums1_2 = {1, 2};
103-
int[] nums2_2 = {3, 4};
104-
System.out.println("Example 2 Output: " + solution.findMedianSortedArrays(nums1_2, nums2_2));
59+
```cpp
60+
#include <vector>
61+
#include <algorithm>
62+
#include <climits>
10563

106-
int[] nums1_3 = {0, 0};
107-
int[] nums2_3 = {0, 0};
108-
System.out.println("Example 3 Output: " + solution.findMedianSortedArrays(nums1_3, nums2_3));
109-
110-
int[] nums1_4 = {};
111-
int[] nums2_4 = {1};
112-
System.out.println("Example 4 Output: " + solution.findMedianSortedArrays(nums1_4, nums2_4));
113-
114-
int[] nums1_5 = {2};
115-
int[] nums2_5 = {};
116-
System.out.println("Example 5 Output: " + solution.findMedianSortedArrays(nums1_5, nums2_5));
64+
class Solution {
65+
public:
66+
double findMedianSortedArrays(std::vector<int>& nums1, std::vector<int>& nums2) {
67+
if (nums2.size() < nums1.size()) {
68+
return findMedianSortedArrays(nums2, nums1);
69+
}
70+
71+
int n1 = nums1.size();
72+
int n2 = nums2.size();
73+
int low = 0;
74+
int high = n1;
75+
76+
while (low <= high) {
77+
int cut1 = (low + high) / 2;
78+
int cut2 = (n1 + n2 + 1) / 2 - cut1;
79+
80+
int l1 = (cut1 == 0) ? INT_MIN : nums1[cut1 - 1];
81+
int l2 = (cut2 == 0) ? INT_MIN : nums2[cut2 - 1];
82+
int r1 = (cut1 == n1) ? INT_MAX : nums1[cut1];
83+
int r2 = (cut2 == n2) ? INT_MAX : nums2[cut2];
84+
85+
if (l1 <= r2 && l2 <= r1) {
86+
if ((n1 + n2) % 2 == 0) {
87+
return (std::max(l1, l2) + std::min(r1, r2)) / 2.0;
88+
}
89+
return std::max(l1, l2);
90+
} else if (l1 > r2) {
91+
high = cut1 - 1;
92+
} else {
93+
low = cut1 + 1;
94+
}
95+
}
96+
97+
return 0.0;
11798
}
118-
}
119-
```
120-
121-
This implementation provides a solution to the Median of Two Sorted Arrays problem in Java with a runtime complexity of O(log(min(m, n))).
99+
};
100+
```

src/main/cpp/g0001_0100/s0005_longest_palindromic_substring/readme.md

+42-56
Original file line numberDiff line numberDiff line change
@@ -36,65 +36,51 @@ Given a string `s`, return _the longest palindromic substring_ in `s`.
3636
* `1 <= s.length <= 1000`
3737
* `s` consist of only digits and English letters.
3838

39-
To solve the Longest Palindromic Substring problem in Java using a `Solution` class, we'll follow these steps:
40-
41-
1. Define a `Solution` class with a method named `longestPalindrome`.
42-
2. Initialize variables to keep track of the start and end indices of the longest palindromic substring found so far (`start` and `end`).
43-
3. Iterate through the string `s`:
44-
- For each character in the string, expand around it to check if it forms a palindrome.
45-
- Handle both odd-length and even-length palindromes separately.
46-
- Update `start` and `end` indices if a longer palindrome is found.
47-
4. Return the substring from `start` to `end`.
48-
5. Handle edge cases where the input string is empty or has a length of 1.
49-
50-
Here's the implementation:
51-
52-
```java
53-
public class Solution {
54-
55-
public String longestPalindrome(String s) {
56-
if (s == null || s.length() < 1) return "";
57-
int start = 0;
58-
int end = 0;
59-
60-
for (int i = 0; i < s.length(); i++) {
61-
int len1 = expandAroundCenter(s, i, i);
62-
int len2 = expandAroundCenter(s, i, i + 1);
63-
int len = Math.max(len1, len2);
64-
if (len > end - start) {
65-
start = i - (len - 1) / 2;
66-
end = i + len / 2;
67-
}
68-
}
69-
70-
return s.substring(start, end + 1);
71-
}
72-
73-
private int expandAroundCenter(String s, int left, int right) {
74-
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
75-
left--;
76-
right++;
77-
}
78-
return right - left - 1;
79-
}
8039

81-
public static void main(String[] args) {
82-
Solution solution = new Solution();
8340

84-
// Test cases
85-
String s1 = "babad";
86-
System.out.println("Example 1 Output: " + solution.longestPalindrome(s1));
41+
## Solution
8742

88-
String s2 = "cbbd";
89-
System.out.println("Example 2 Output: " + solution.longestPalindrome(s2));
43+
```cpp
44+
#include <vector>
45+
#include <string>
46+
#include <algorithm>
9047

91-
String s3 = "a";
92-
System.out.println("Example 3 Output: " + solution.longestPalindrome(s3));
48+
class Solution {
49+
public:
50+
std::string longestPalindrome(const std::string& s) {
51+
// Transform s into newStr with delimiters
52+
std::string newStr(s.length() * 2 + 1, '#');
53+
for (int i = 0; i < s.length(); ++i) {
54+
newStr[2 * i + 1] = s[i];
55+
}
9356

94-
String s4 = "ac";
95-
System.out.println("Example 4 Output: " + solution.longestPalindrome(s4));
96-
}
97-
}
98-
```
57+
std::vector<int> dp(newStr.length(), 0);
58+
int friendCenter = 0, friendRadius = 0;
59+
int lpsCenter = 0, lpsRadius = 0;
60+
61+
for (int i = 0; i < newStr.length(); ++i) {
62+
dp[i] = (friendCenter + friendRadius > i)
63+
? std::min(dp[2 * friendCenter - i], friendCenter + friendRadius - i)
64+
: 1;
65+
66+
while (i + dp[i] < newStr.length() && i - dp[i] >= 0 && newStr[i + dp[i]] == newStr[i - dp[i]]) {
67+
dp[i]++;
68+
}
69+
70+
if (friendCenter + friendRadius < i + dp[i]) {
71+
friendCenter = i;
72+
friendRadius = dp[i];
73+
}
74+
75+
if (lpsRadius < dp[i]) {
76+
lpsCenter = i;
77+
lpsRadius = dp[i];
78+
}
79+
}
9980

100-
This implementation provides a solution to the Longest Palindromic Substring problem in Java.
81+
int start = (lpsCenter - lpsRadius + 1) / 2;
82+
int length = lpsRadius - 1;
83+
return s.substr(start, length);
84+
}
85+
};
86+
```

src/main/cpp/g0001_0100/s0006_zigzag_conversion/readme.md

+35-61
Original file line numberDiff line numberDiff line change
@@ -41,70 +41,44 @@ string convert(string s, int numRows);
4141
* `s` consists of English letters (lower-case and upper-case), `','` and `'.'`.
4242
* `1 <= numRows <= 1000`
4343

44-
To solve the Zigzag Conversion problem in Java using a `Solution` class, we'll follow these steps:
45-
46-
1. Define a `Solution` class with a method named `convert`.
47-
2. Create an array of strings to represent each row of the zigzag pattern.
48-
3. Initialize variables to keep track of the current row (`row`) and the direction of traversal (`down`).
49-
4. Iterate through each character in the input string `s`.
50-
- Append the current character to the string representing the current row.
51-
- If we reach the first or last row, change the direction of traversal accordingly.
52-
- Update the current row based on the direction of traversal.
53-
5. Concatenate the strings representing each row to form the final zigzag conversion.
54-
6. Return the concatenated string.
55-
7. Handle edge cases where the number of rows is 1 or the input string is empty.
56-
57-
Here's the implementation:
58-
59-
```java
60-
public class Solution {
61-
62-
public String convert(String s, int numRows) {
63-
if (numRows == 1 || s.length() <= numRows) {
64-
return s;
65-
}
6644

67-
StringBuilder[] rows = new StringBuilder[numRows];
68-
for (int i = 0; i < numRows; i++) {
69-
rows[i] = new StringBuilder();
70-
}
7145

72-
int row = 0;
73-
boolean down = false;
46+
## Solution
7447

75-
for (char c : s.toCharArray()) {
76-
rows[row].append(c);
77-
if (row == 0 || row == numRows - 1) {
78-
down = !down;
79-
}
80-
row += down ? 1 : -1;
81-
}
48+
```cpp
49+
#include <string>
50+
#include <vector>
8251

83-
StringBuilder result = new StringBuilder();
84-
for (StringBuilder sb : rows) {
85-
result.append(sb);
52+
class Solution {
53+
public:
54+
std::string convert(std::string s, int numRows) {
55+
int sLen = s.length();
56+
if (numRows == 1) {
57+
return s;
8658
}
87-
88-
return result.toString();
89-
}
90-
91-
public static void main(String[] args) {
92-
Solution solution = new Solution();
93-
94-
// Test cases
95-
String s1 = "PAYPALISHIRING";
96-
int numRows1 = 3;
97-
System.out.println("Example 1 Output: " + solution.convert(s1, numRows1));
98-
99-
String s2 = "PAYPALISHIRING";
100-
int numRows2 = 4;
101-
System.out.println("Example 2 Output: " + solution.convert(s2, numRows2));
102-
103-
String s3 = "A";
104-
int numRows3 = 1;
105-
System.out.println("Example 3 Output: " + solution.convert(s3, numRows3));
59+
int maxDist = numRows * 2 - 2;
60+
std::string buf;
61+
buf.reserve(sLen);
62+
for (int i = 0; i < numRows; i++) {
63+
int index = i;
64+
if (i == 0 || i == numRows - 1) {
65+
while (index < sLen) {
66+
buf += s[index];
67+
index += maxDist;
68+
}
69+
} else {
70+
while (index < sLen) {
71+
buf += s[index];
72+
index += maxDist - i * 2;
73+
if (index >= sLen) {
74+
break;
75+
}
76+
buf += s[index];
77+
index += i * 2;
78+
}
79+
}
80+
}
81+
return buf;
10682
}
107-
}
108-
```
109-
110-
This implementation provides a solution to the Zigzag Conversion problem in Java.
83+
};
84+
```

0 commit comments

Comments
 (0)