NC330. 36进制加法
描述
示例1
输入:
"1b","2b"
输出:
"3m"
示例2
输入:
"z","1"
输出:
"10"
C 解法, 执行用时: 67ms, 内存消耗: 6280KB, 提交时间: 2022-06-26
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 * * C语言声明定义全局变量请加上static,防止重复定义 */ void reverse(char *s) { int sLen = strlen(s); int halfLen = sLen / 2; char tmp; for (int i = 0; i < halfLen; i++) { tmp = s[i]; s[i] = s[sLen - 1 -i]; s[sLen - 1 - i] = tmp; } } int max(int a, int b) { return a > b ? a : b; } int charToInt(char a) { if (a >= '0' && a <= '9') { return a - '0'; } else if (a >= 'a' && a <= 'z') { return a - 'a' + 10; } else { return 0; } } char intToChar(int a) { int m = a % 36; if (m < 10) { return m + '0'; } else { return (m - 10) + 'a'; } } char* thirtysixAdd(char* A, char* B ) { reverse(A); reverse(B); int ALen = strlen(A), BLen = strlen(B); char *result = (char *)malloc(sizeof(char) * (max(ALen, BLen) + 2)); memset(result, 0, sizeof(char) * (max(ALen, BLen) + 2)); int i = 0, carry = 0, v; while(i < ALen || i < BLen || carry > 0) { v = carry; if (i < ALen) { v += charToInt(A[i]); } if (i < BLen) { v += charToInt(B[i]); } carry = v / 36; v = v % 36; result[i++] = intToChar(v); } reverse(A); reverse(B); reverse(result); return result; }
C++ 解法, 执行用时: 67ms, 内存消耗: 12060KB, 提交时间: 2022-06-25
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ string add(string &s1, string &s2) { string res = ""; int carry = 0; for (int i = 0; i < s1.length(); i++) { int a, b; if (s1[i] >= '0' && s1[i] <= '9') { a = s1[i] - '0'; } else { a = s1[i] - 87; } if (s2[i] >= '0' && s2[i] <= '9') { b = s2[i] - '0'; } else { b = s2[i] - 87; } int t = a + b + carry; carry = t / 36; t = t % 36; if (t < 10) { res.append(1, '0' + t); } else { res.append(1, 'a' + t - 10); } } if(carry>0) { if (carry < 10) { res.append(1, '0' + carry); } else { res.append(1, 'a' + carry - 10); } } return res; } string thirtysixAdd(string s1, string s2) { // write code here reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); while (s1.length() < s2.length()) { s1.append(1, '0'); } while (s2.length() < s1.length()) { s2.append(1, '0'); } string res=add(s1,s2); reverse(res.begin(),res.end()); return res; } };
C++ 解法, 执行用时: 68ms, 内存消耗: 13036KB, 提交时间: 2022-06-10
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ int CharToNum(char ch) { if(isdigit(ch)) { return ch - '0'; } else { return ch - 'a' + 10; } } char NumToChar(int num) { if(num < 10) { // 0 ~ 9 return num + '0'; } else { // 10 ~ 35 return num - 10 + 'a'; } } string thirtysixAdd(string A, string B) { int sizeA = A.size(); int sizeB = B.size(); vector<char> resVec(sizeA + sizeB); int index = resVec.size() - 1; int indexA = sizeA - 1, indexB = sizeB - 1; int add = 0; while(indexA >= 0 || indexB >= 0 || add > 0) { int numA = indexA >= 0 ? CharToNum(A[indexA]) : 0; int numB = indexB >= 0 ? CharToNum(B[indexB]) : 0; int val = numA + numB + add; add = val / 36; val = val % 36; resVec[index] = NumToChar(val); index--; indexA--; indexB--; } index = 0; while(resVec[index] == 0) { index++; } string res; for(int i = index; i < resVec.size(); i++) { res.push_back(resVec[i]); } return res; } };
C++ 解法, 执行用时: 69ms, 内存消耗: 12140KB, 提交时间: 2022-05-23
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ string num = "0123456789abcdefghijklmnopqrstuvwxyz"; string thirtysixAdd(string A, string B) { string res; int i = A.size() - 1; int j = B.size() - 1; int temp = 0; while (i >= 0 && j >= 0) { int value_a = num.find(A[i]); int value_b = num.find(B[j]); int sum = value_a + value_b + temp; if (sum >= 36) { temp = 1; res += num[sum % 36]; } else { temp = 0; res += num[sum]; } i--, j--; } while (i >= 0) { int sum = num.find(A[i]) + temp; if (sum >= 36) { temp = 1; res += num[sum % 36]; } else { temp = 0; res += num[sum]; } i--; } while (j >= 0) { int sum = num.find(B[j]) + temp; if (sum >= 36) { temp = 1; res += num[sum % 36]; } else { temp = 0; res += num[sum]; } j--; } if (temp) { res += '1'; } reverse(res.begin(), res.end()); return res; } };
C++ 解法, 执行用时: 69ms, 内存消耗: 12140KB, 提交时间: 2022-05-04
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ string num = "0123456789abcdefghijklmnopqrstuvwxyz"; string thirtysixAdd(string A, string B) { // write code here // 倒过来相加 string res; int i = A.size() - 1; int j = B.size() - 1; int index1, index2, sum, carry = 0; while (i >= 0 && j >= 0) { index1 = num.find(A[i]); index2 = num.find(B[j]); sum = index1 + index2 + carry; if (sum >= 36) { carry = 1; res += num[sum % 36]; } else { carry = 0; res += num[sum]; } i--, j--; } //A有剩余 while (i >= 0) { sum = num.find(A[i]) + carry; if (sum >= 36) { carry = 1; res += num[sum % 36]; } else { carry = 0; res += num[sum]; } i--; } //B有剩余 while (j >= 0) { sum = num.find(B[j]) + carry; if (sum >= 36) { carry = 1; res += num[sum % 36]; } else { carry = 0; res += num[sum]; } j--; } if (carry) { res += '1'; } reverse(res.begin(), res.end()); return res; } };