码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • c++入门99题21-30


    解题报告

    1.力扣476

    原题链接

    476. 数字的补数

    源码剖析

    class Solution {
    public:
        int findComplement(int num) {
            if(num==0) return 1;
            long n = 1;
            while(n<=num){
                n<<=1;
            }
            return (n-1)^num;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.力扣剑指 Offer 15. 二进制中1的个数

    原题链接

    剑指 Offer 15. 二进制中1的个数

    源码剖析

    法一:

    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            int ans=0;
            while(n>0){
                ans+=n&1;
                n=n>>1;
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    计算一位就右移一位,使用 & 来判断是否当前位为 1 。
    法二:

    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            int ans = 0;
            while(n){
                n&=(n-1);
                ans++;
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    使用位与来消除最末位的 1。

    3.力扣191

    原题链接

    191. 位1的个数

    源码剖析

    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            int ans = 0;
            while(n){
                n&=(n-1);
                ans++;
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    同上。

    4.力扣461

    原题链接

    461. 汉明距离

    源码剖析

    class Solution {
        int cal(int n){
            int ans = 0;
            while(n){
                n&=(n-1);
                ans++;
            }
            return ans;
        }
    public:
        int hammingDistance(int x, int y) {
            return cal(x^y);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    仍然同上,先异或两个数然后再求位 1 个数即可。

    解题报告

    5.力扣2220

    原题链接

    2220. 转换数字的最少位翻转次数

    源码剖析

    class Solution {
        int cal(int n){
            int ans = 0;
            while(n){
                n&=(n-1);
                ans++;
            }
            return ans;
        }
    public:
        int minBitFlips(int start, int goal) {
            return cal(start^goal);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    其实这题和上一题是一样的,翻转只需要翻转不同的位即可。

    6.力扣1342

    原题链接

    1342. 将数字变成 0 的操作次数

    源码剖析

    class Solution {
    public:
        int numberOfSteps(int num) {
            int ans=0;
            while(num){
                if(num&1){
                    num--;
                }else{
                    num/=2;
                }
                ans++;
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    直接使用循环即可。

    7.力扣1492

    原题链接

    1492. n 的第 k 个因子

    源码剖析

    class Solution {
    public:
        int kthFactor(int n, int k) {
            int i = 0;
            int ans = 1;
            while(ans<=n){
                if(n%ans==0){
                    ++i;
                    if(i==k){
                        return ans;
                    }else{
                        ans++;
                    }
                }else{
                    ans++;
                }
            }
            return -1;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    使用了一堆的判断进行处理,不过效率还可以。

    8.力扣2006

    原题链接

    2006. 差的绝对值为 K 的数对数目

    源码剖析

    class Solution {
    public:
        int countKDifference(vector<int>& nums, int k) {
            int ans = 0;
            int size = nums.size();
            for(int i = 0;i<size;++i){
                for(int j = i+1;j<size;++j){
                    if(abs(nums[i]-nums[j])==k){
                        ans++;
                    }
                }
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    解题报告

    9.力扣1929

    原题链接

    1929. 数组串联

    源码剖析

    class Solution {
    public:
        vector<int> getConcatenation(vector<int>& nums) {
            int n = nums.size();
            for(int i = 0;i<n;++i){
                nums.push_back(nums[i]);
            }
            return nums;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    直接将原来的元素按顺序在往原数组的后面填入就可以了。

    10.力扣1108

    原题链接

    1108. IP 地址无效化

    源码剖析

    class Solution {
    public:
        string defangIPaddr(string address) {
            string ans;
            for(int i = 0;i<address.size();++i){
                if(address[i]=='.'){
                    ans+="[.]";
                }else{
                    ans+=address[i];
                }
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    string类确实比char数组好用多了。

  • 相关阅读:
    DevOps系列---【jenkinsfile使用sshpass发送到另一台服务器】
    风控特征的优化分箱,看看这样教科书的操作
    git 以某次提交 创建新的分支
    RobotFramework+Eclispe环境安装篇
    Python初级教程-廖雪峰Python教程
    shellcode编写
    贪心算法选择不相交区间
    yolov8中train.py、val.py、predict.py的区别,什么时候该用哪个?
    (最新版2022版)剑指offer之树和二叉树题解
    IDEA启动Web项目总是提示端口占用
  • 原文地址:https://blog.csdn.net/smallwind256/article/details/125995413
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号