• 93. 复原 IP 地址


    题目链接:

    力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

    思路:分割

            分割点就是index

            停止条件不知道怎么判断:模仿分割回文串,单独写个函数判断字符是否是合法的

                    合法条件:1不以0开头 2小于255 

    想不出来,手码一边:

    无法运行,报没有insert函数不知道为什么

    1. class Solution {
    2. public:
    3. std::vector result;
    4. vector restoreIpAddresses(string s) {
    5. if(s.size() < 12 && s.size() > 4)
    6. {
    7. ip(s, 0, 0);
    8. return result;
    9. }
    10. return result;
    11. }
    12. void ip(const string& s, int startIndex, int pointNum)
    13. {
    14. if(pointNum == 3)
    15. {
    16. if(isIPsection(s, startIndex, s.size() - 1))
    17. {
    18. result.push_back(s);
    19. }
    20. return;
    21. }
    22. for(int i = startIndex; i < s.size(); i++)
    23. {
    24. if(isIPsection(s, startIndex, i))
    25. {
    26. // s.insert(s.begin() + i + 1, '.');
    27. s.insert(s.begin() + i + 1 , '.');//这里报错没有insert这个函数,不知道为什么
    28. pointNum++;
    29. ip(s, i + 2,pointNum);
    30. pointNum--;
    31. s.erase(s.begin() + i + 1);
    32. }
    33. else
    34. {
    35. break;
    36. }
    37. }
    38. }
    39. bool isIPsection(const string& s, int start, int end)
    40. {
    41. if(start > end) return false;
    42. if(s[[start] == '0'] && start != end) return false;
    43. int num = 0;
    44. for(int i = start, i < end; i++)
    45. {
    46. if(s[i] < '0' || s[i] > '9')
    47. return false;
    48. }
    49. num = num * 10 + (s[i] - '0');
    50. if (num > 255) { // 如果大于255了不合法
    51. return false;
    52. }
    53. return true;
    54. }
    55. };

    正确代码:

    1. class Solution {
    2. private:
    3. vector result;// 记录结果
    4. // startIndex: 搜索的起始位置,pointNum:添加逗点的数量
    5. void backtracking(string& s, int startIndex, int pointNum) {
    6. if (pointNum == 3) { // 逗点数量为3时,分隔结束
    7. // 判断第四段子字符串是否合法,如果合法就放进result中
    8. if (isValid(s, startIndex, s.size() - 1)) {
    9. result.push_back(s);
    10. }
    11. return;
    12. }
    13. for (int i = startIndex; i < s.size(); i++) {
    14. if (isValid(s, startIndex, i)) { // 判断 [startIndex,i] 这个区间的子串是否合法
    15. s.insert(s.begin() + i + 1 , '.'); // 在i的后面插入一个逗点
    16. pointNum++;
    17. backtracking(s, i + 2, pointNum); // 插入逗点之后下一个子串的起始位置为i+2
    18. pointNum--; // 回溯
    19. s.erase(s.begin() + i + 1); // 回溯删掉逗点
    20. } else break; // 不合法,直接结束本层循环
    21. }
    22. }
    23. // 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法
    24. bool isValid(const string& s, int start, int end) {
    25. if (start > end) {
    26. return false;
    27. }
    28. if (s[start] == '0' && start != end) { // 0开头的数字不合法
    29. return false;
    30. }
    31. int num = 0;
    32. for (int i = start; i <= end; i++) {
    33. if (s[i] > '9' || s[i] < '0') { // 遇到非数字字符不合法
    34. return false;
    35. }
    36. num = num * 10 + (s[i] - '0');
    37. if (num > 255) { // 如果大于255了不合法
    38. return false;
    39. }
    40. }
    41. return true;
    42. }
    43. public:
    44. vector restoreIpAddresses(string s) {
    45. result.clear();
    46. if (s.size() < 4 || s.size() > 12) return result; // 算是剪枝了
    47. backtracking(s, 0, 0);
    48. return result;
    49. }
    50. };

  • 相关阅读:
    webpack优化系列五:vue项目配置 webpack-obfuscator 进行代码加密混淆
    RabbitMQ
    下一个十年,什么样的测试最吃香?
    如何配置nginx的转发?
    SpringCloud01
    编译安装MySQL服务(LAMP2)
    带你手写vue3核心源码(九)
    2022-08-25 第五组 张明敏 学习笔记
    【云原生】微服务之Feign的介绍与使用
    Postman的环境变量和全局变量
  • 原文地址:https://blog.csdn.net/weixin_44965579/article/details/133127163