zoukankan      html  css  js  c++  java
  • lc 复原IP地址

    链接:https://leetcode-cn.com/explore/interview/card/bytedance/242/string/1044/

    代码:

    #include <string>
    #include <algorithm>
    using namespace std;
    class Solution {
    public:
        vector<string> restoreIpAddresses(string s) {
            string path[5];
            int k = 0;
            int index = 0;
            vector<string> res;
            find(s, path, k, index, res);
            return res;
        }
        bool is_valid(string s, int front, int back) {
            if(back >= s.size()) return false;
            if(back - front >= 1 && s[front] == '0') return false;
            string temp = "";
            for(int i = front; i <= back; i++) {
                temp += s[i];
            }
            // cout << temp << endl;
            int value = stoi(temp);
            if(value >= 0 && value <= 255) return true;
            else return false;
        }
        void find(string s, string path[], int k, int index, vector<string> & res) {
            if(index >= s.size()) return;
            if(k == 3) {
                // debug
                string temp = "";
                for(int i = index; i <= s.size()-1; i++) {
                    temp += s[i];
                }
                // cout << temp << endl;
                // if(temp == "135") cout << "++++++++++" << endl;
                
                if(s.size() - index > 3) return;
                
                if(is_valid(s, index, s.size()-1)) {
                    cout << "====" << endl;
                    string temp = "";
                    for(int i = index; i <= s.size()-1; i++) {
                        temp += s[i];
                    }
                    string ss = "";
                    for(int i = 0; i < 3; i++) {
                        if(i == 0) ss += path[i];
                        else {
                            ss += ".";
                            ss += path[i];
                        }
                    }
                    ss += ".";
                    ss += temp;
                    // cout << ss << endl;
                    res.push_back(ss);
                }
                return;
            }
            for(int i = 0; i <= 2; i++) {
                if(is_valid(s, index, index+i)) {
                    string temp = "";
                    for(int j = index; j <= index+i; j++) {
                        temp += s[j];
                    }
                    path[k] = temp;
                    // cout << "i: " << index << " " << index + i << endl;
                    // cout << "k: " << k << endl;
                    // cout << "path: ";
                    // for(int j = 0; j <= k; j++) {
                    //     cout << path[j] << " ";
                    // }
                    // cout << endl;
                    // cout << "------" << endl;
                    if(index+i+1 < s.size() && k < 3) find(s, path, k+1, index+i+1, res);
                }
                
            }
            return;
        }
        
    };
    View Code

    思路:深搜,不符合条件回溯,记录路径,valid 时候记录方案。

  • 相关阅读:
    《机器学习十讲》学习报告七
    找到每个人的任务
    牛客每个人最近的登陆日期
    考试分数(一)
    牛客的课程订单分析(一)
    实习广场投递简历分析(一)
    sql 查找最晚入职员工信息
    sql 学习笔记
    shell 编程获取文件名后缀为特定字符的函数
    im的基本思路
  • 原文地址:https://www.cnblogs.com/FriskyPuppy/p/12907832.html
Copyright © 2011-2022 走看看