GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/algorithms/cpp/addStrings/AddStrings.cpp at master · weimo/leetcode Expand file tree/ Copy pathAddStrings.cppMore file actions 48 lines (42 loc) · 1.46 KB / Copy pathAddStrings.cppFile metadata and controls48 lines (42 loc) · 1.46 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 // Source : https://leetcode.com/problems/add-strings/ // Author : Hao Chen // Date : 2016-11-12 /*************************************************************************************** * * Given two non-negative numbers num1 and num2 represented as string, return the sum * of num1 and num2. * * Note: * * The length of both num1 and num2 is * Both num1 and num2 contains only digits 0-9. * Both num1 and num2 does not contain any leading zero. * You must not use any built-in BigInteger library or convert the inputs to integer * directly. ***************************************************************************************/ class Solution { public: string addStrings(string num1, string num2) { string& longstr = ( num1.size() >= num2.size() ? num1 : num2 ); string& shortstr = ( num1.size() < num2.size() ? num1 : num2 ); int longlen = longstr.size(); int shortlen = shortstr.size(); char carry = 0; int i, j; string result; for (i = longlen-1, j=shortlen-1; i>=0; i--, j--) { int add = 0; if (j>=0) { add = longstr[i] + shortstr[j] - 2 * '0' + carry; }else{ add = longstr[i] - '0' + carry; } carry = add/10; result = char('0' + add % 10) + result; } if (carry) { result = '1' + result; } return result; } }; |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |