93. 澶嶅師IP鍦板潃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了93. 澶嶅師IP鍦板潃相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/size' title='size'>size   闅惧害   ref   娆℃暟   tco   16px   code   emoji   瀛楃   

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
 #define MAX_IP_ADR 1000
 #if 0
 int IsValid(char* s, int i, int j)
{
    int num = 0;
    int k;
    if (j - i > 3) {
        return -1;
    }
    // 涓嶅厑璁?寮€澶?/span>
    for (k = i; k < j; k++) {
        num = num * 10 + (s[k] - 鈥?/span>0鈥?/span>);
    }
    if ((num != 0 && s[i] == 鈥?/span>0鈥?/span>) || (num == 0 && (j - i) > 1)) {
        return -1;
    }
    if (num >= 0 && num <= 255) {
        //printf("the num is %d
", num);
        return num;
    }
    return -1;
}
#endif
#if 1
 int IsValid(char *s,int start,int end){
     int len=strlen(s);
     int tmp,num=0;
    // 涓嶅彲鑳芥槸鍥涗綅鏁?/span>
     if(end-start>3){
         return -1;
     }
   
     for(int i=start;i<end;i++){
         
         num=num*10+s[i]-鈥?/span>0鈥?/span>;
     }
     //涓嶈兘鍑虹幇08.07绫讳技鎯呭喌锛屽悗闈㈡槸000
     if(((num!=0)&&(s[start]-鈥?/span>0鈥?/span>==0))||((num == 0 && (end - start) > 1))){
         return -1;
     }
    
     if((num>255)||(num<0)){
         
         return -1;
     }
     return num;

 }
 #endif
char ** restoreIpAddresses(char * s, int* returnSize){
    int len=strlen(s);
    
    /* "xxx.xxx.xxx.xxx" */
    int ipaddress[1000][4]={0};
    int cnt=0;
    for(int i=1;i<=3;i++){
        for(int j=i+1;j<len;j++){
            for(int k=j+1;k<len;k++){
                //printf("i%d j%d k%d
",i,j,k);
                if(((ipaddress[cnt][0]=IsValid(s,0,i))!=-1)&&((ipaddress[cnt][1]=IsValid(s,i,j))!=-1)&&(((ipaddress[cnt][2]=IsValid(s,j,k))!=-1)&&((ipaddress[cnt][3]=IsValid(s,k,len))!=-1))){
                    cnt++;
                }
                
            }

        }
    }
    //printf("%d/r/n",cnt);
    char **ipadr=(char **)malloc(sizeof(char *)*cnt);
    for(int i=0;i<cnt;i++){
        ipadr[i]=(char *)malloc(sizeof(char)*16);
        //printf("%d %d %d %d
",ipaddress[i][0],ipaddress[i][1],ipaddress[i][2],ipaddress[i][3]);
       sprintf(ipadr[i],"%d.%d.%d.%d",ipaddress[i][0],ipaddress[i][1],ipaddress[i][2],ipaddress[i][3]); 
    }
    *returnSize=cnt;
    return ipadr;
}

//瑕佽€冭檻sprintf鐨勪娇鐢?/span>

 

93. 澶嶅師IP鍦板潃

闅惧害涓瓑

缁欏畾涓€涓彧鍖呭惈鏁板瓧鐨勫瓧绗︿覆锛屽鍘熷畠骞惰繑鍥炴墍鏈夊彲鑳界殑 IP 鍦板潃鏍煎紡銆?/p>

鏈夋晥鐨?IP 鍦板潃 姝eソ鐢卞洓涓暣鏁帮紙姣忎釜鏁存暟浣嶄簬 0 鍒?255 涔嬮棿缁勬垚锛屼笖涓嶈兘鍚湁鍓嶅 0锛夛紝鏁存暟涔嬮棿鐢?nbsp;鈥?鈥?nbsp;鍒嗛殧銆?/p>

渚嬪锛?0.1.2.201" 鍜?"192.168.1.1" 鏄?nbsp;鏈夋晥鐨?/strong> IP 鍦板潃锛屼絾鏄?"0.011.255.245"銆?192.168.1.312" 鍜?"192.168@1.1" 鏄?nbsp;鏃犳晥鐨?/strong> IP 鍦板潃銆?/p>

 

绀轰緥 1锛?/strong>

杈撳叆锛?/strong>s = "25525511135"
杈撳嚭锛?/strong>["255.255.11.135","255.255.111.35"]

绀轰緥 2锛?/strong>

杈撳叆锛?/strong>s = "0000"
杈撳嚭锛?/strong>["0.0.0.0"]

绀轰緥 3锛?/strong>

杈撳叆锛?/strong>s = "1111"
杈撳嚭锛?/strong>["1.1.1.1"]

绀轰緥 4锛?/strong>

杈撳叆锛?/strong>s = "010010"
杈撳嚭锛?/strong>["0.10.0.10","0.100.1.0"]

绀轰緥 5锛?/strong>

杈撳叆锛?/strong>s = "101023"
杈撳嚭锛?/strong>["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

 

鎻愮ず锛?/strong>

  • 0 <= s.length <= 3000
  • s 浠呯敱鏁板瓧缁勬垚
閫氳繃娆℃暟89,272
 
鎻愪氦娆℃暟175,877
 
 
 

以上是关于93. 澶嶅師IP鍦板潃的主要内容,如果未能解决你的问题,请参考以下文章

eNSP鈥斺€旈厤缃叏灞€鍦板潃姹犵殑DHCP

Centos7娌℃湁IP鍦板潃锛屾煡鐪嬬綉缁滅姸鎬佹樉绀篘o suitable device found for this connection (devint match))

淇濊瘉Linux绯荤粺瀹夊叏涔嬮厤缃甪irewalld闃茬伀澧欑殑鍦板潃浼鍙婄鍙h浆鍙戝疄渚嬶紝鍙窡鍋氾紒锛侊紒

web寮€鍙戜竴浜涜祫婧愮殑鍦ㄧ嚎寮曠敤鍦板潃

logstash甯哥敤鎻掍欢瑙f瀽

memcache瀹夎杩囩▼