题解 | #36进制加法#
36进制加法
https://www.nowcoder.com/practice/c5db069fd9d64e6e9cf5fd68860abcdd
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((char)(res + '0')); } else { sb.append((char)(res - 10 + 'a')); } add = (x + y + add) / 36; a--; b--; } return sb.reverse().toString(); } }