[Python] 3.9

Trp_Cys 2023-03-09 13:54:14

题目: 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

 

 

...全文
258 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

30,416

社区成员

发帖
与我相关
我的任务
社区描述
打造最热爱学习的高校社区,帮助大家提升计算机领域知识,带大家打比赛拿奖,提高自我,希望大家共同创造良好的社区氛围。
社区管理员
  • 川川菜鸟
  • 亡心灵
  • 星辰菜鸟
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

监督大家学习,每日学习打卡,以投稿形式打卡。扫码关注公众号,可加入粉丝群和领取大量资源。

试试用AI创作助手写篇文章吧