2
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {} * };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int k=0;
ListNode*l=new ListNode(0);
ListNode*l3=l;
while(l1||l2)
{
if(!l1&&l2) l1=new ListNode(0);
if(l1&&!l2) l2=new ListNode(0);
l3->next=new ListNode((l1->val+l2->val+k)%10);
if(l1->val+l2->val+k>9)
k=1;
else
k=0;
l1=l1->next;
l2=l2->next;
l3=l3->next;
}
if(k)
l3->next=new ListNode(k);
return l->next;
}
};
4
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
int m = nums2.size();
if (n > m)
return findMedianSortedArrays(nums2, nums1);
int LMax1, LMax2, RMin1, RMin2, c1, c2, lo = 0, hi = 2 * n;
while (lo <= hi)
{
c1 = (lo + hi) / 2;
c2 = m + n - c1;
LMax1 = (c1 == 0) ? INT_MIN : nums1[(c1 - 1) / 2];
RMin1 = (c1 == 2 * n) ? INT_MAX : nums1[c1 / 2];
LMax2 = (c2 == 0) ? INT_MIN : nums2[(c2 - 1) / 2];
RMin2 = (c2 == 2 * m) ? INT_MAX : nums2[c2 / 2];
if (LMax1 > RMin2)
hi = c1 - 1;
else if (LMax2 > RMin1)
lo = c1 + 1;
else
break;
}
return (max(LMax1, LMax2) + min(RMin1, RMin2)) / 2.0;
}
};
5
class Solution {
public:
string longestPalindrome(string s) {
if(size(s)<2) return s;
int i,k,l=1,location;
string a="*";
for(i=0;i<size(s);i++)
a=a+s.substr(i,1)+"*";
for(i=2;i<size(a)-2;i++)
{
k=1;
while(a[i-k]==a[i+k]&&i-k>=1&&i+k<size(a)-1)
{
k++;
}
if(2*k-1>l&&a[i+k]!=a[i-k])
{
l=2*k-1;
location=i-k+1;
}
else if(2*k+1>l&&a[i+k]==a[i-k])
{
l=2*k+1;
location=i-k;
}
}
if(l==1||l==3)
return s.substr(0,1);
else
a=a.substr(location,l);
s="";
for(i=1;i<l;i+=2)
s=s+a.substr(i,1);
return s;
}
};