leetcode 397. Integer Replacement

来源:互联网 发布:eclipse端口号 编辑:程序博客网 时间:2024/06/05 14:33

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

超时的版本如下:

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;    }};
0 0
原创粉丝点击