题解 | 36进制加法

36进制加法

https://www.nowcoder.com/practice/c5db069fd9d64e6e9cf5fd68860abcdd

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param A string字符串 
# @param B string字符串 
# @return string字符串
#
class Solution:
    def thirtysixAdd(self , A: str, B: str) -> str:
        # write code here
        chars = "0123456789abcdefghijklmnopqrstuvwxyz"
        index_A = len(A) - 1
        index_B = len(B) - 1
        res = []
        carry = 0
        while index_A >= 0 or index_B >= 0 or carry > 0:
            num_A = int(chars.index(A[index_A])) if index_A >= 0 else 0
            num_B = int(chars.index(B[index_B])) if index_B >= 0 else 0
            total = num_A + num_B + carry
            current = total % 36
            res.append(chars[current])
            carry = total // 36
            index_A -= 1
            index_B -= 1
        return "".join(map(str, reversed(res)))

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务