leetcode 397. Integer Replacement

本文探讨了在解决特定问题时广度优先搜索算法的一种优化方法。通过避免使用耗时的操作如cout,并调整数据类型来处理大整数,显著提高了算法效率。文中提供了优化前后的代码实现对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始想的是采用类似于广度搜索图的算法计算,发现它超时了。

超时的版本如下:

class Solution {
public:
    typedef struct trace
    {
        int num;
        int residual;
    }TRACE;

    int integerReplacement(int n) {
        int rel = INT_MAX;
        int ini = 0;
        queue<TRACE> remain;

        cout << rel << endl; 

        while(1)
        {
            if(!ini)
            {
                if(n == 1)
                    return 0;

                if(n&1)
                {
                    TRACE t_h = {n+1, 1};
                    TRACE t_l = {n-1, 1};
                    remain.push(t_h);
                    remain.push(t_l);
                }    
                else
                {
                    TRACE t = {n >> 1, 1};
                    remain.push(t);
                }

                ini = 1;
            }
            else
            {
                int cur_size = remain.size();
                cout << cur_size << endl;

                for(int i = 0; i < cur_size; i++)
                {
                    TRACE t_top = remain.front();
                    remain.pop();
                    cout << t_top.num << "  " << t_top.residual << endl;
                    if(t_top.num == 1 && t_top.residual < rel)
                    {
                        rel = t_top.residual;
                        return rel;
                    }    
                    else if(t_top.num & 1)
                    {
                        TRACE t_h = {t_top.num+1, t_top.residual+1};
                        TRACE t_l = {t_top.num-1, t_top.residual+1}; 
                        remain.push(t_h);
                        remain.push(t_l);                        
                    }   
                    else
                    {
                       TRACE t = {t_top.num >> 1, t_top.residual+1};
                       remain.push(t);
                    }
                }

                if(remain.empty())
                    return rel;
            }
        }

        return rel;
    }
};

后来我觉得这里面可能有问题,原来是cout占了不少时间。后来我运行,发现输入的n居然有可能是最大的有符号的正整数,所以我进行一个类型转换。下面就是可以的代码。

class Solution {
public:
    typedef struct trace
    {
        unsigned int num;
        int residual;
    }TRACE;

    int integerReplacement(int n) {
        int rel = INT_MAX;
        int ini = 0;
        queue<TRACE> remain;

        cout << rel << endl; 

        while(1)
        {
            if(!ini)
            {
                if(n == 1)
                    return 0;

                if(n&1)
                {
                    TRACE t_h = {(unsigned int)(n+1), 1};
                    remain.push(t_h);
                    TRACE t_l = {n-1, 1};
                    remain.push(t_l);
                }    
                else
                {
                    TRACE t = {n >> 1, 1};
                    remain.push(t);
                }

                ini = 1;
            }
            else
            {
                int cur_size = remain.size();
                // cout << cur_size << endl;

                for(int i = 0; i < cur_size; i++)
                {
                    TRACE t_top = remain.front();
                    remain.pop();
                    // cout << t_top.num << "  " << t_top.residual << endl;
                    if(t_top.num == 1 && t_top.residual < rel)
                    {
                        rel = t_top.residual;
                        return rel;
                    }    
                    else if(t_top.num & 1)
                    {
                        TRACE t_h = {t_top.num+1, t_top.residual+1};
                        TRACE t_l = {t_top.num-1, t_top.residual+1}; 
                        remain.push(t_h);
                        remain.push(t_l);                        
                    }   
                    else
                    {
                       int tmp = t_top.num >> 1;
                       TRACE t = {tmp, t_top.residual+1};
                       remain.push(t);
                    }
                }

                if(remain.empty())
                    return rel;
            }
        }

        return rel;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值