Task01
Question number 2: Add two numbers
Question number: 2
Difficulty: Medium
https://leetcode-cn.com/problems/add-two-numbers/ 3
Two non-empty linked lists are given to represent two non-negative integers. Among them, their respective digits are stored in reverse order, and each node can only store one digit.
If we add these two numbers together, a new linked list will be returned to represent their sum.
You can assume that none of these numbers start with 0 except for the number 0.
Example 1:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
Example 2:
输入:(3 -> 7) + (9 -> 2)
输出:2 -> 0 -> 1
原因:73 + 29 = 102
C++ method:
use each bit of the linked list to add, and when the result is greater than 10, move forward (there are some minor issues to be corrected)
class Solution
{
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
ListNode *result = new ListNode();
int Carry = 0;
while (l1->next != nullptr && l2->next != nullptr)
{
int a = l1->val;
int b = l2->val;
Carry = a + b + Carry;
result->next = new ListNode(Carry % 10);
result = result->next;
Carry = Carry / 10;
l1 = l1->next;
l2 = l2->next;
}
while (l1->next != nullptr)
{
Carry = Carry + l1->val;
result->next = new ListNode(Carry % 10);
Carry = Carry / 10;
l1 = l1->next;
result = result->next;
}
while (l2->next != nullptr)
{
Carry = Carry + l2->val;
result->next = new ListNode(Carry % 10);
Carry = Carry / 10;
l2 = l2->next;
result = result->next;
}
if (Carry == 1)
result->next = new ListNode(1);
return result;
}
};
Topic: Finding the median of two arrays
Question number: 4
Difficulty: Difficulty
https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 1
Given two ordered arrays nums1 and nums2 of size m and n.
Please find the median of these two ordered arrays, and the time complexity of the required algorithm is O(log(m + n)).
You can assume that nums1 and nums2 will not be empty at the same time.
Example 1:
nums1 = [1, 3]
nums2 = [2]
则中位数是 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
则中位数是 (2 + 3)/2 = 2.5
C++ approach:
thinking: adopt a recursive method (learn from the idea of the great god, and understand it yourself)
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m = nums1.size(), n = nums2.size(), left = (m + n + 1) / 2, right = (m + n + 2) / 2;
return (findKth(nums1, 0, nums2, 0, left) + findKth(nums1, 0, nums2, 0, right)) / 2.0;
}
int findKth(vector<int>& nums1, int i, vector<int>& nums2, int j, int k) {
if (i >= nums1.size()) return nums2[j + k - 1];
if (j >= nums2.size()) return nums1[i + k - 1];
if (k == 1) return min(nums1[i], nums2[j]);
int midVal1 = (i + k / 2 - 1 < nums1.size()) ? nums1[i + k / 2 - 1] : INT_MAX;
int midVal2 = (j + k / 2 - 1 < nums2.size()) ? nums2[j + k / 2 - 1] : INT_MAX;
if (midVal1 < midVal2) {
return findKth(nums1, i + k / 2, nums2, j, k - k / 2);
} else {
return findKth(nums1, i, nums2, j + k / 2, k - k / 2);
}
}
};
Title: The longest palindrome substring (unfinished, reprinted by others)
Question number: 5
Difficulty: Medium
https://leetcode-cn.com/problems/longest-palindromic-substring/
Given a string s, find the longest palindrome substring in s. You can assume that the maximum length of s is 1000.
Example 1:
输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。
Example 2:
输入: "cbbd"
输出: "bb"
Example 3:
输入: "a"
输出: "a"
C++ approach:
reprinted to https://www.cnblogs.com/cicinnus/p/13227577.html
1. Violent solution time O(n^3) Space O(1)
string longestPalindrome(string s) {
int len = s.length();
if (len < 2) {
return s;
}
int maxn = 1;
int idx = 0;
for (int i = 1; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
string tmp = s.substr(i, j - i + 1);
string tmp2 = tmp;
reverse(tmp.begin(), tmp.end());
if (tmp == tmp2 && j - i + 1 > maxn) {
maxn = j - i + 1;
idx = i;
}
}
}
return s.substr(idx, maxn);
}
2. Dynamic programming time O(n^2) Space O(n^2)
Boundary conditions
When the length of the substring is 1, dp[i][i] = true
When the length of the substring is 2, dp[i][j] = s[i]==s[j]
dynamic transfer equation
dp[i][j] = dp[i+1][j-1] && s[i] == s[j]
string longestPalindrome(string s) {
int len = s.length();
if (len < 2) {
return s;
}
int maxn = 1;
int idx = 0;
vector<vector<int> > dp(len, vector<int>(len));
for (int i = 0; i < len; i++) {
dp[i][i] = 1;
}
for (int j = 1; j < len; j++) {
for (int i = 0; i < j; i++) {
if (s[i] != s[j]) {
dp[i][j] = 0;
}
else
{
if (j - i + 1 < 4)
dp[i][j] = 1;
else
dp[i][j] = dp[i + 1][j - 1];
}
if (dp[i][j] && j - i + 1 > maxn) {
maxn = j - i + 1;
idx = i;
}
}
}
return s.substr(idx, maxn);
}
3. Center expansion time O(n^2) Space O(1)
The character represented by the subscript i is the center point and expand to both ends, and judge whether the expanded character is a palindrome
The length of the palindrome substring is odd, the center is s[i] the
length of the palindrome substring is even, the center is s[i, i+1]
string longestPalindrome(string s) {
int len = s.length();
if (len < 2) {
return s;
}
int maxn = 1;
int idx = 0;
for (int i = 0; i < len - 1; i++) {
int oddLen = computeLen(s, i, i);
int evenLen = computeLen(s, i, i + 1);
int tempLen = max(oddLen, evenLen);
if (tempLen > maxn) {
maxn = tempLen;
idx = i - (tempLen - 1) / 2;
}
}
return s.substr(idx, maxn);
}
int computeLen(string s, int l, int r) {
int len = s.length();
int i = l, j = r;
while (i >= 0 && j < len) {
if (s[i] == s[j]) {
i--; j++;
}
else
{
break;
}
}
return j - i - 1;
}