93.复原IP地址

93.复原IP地址

思路

按照昨天分割字符串的套路。

点1:终止条件设为在遍历完整个串时发生,且这道题目需要考虑将分割的子串数,即ip地址为四段,因此需要两个共同条件:遍历整串+子串四段。若只达到其中一个而另一个不满足,则返回。

点2:在for循环分割串时,正常分割从startIndex开始,母串尾结束,这道题需要额外考虑其他三个条件。1.子串长小于3 2.不能由0作为数字开端,0除外 3.子串大小介于0-255

点3:利用StringBuilder构建串时,需要添加删除,删除最后一个网段。

前面两个点在写题时基本考虑到,因为最后一个点一直错。

正确应该书写为:stringBuilder.delete(startIndex+num-1,i+num+1);

我写为了:stringBuilder.delete(stringBuilder.length()-temp.length(),stringBuilder.length());

应该是由stringbuilder全局变量引起的错误

代码

class Solution {
    List<String> result =new ArrayList<>();
    StringBuilder stringBuilder=new StringBuilder();
    int num=0;
    public List<String> restoreIpAddresses(String s) {
        backTracking(s,0);
        return result;
    }
    public void backTracking(String s,int startIndex){

        if (num==4){
            if (startIndex<s.length()){
                return;
            }else {
                result.add(stringBuilder.toString());
                return;
            }
        }

        for (int i=startIndex;i<s.length();i++){
            String temp=s.substring(startIndex,i+1);
            if (temp.length()>3)break;


            if (Integer.parseInt(temp)>=0 && Integer.parseInt(temp)<=255){
                if (temp.length()>1 && temp.charAt(0)-'0'==0) return;
                stringBuilder.append(temp);
                num++;
                if (num<4){
                    stringBuilder.append('.');
                }
                backTracking(s,i+1);

                stringBuilder.delete(startIndex+num-1,i+num+1);

                num--;
            }

        }
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个可能的解法: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LEN 12 int isValid(char* s, int start, int end) { if (start > end) { return 0; } if (s[start] == '0' && start != end) { return 0; } int num = 0; for (int i = start; i <= end; i++) { if (s[i] < '0' || s[i] > '9') { return 0; } num = num * 10 + (s[i] - '0'); if (num > 255) { return 0; } } return 1; } void backtrack(char* s, int len, int start, int depth, char* path, char** res, int* returnSize) { if (depth == 4) { if (start == len) { path[strlen(path) - 1] = '\0'; // 去掉最后一个点 res[*returnSize] = (char*) malloc(sizeof(char) * (strlen(path) + 1)); strcpy(res[*returnSize], path); (*returnSize)++; } return; } for (int i = start; i < start + 3 && i < len; i++) { if (isValid(s, start, i)) { char* temp = (char*) malloc(sizeof(char) * (MAX_LEN + 2)); sprintf(temp, "%s%d.", path, atoi(s + start)); backtrack(s, len, i + 1, depth + 1, temp, res, returnSize); free(temp); } } } char** restoreIpAddresses(char* s, int* returnSize) { int len = strlen(s); char** res = (char**) malloc(sizeof(char*) * 5000); *returnSize = 0; if (len < 4 || len > 12) { return res; } char* path = (char*) malloc(sizeof(char) * (MAX_LEN + 2)); backtrack(s, len, 0, 0, path, res, returnSize); free(path); return res; } int main() { int returnSize; char* s = "25525511135"; char** res = restoreIpAddresses(s, &returnSize); for (int i = 0; i < returnSize; i++) { printf("%s\n", res[i]); free(res[i]); } free(res); return 0; } ``` 解题思路和 Python 版本相同,这里就不再赘述。需要注意的是,在 C 语言中,字符串长度是不包括结尾的空字符 `\0` 的,因此每个字符串的申请空间要比 Python 版本多加 1。同时,由于 C 语言不支持字符串加法,所以要使用 `sprintf` 函数将数字转换为字符串后再拼接。最后,记得释放申请的内存。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值