#include <string>
#include <vector>
class Solution {
public:
string solve(string s, string t) {
int m = s.size(), n = t.size();
reverse(s.begin(), s.end());
reverse(t.begin(), t.end());
//1.无进位相加并相乘
vector<int> tmp(m + n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
tmp[i + j] += (s[i] - '0') * (t[j] - '0');
}
}
//2.处理进位
int c = 0;
string ans;
for (auto x : tmp) {
c = c + x;
ans += c % 10 + '0';
c = c / 10;
}
while (c) {
ans += c % 10 + '0';
c /= 10;
}
//3.处理前置0
while (ans.size() > 1 && ans.back() == '0') ans.pop_back();
reverse(ans.begin(), ans.end());
return ans;
}
};
#include <type_traits>
class Solution {
public:
string solve(string s, string t) {
int i=s.size()-1,j=t.size()-1;
int tmp=0; //进位符
string ans;
while(i>=0||j>=0||tmp)
{
int sum=0;
if(i>=0) tmp+=s[i--]-'0';
if(j>=0) tmp+=t[j--]-'0';
ans+=tmp%10+'0';
tmp/=10;
}
reverse(ans.begin(),ans.end());
return ans;
}
};
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
#include <new>
class Solution {
public:
ListNode* reverse(ListNode* head)
{
ListNode* newhead=new ListNode(0);
ListNode* cur=head;
while(cur)
{
ListNode* next=cur->next;
cur->next=newhead->next;
newhead->next=cur;
cur=next;
}
cur=newhead->next;
delete newhead;
return cur;
}
ListNode* addInList(ListNode* head1, ListNode* head2) {
//逆序
head1=reverse(head1),head2=reverse(head2);
ListNode *cur1=head1,*cur2=head2;
ListNode *ret=new ListNode(0);
ListNode *prve=ret;
int t=0;
while(cur1||cur2||t)
{
if(cur1)
{
t+=cur1->val;
cur1=cur1->next;
}
if(cur2)
{
t+=cur2->val;
cur2=cur2->next;
}
prve=prve->next=new ListNode(t%10);
t/=10;
}
cur1=ret->next;
delete ret;
return reverse(cur1);
}
};