#include <algorithm> #include <string> #include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 计算两个数之和 * @param s string字符串 表示第一个整数 * @param t string字符串 表示第二个整数 * @return string字符串 */ string solve(string s, string t) { if(s.empty()) { return t; } if (t.empty()) { return s; } if (s.length() < t.length()) { swap(s, t); } int i = s.length() - 1; int j = t.length() - 1; int carry = 0; string ans; while (i >= 0 || j >= 0 || carry != 0) { int x = i >= 0 ? s[i] - '0' : 0; int y = j >= 0 ? t[j] - '0' : 0; int temp = x + y + carry; ans += temp % 10 + '0'; carry = temp / 10; i -= 1; j -= 1; } reverse(ans.begin(), ans.end()); return ans; } };</vector></string></algorithm>
点赞

相关推荐

牛客热帖

牛客网
牛客企业服务