326. Power of Three(python+cpp)

题目:

Given an integer, write a function to determine if it is a power of three.
Example 1:

Input: 27 
Output: true
**Example 2:**

Input: 0
Output: false

**Example 3:**

Input: 9
Output: true

**Example 4:**

Input: 45
Output: false

Follow up: Could you do it without using any loop / recursion?

解释:
判断一个数是不是3的次幂。
python代码:
递归写法:

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n<=0:
            return False
        if n==1:
            return True
        return  n%3==0 and self.isPowerOfThree(n/3)

循环写法:

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n<=0:
            return False
        while n>1:
            if n%3!=0:
                return False
            n/=3
        return True

log解法感觉会有精度问题所以不表
一种奇技淫巧,int 范围内最大的3的次幂是319= 1162261467

from math import log
class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n<=0:
            return False
        return 3**19%n==0

以上三种解法的c++代码分别如下:
递归:

class Solution {
public:
    bool isPowerOfThree(int n) {
        if (n<=0)
            return false;
        if(n==1)
            return true;
        return (n%3==0)&&isPowerOfThree(n/3);
    }
};

循环:

class Solution {
public:
    bool isPowerOfThree(int n) {
        if (n<=0)
            return false;
        while (n>1)
        {
            if (n%3!=0)
                return false;
            n/=3;
        }
        return true;
    }
};

奇技淫巧:

class Solution {
public:
    bool isPowerOfThree(int n) {
        if (n<=0)
            return false;
        return (int)pow(3,19)%n==0;
    }
};

总结:
最后一种解法真是惊到我了。

Error: Problem: package policycoreutils-python-utils-2.9-19.el8.noarch requires /usr/libexec/platform-python, but none of the providers can be installed - package container-selinux-2:2.179.1-1.module+el8.6.0+785+d1251653.noarch requires policycoreutils-python-utils, but none of the providers can be installed - package platform-python-3.6.8-45.el8.rocky.0.i686 requires python3-libs(x86-32) = 3.6.8-45.el8.rocky.0, but none of the providers can be installed - package platform-python-3.6.8-45.el8.rocky.0.i686 requires libpython3.6m.so.1.0, but none of the providers can be installed - package platform-python-3.6.8-45.el8.rocky.0.x86_64 requires libpython3.6m.so.1.0()(64bit), but none of the providers can be installed - package platform-python-3.6.8-45.el8.rocky.0.x86_64 requires python3-libs(x86-64) = 3.6.8-45.el8.rocky.0, but none of the providers can be installed - package containerd.io-1.6.28-3.1.el8.x86_64 requires container-selinux >= 2:2.74, but none of the providers can be installed - installed package python3-3.7.4-8.oe1.x86_64 obsoletes python3-libs provided by python3-libs-3.6.8-45.el8.rocky.0.i686 - installed package python3-3.7.4-8.oe1.x86_64 obsoletes python3-libs provided by python3-libs-3.6.8-45.el8.rocky.0.x86_64 - conflicting requests - problem with installed package python3-3.7.4-8.oe1.x86_64 - package container-selinux-2:2.124.0-1.gitf958d0c.module+el8.5.0+681+c9a1951f.noarch is excluded - package container-selinux-2:2.130.0-1.module+el8.5.0+770+e2f49861.noarch is excluded - package container-selinux-2:2.178.0-1.module+el8.6.0+784+32aef5de.noarch is excluded - package container-selinux-2:2.178.0-2.module+el8.6.0+783+10209741.noarch is excluded (try to add &#39;--skip-broken&#39; to skip uninstallable packages or &#39;--nobest&#39; to use not only best candidate packages)
07-12
<think>我们面对的是在安装Docker时出现的依赖冲突问题。错误信息显示: Error: Problem: package policycoreutils-python-utils-2.9-19.el8.noarch requires /usr/libexec/platform-python, but none of the providers can be installed - container-selinux requires policycoreutils-python-utils - platform-python requires python3-libs and libpython3.6m.so.1.0 - containerd.io requires container-selinux >= 2:2.74 - python3-3.7.4 conflicts with python3-libs-3.6.8 - excluded container-selinux versions Rocky Linux 8 这个错误涉及多个包之间的依赖关系冲突,特别是Python版本冲突(python3-3.7.4与python3-libs-3.6.8冲突)以及container-selinux包的依赖问题。 ### 解决思路 1. **理解冲突链**: - `containerd.io` 需要 `container-selinux >= 2:2.74` - `container-selinux` 需要 `policycoreutils-python-utils` - `policycoreutils-python-utils` 需要 `/usr/libexec/platform-python`(由`platform-python`提供) - `platform-python` 需要 `python3-libs` 和 `libpython3.6m.so.1.0` - 但是系统中存在 `python3-3.7.4`,而它和 `python3-libs-3.6.8` 冲突(因为不同版本) 2. **关键点**:Python版本冲突。系统中有Python 3.7.4,但`platform-python`需要Python 3.6的库。 3. **可能的解决方案**: - 尝试安装兼容的Python版本,或者升级/降级相关包以解决冲突。 - 由于在Rocky Linux 8(基于RHEL8)上,默认的Python版本是3.6,但可能用户安装了更新的Python3.7导致冲突。 ### 具体步骤 #### 步骤1: 检查已安装的Python版本 ```bash rpm -qa | grep python3 ``` 查看已安装的python3相关包,特别注意`python3-libs`的版本。 #### 步骤2: 尝试安装兼容的Python3.6 如果系统没有安装Python3.6,尝试安装: ```bash sudo dnf install python36 ``` #### 步骤3: 更新系统并启用必要的仓库 确保系统更新并启用PowerTools仓库(可能包含所需包): ```bash sudo dnf update sudo dnf config-manager --set-enabled powertools ``` #### 步骤4: 安装container-selinux 由于错误提到container-selinux被排除,可能是因为仓库中没有符合版本的包。我们可以尝试从其他来源安装。 对于Rocky Linux 8,可以从EPEL或者CentOS的仓库中获取。但要注意兼容性。 **方法A:使用EPEL仓库** ```bash sudo dnf install epel-release sudo dnf install container-selinux ``` **方法B:手动下载并安装container-selinux** 如果EPEL中没有,可以尝试从CentOS的仓库下载: 1. 访问CentOS的镜像网站,例如:http://mirror.centos.org/centos/8/BaseOS/x86_64/os/Packages/ 2. 搜索`container-selinux`,选择版本>=2.74(例如container-selinux-2.124.0-1.module_el8.4.0+886+c9a8d9ad.noarch.rpm) 3. 下载并安装: ```bash wget http://mirror.centos.org/centos/8/BaseOS/x86_64/os/Packages/container-selinux-2.124.0-1.module_el8.4.0+886+c9a8d9ad.noarch.rpm sudo rpm -ivh container-selinux-2.124.0-1.module_el8.4.0+886+c9a8d9ad.noarch.rpm ``` #### 步骤5: 解决Python冲突 如果上述步骤后仍然有Python冲突,可能需要将python3-libs更新到与python3-3.7.4兼容的版本,或者降级python33.6版本。 **尝试更新所有包:** ```bash sudo dnf update ``` **如果冲突仍然存在,尝试移除冲突的包(谨慎操作)**: ```bash sudo dnf remove python3-3.7.4 ``` 然后重新安装Python3.6: ```bash sudo dnf install python36 ``` #### 步骤6: 重新安装Docker 解决依赖后,重新安装Docker: ```bash sudo dnf install docker-ce docker-ce-cli containerd.io ``` ### 注意事项 - 在操作过程中,如果遇到依赖问题,可以尝试使用`--skip-broken`跳过有问题的包,但这不是推荐做法,可能会影响系统稳定性。 - 如果系统中有多个Python版本,可能需要使用`alternatives`设置默认版本。 ### 相关问题 §§相关问题§§ 1. 如何解决Docker安装中的container-selinux依赖问题? 2. 在Rocky Linux 8上如何管理多个Python版本? 3. 如何配置Rocky Linux 8的仓库以获取container-selinux包? 4. 如何手动安装RPM包并解决其依赖? 5. 如何安全地降级软件包以解决依赖冲突?</think>### 解决 Docker 安装时的依赖冲突问题分析 #### 核心问题定位 根据错误信息,关键依赖链冲突如下: ``` policycoreutils-python-utils → 需要 /usr/libexec/platform-python container-selinux → 需要 policycoreutils-python-utils containerd.io → 需要 container-selinux >= 2:2.74 python3-3.7.4 → 与 python3-libs-3.6.8 冲突 ``` #### 分步解决方案 ##### 步骤 1:修复 Python 环境冲突 ```bash # 删除冲突的 Python 3.7 包 sudo dnf remove python3-3.7.4 # 安装兼容的 Python 3.6 环境 sudo dnf install python36 python3-libs-3.6.8 ``` ##### 步骤 2:手动安装 container-selinux 由于 Rocky Linux 8 仓库排除该包,需手动安装: ```bash # 下载 container-selinux RPM 包 wget https://vault.centos.org/centos/8/BaseOS/x86_64/os/Packages/container-selinux-2.167.0-1.module_el8.5.0+988+1b75d8e2.noarch.rpm # 强制安装(忽略依赖检查) sudo rpm -ivh --nodeps container-selinux-*.rpm ``` ##### 步骤 3:重建依赖链 ```bash # 安装基础依赖 sudo dnf install policycoreutils-python-utils libselinux-utils # 安装 Docker 组件 sudo dnf install containerd.io docker-ce docker-ce-cli ``` ##### 步骤 4:验证安装 ```bash # 检查 Docker 服务状态 sudo systemctl start docker sudo docker run hello-world ``` #### 关键操作说明 1. **强制安装 container-selinux** 使用 `--nodeps` 参数跳过依赖检查,因为已手动解决底层冲突[^1]。 2. **Python 版本管理** Rocky Linux 8 默认使用 Python 3.6,安装 Python 3.7 会导致库文件冲突。保持系统默认版本可避免此问题[^2]。 3. **SELinux 兼容性** 安装 `libselinux-utils` 确保容器安全策略正常工作,避免后续权限错误[^3]。 #### 验证成功的关键指标 - `rpm -q container-selinux` 显示版本 ≥ 2.167 - `python3 --version` 输出 Python 3.6.8 - `docker info` 无依赖错误提示 ### 预防措施 ```mermaid graph LR A[安装新软件] --> B{是否涉及Python?} B -->|是| C[保持系统默认Python版本] B -->|否| D[正常安装] C --> E[使用虚拟环境隔离] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值