您当前的位置: 首页 > 

对方正在debug

暂无认证

  • 5浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

复制ip地址(stoi/to_string)

对方正在debug 发布时间:2020-02-18 19:42:42 ,浏览量:5

题目:https://leetcode-cn.com/problems/restore-ip-addresses/ 参考:https://leetcode-cn.com/problems/restore-ip-addresses/solution/cdi-gui-hui-su-jian-zhi-by-codave/

class Solution {
public:
    vector restoreIpAddresses(string s) {
        /*
        *stoi与to_string的应用
        */
        this->s = s;
        len = s.length();
        dfs(0,"",0);
        return ans;
    }
    void dfs(int pos,string ip,int cur) {
        if(pos >= 4) {
            if(cur >= len)
                ans.push_back(ip);
            return;
        }
        for(int k = 1;k  len) break;
            int val = stoi(s.substr(cur,k));
            if(val > 255 || k != (int)to_string(val).size()) continue;
            dfs(pos+1,ip + s.substr(cur,k)+(pos == 3 ? "":"."),cur+k);
        }
    }
private:
    vector ans;
    int len;
    string s;
};
关注
打赏
1664895754
查看更多评论
立即登录/注册

微信扫码登录

0.0379s