36进制数由 0~9 , a~z 共 36 个字符组成,请你按照加法规则计算两个 36 进制数的和。
例如 1b+2b = 3m
数据范围:两个数的长度都满足 , 两个数中包含的字符仅包括 和
package main func thirtysixAdd( A string , B string ) string { lA, lB := len(A), len(B) res := []byte{} // 转为数字进行计算 i, j, carry := lA-1, lB-1, 0 for i >= 0 || j >= 0 || carry > 0 { t := 0 if i >= 0 { t = count(A[i]) i-- } if j >= 0 { t += count(B[j]) j-- } t += carry res = append(res, byte(t%36)) carry = t / 36 } // 反转 for i := 0; i < len(res) / 2; i++ { res[i], res[len(res)-i-1] = res[len(res)-i-1], res[i] } // 转回字母返回答案 for i, v := range res { if v < 10 { res[i] = v + '0' } else { res[i] = 'a' + v - 10 } } return string(res) } func count(a byte) int { if a > '9' { return int(a-'a'+10) } else { return int(a-'0') } }
import java.util.*; public class Solution { public static HashMap<Integer, Character> map1 = new HashMap<>(); public static HashMap<Character, Integer> map2 = new HashMap<>(); static{ generate36Str(); } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ public String thirtysixAdd (String A, String B) { // write code here int n = A.length()-1, m = B.length()-1; StringBuilder ans = new StringBuilder(); int add = 0; while(n >= 0 || m >= 0 || add != 0) { int a = n >= 0 ? map2.get(A.charAt(n)) : 0; int b = m >= 0 ? map2.get(B.charAt(m)) : 0; int sum = a + b + add; ans.append(map1.get(sum % 36)); add = sum / 36; n--; m--; } ans.reverse(); return ans.toString(); } public static void generate36Str() { for(int i = 0; i <= 9 ; i++) { map1.put(i, (char) (48 + i)); map2.put((char) (48 + i), i); } for(int i = 0; i <= 26 ; i++) { map1.put(10 + i, (char) (97 + i)); map2.put((char) (97 + i), 10 + i); } } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ public String thirtysixAdd (String A, String B) { // write code here int add = 0; StringBuilder sb = new StringBuilder(); char[] arr1 = A.toCharArray(); char[] arr2 = B.toCharArray(); int a = arr1.length - 1; int b = arr2.length - 1; while (a >= 0 || b >= 0 || add != 0) { int x = a >= 0 ? (Character.isDigit(A.charAt(a)) ? A.charAt(a) - '0' : (A.charAt(a) - 'a' + 10)) : 0; int y = b >= 0 ? (Character.isDigit(B.charAt(b)) ? B.charAt(b) - '0' : (B.charAt(b) - 'a' + 10)) : 0; int res = (x + y + add) % 36; if (res >= 0 && res <= 9) { sb.append(res); } else { sb.append((char)(res - 10 + 'a')); } add = (x + y + add) / 36; a--; b--; } return sb.reverse().toString(); } }
package main //import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ func thirtysixAdd( num1 string , num2 string ) string { i, j := len(num1) - 1, len(num2) - 1 // i,j都指向数字的最后一位 carry := 0 // 进位 res := []byte{} // 字节切片来存储每位相加后得到的答案 for x, y := 0, 0; i >= 0 || j >= 0 || carry > 0; i, j = i - 1, j - 1 { // x存储第一个数的一位数字 // 将其转为整型 if i >= 0 { x = getInt(num1[i]) } else { x = 0 } // y存储第二个数的一位数字 // 将其转为整型 if j >= 0 { y = getInt(num2[j]) } else { y = 0 } // 两数同位的数字相加,再加上进位 temp := x + y + carry // 转为字节型添加到res里面,个位数先计算,故总体上低位数在前,高位数在后 res = append(res, getChar(temp % 36)) // 如果满36,就有进位 carry = temp / 36 } reverse(res) // 反转后得到一个正序的结果,高位数在前,低位数在后 return string(res) } // 前后两两原地交换 func reverse(res []byte) []byte { for i := 0; i < len(res) / 2; i++ { j := len(res) - i - 1 res[i], res[j] = res[j], res[i] } return res } // 转成字节型 func getChar(n int) byte { if n <= 9 { return byte(n) + '0' } else { return byte(n - 10) + 'a' } } // 转成整型 func getInt(ch byte) int { if '0' <= ch && ch <= '9' { return int(ch - '0') } else { return int(ch - 'a' + 10) } }
class Solution { public: string thirtysixAdd(string A, string B) { reverse(A.begin(), A.end()); reverse(B.begin(), B.end()); int t = 0, i = 0, j = 0; vector<int> ans; while (i < A.size() || j < B.size() || t > 0) { if (i < A.size()) { t += count(A[i]); i++; } if (j < B.size()) { t += count(B[j]); j++; } ans.push_back(t % 36); t /= 36; } reverse(ans.begin(), ans.end()); string res = ""; for (auto& x : ans) { if (x < 10) { res.push_back(x + '0'); } else { res.push_back(x - 10 + 'a'); } } return res; } int count(char c) { if (c > '9') { return c - 'a' + 10; } return c - '0'; } };
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ public String thirtysixAdd (String a, String b) { // write code here StringBuilder sb = new StringBuilder(); int i = a.length() - 1, j = b.length() - 1; int r = 0; while (i >= 0 && j >= 0) { int x = char2int(a.charAt(i--)) + char2int(b.charAt(j--)) + r; sb.append(int2char(x % 36)); r = x / 36; } while (i >= 0) { int x = char2int(a.charAt(i--)) + r; sb.append(int2char(x)); r = x / 36; } while (j >= 0) { int x = char2int(b.charAt(j--)) + r; sb.append(int2char(x)); r = x / 36; } if (r != 0) sb.append(int2char(r)); return sb.reverse().toString(); } private int char2int(char c) { if ('0' <= c && c <= '9') return c - '0'; return c - 'a' + 10; } private char int2char(int x) { if (0 <= x && x <= 9) return (char) ('0' + x); return (char) ((x - 10) + 'a'); } }