首页 > 其他分享 >93. 复原 IP 地址c

93. 复原 IP 地址c

时间:2024-03-09 15:34:08浏览次数:16  
标签:index head return int IP char tail 93 复原

leetcode调试功能真香啊,效率高了不少。但钱包也变扁了。。

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

char temp[30];
int top=0;
bool judge(char* s,int head,int tail){
    if(head >tail) return false;
    if(head==tail){
        int t=s[head]-'0';
        if(t>=0&&t<=9) return true;
        return false;
    }
    if(s[head]=='0') return false;
    int sum=0;
    for(;head<=tail;head++){
        if(s[head]>'9'||s[head]<'0') return false;
        sum=sum*10+s[head]-'0';
    }
    if(sum>255) return false;
    return true ;
}

void dfs(char* s,char** array,int* returnSize,int index,int n,int count,int x){
    if(index>n) return;
    if(x>4) return;
    if(index==n&&x<4) return;
    if(index==n&&x==4){
        array[*returnSize]=(char*)malloc(sizeof(char)*(count+1));
        strcpy(array[*returnSize],temp);
        array[*returnSize][count-1]=0;
        (*returnSize)++;
        return;
    }
    for(int i=0;i<3;i++){
        int tail=index+i;
        if(tail>n) break;
        if(judge(s,index,tail)){
            for(int j=index;j<=tail;j++) temp[top++]=s[j];
            temp[top++]='.';
            dfs(s,array,returnSize,tail+1,n,count+i+2,x+1);
            for(int j=index;j<=tail+1;j++) temp[--top]=0;
        }
    }
}

char** restoreIpAddresses(char* s, int* returnSize) {
    *returnSize=0;
    int n=strlen(s);
    if(n<4)  return NULL;
    char** array=(char**)malloc(sizeof(char*)*400);
    for(int i=0;i<30;i++) temp[i]=0;   
    dfs(s,array,returnSize,0,n,0,0);
    return array;
}

结果:

标签:index,head,return,int,IP,char,tail,93,复原
From: https://www.cnblogs.com/llllmz/p/18062764

相关文章

  • 痞子衡嵌入式:不清i.MXRTxxx里FLEXSPI_MCR0寄存器保留位会造成IP CMD读写异常
    大家好,我是痞子衡,是正经搞技术的痞子。今天痞子衡给大家介绍的是不清i.MXRTxxx里FLEXSPI_MCR0寄存器保留位会造成IPCMD读写异常。痞子衡曾经写过一篇文章《改动i.MXRT1xxx里IOMUXC_GPR寄存器保留位可能会造成系统异常》,这篇文章提出了一个观点,即对于MCU外设寄存器应......
  • 【力扣】复原IP地址(回溯法)(分割问题)
    问题描述在这个题中,因为结果的数据类型为vector<string>所以直接在s中添加分割点比较方便,先看一下代码:classSolution{private:vector<string>result;//记录结果//startIndex:搜索的起始位置,pointNum:添加逗点的数量voidbacktracking(string&s,intst......
  • eclipse编写html
    Windows—>Perspective—>CustommizePerspectiveShortcuts->WebFile->New—>StaticWebProject输入名称自定义文件夹名称创建html文件指定名称选择版本新建文件夹,存放图片......
  • virtual box安装cendos7并配置外网及静态IP(转)
    一、前期准备工作:1、虚拟机下载VirtualBox版本:7.0.6下载VirtualBox的下载页面:https://www.virtualbox.org/wiki/DownloadsVMWare虚拟机软件(收费的,要使用请购买正版软件)的官网:https://www.vmware.comVMWare虚拟机的免费版VMWarePlayer:https://www.vmware.com/products/w......
  • 1938.2024 ICPC Asia Pacific Championship - sol
    20240302-202403082024ICPCAsiaPacificChampionship-OnlineMirror和Mea,Hanghang组队一起打,只做了F,三个人不会G,我又被简单的C搏杀。。。现阶段没有补完,待更新。进度:11/13D和M是多项式题目,一道FFT,一道NTT,由于笔者太菜不会多项式,所以这两道没有补。L是线性......
  • Lua中pair和ipair的区别
    Lua中pair和ipair的区别?二者都是Lua中内置的迭代器,可以对数组或table进行遍历。在正常的数组或table的遍历中,二者没有区别。tableNormal={"this","is","a","array"}--使用pairs遍历forkey,valinpairs(tableNormal)doprint(key,'==',val)end遍历结果:--使用......
  • 蓝牛公网ip检测助手V1.00免费版
        蓝牛公网ip检测助手是一款非常实用的网络软件,可以实时检测公网IP当公网IP变化时软件自动发邮件通知管理员,同时也支持窗口发信息通知管理员(QQ或是微信等)和自定义API提交到你自己网站服务器,让你第一时间掌握公网IP变化情况。 本地下载  ......
  • 0day-seeyonOA-zipslip(win)
    IncorrectdecompressionexistsinseeyonOAv8.Anattackercangainaccesstotheserverthroughzipslipafterobtainingordinaryuserprivileges.RouteAnalyseThevulnerabilityliesinWorkFlowDesignerController.class'simportProcessmethod,whic......
  • macOS Ventura 13.6.5 (22G621) 正式版发布,ISO、IPSW、PKG 下载 (安全更新)
    macOSVentura13.6.5(22G621)正式版发布,ISO、IPSW、PKG下载(安全更新)3月8日凌晨,macOSSonoma14.4发布,同时带来了macOSVentru13.6.5和macOSMonterey12.7.4安全更新。macOSVentura13.6及更新版本,如无特殊说明皆为安全更新,不再赘述。请访问原文链接:https://......
  • JavaScript 打包器esbuild的基础使用
    esbuild是一种类似于webpack的极速JavaScript打包器。esbuild项目主要目标是:开辟一个构建工具性能的新时代,创建一个易用的现代打包器。先安装esbuildnpmiesbuild-g-g代表全局范围检查esbuild的版本esbuild--version命令行构建esbuildsrc\app.jsx--bundle--outfi......