You're given 2 huge integers represented by linked...
創建於:2025年6月23日
創建於:2025年6月23日
You're given 2 huge integers represented by linked lists. Each linked list element is a number from 0 to 9999 that represents a number with exactly 4 digits. The represented number might have leading zeros. Your task is to add up these huge integers and return the result in the same format.
Example
For a = [9876, 5432, 1999] and b = [1, 8001], the output should be solution(a, b) = [9876, 5434, 0].
Explanation: 987654321999 + 18001 = 987654340000.
For a = [123, 4, 5] and b = [100, 100, 100], the output should be solution(a, b) = [223, 104, 105].
Explanation: 12300040005 + 10001000100 = 22301040105.
in c++ wihtotu comments
cpp#include <bits/stdc++.h> using namespace std; vector<int> solution(const vector<int>& a, const vector<int>& b) { int i = static_cast<int>(a.size()) - 1; int j = static_cast<int>(b.size()) - 1; int carry = 0; vector<int> res; while (i >= 0 || j >= 0 || carry) { int sum = carry; if (i >= 0) sum += a[i--]; if (j >= 0) sum += b[j--]; res.push_back(sum % 10000); carry = sum / 10000; } reverse(res.begin(), res.end()); return res; }