leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0415.cpp (525B)


      1 class Solution {
      2   public:
      3     string addStrings(string num1, string num2) {
      4         if (num1 == "0" && num2 == "0") return "0";
      5 
      6         string res = "";
      7         int i = num1.size() - 1;
      8         int j = num2.size() - 1;
      9 
     10         int add = 0;
     11         while (i >= 0 || j >= 0 || add) {
     12             if (i >= 0) add += num1[i--] - '0';
     13             if (j >= 0) add += num2[j--] - '0';
     14             res += to_string(add % 10);
     15             add /= 10;
     16         }
     17 
     18         reverse(res.begin(), res.end());
     19         return res;
     20     }
     21 };