[Python] 3.9

Trp_Cys Trp_Cys 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

题目: 504. 七进制数 - 力扣(Leetcode)

class Solution:
    def convertToBase7(self, num: int) -> str:
        hep = 0
        index = 0
        abs_num = abs(num)
        str_hep = ''
        if num > 0:
            while num > 0:
                hep += (num % 7) * pow(10, index)
                index += 1
                num //= 7
            str_hep = str(hep)
        elif num < 0:
            num = abs_num
            while num > 0:
                hep += (num % 7) * pow(10, index)
                index += 1
                num //= 7
            str_hep = '-' + str(hep)
        else:
            str_hep = '0'
        
        return (str_hep)

 

题目:342. 4的幂 - 力扣(Leetcode)

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        index = 0   # 四进制的位数
        quater = 0   # n 的四进制
        while n > 0:
            quater += (n % 4) * pow(10, index)
            index += 1
            n //= 4
        s = str(quater)
        if s[0] == '1' and s[1:].count('0') == index - 1:
            return True
        else:
            return False

 

 

...全文
评论
收藏 已收藏
分享
返回顶部