创新工场涂鸦移动2018校园招聘测试题

1写一个算法判断某个字符串是不是一个合法的IP地址。

#include <string.h>
#include <stdlib.h>

#include <iostream>
using namespace std;

bool IsIpaddr(char *ipaddr)
{
    if(ipaddr == NULL)
        return false;

    char *token;
    const char *delim = ".";
    token = strtok(ipaddr,delim);
    if(!token)
    {
        return false;
    }

    while(token)
    {
        int temp = atoi(token);
        if(temp < 0 || temp > 255)
        {
            return false;
        }
        token = strtok(NULL,delim);
    }
    return true;
}

int main()
{
    const int length = 60;
        char ipaddr[length] = "192.168.0.28";

    if(IsIpaddr(ipaddr))
    {
        cout << "is ipaddr format" << endl;
    }
    else
    {
        cout << "is not ipaddr format" << endl;
    }

    return 0;
}

在Linux下,可以借助库函数inet_pton来判断是否为ipv4或者ipv6。

2给定一字符串只包含数字,请写一个算法,找出该字符串中的最长不重复子串(不重复是指子串中每一元素不同于子串中其他元素)
如: “120135435”最长不重复子串为 “201354”

#include <iostream>
#include <cassert>
#include <string>
#include <algorithm>
using namespace std;

void LongestSubstring(string& s)
{
    assert(s.size() != 0);
    int n = s.size();
    int i, j,longest,start;
    for (i = 0; i < n; ++i)
    {
        bool exist[10] = {false};
        exist[s[i]-'0'] = true;
        for (j = i + 1; j < n; ++j)
        {
            if (exist[s[j]-'0'] == false)
            {
                exist[s[j]-'0'] = true;
            }
            else
            {
                if (j - i > longest)
                {
                    longest = j - i;
                    start = i;
                }
                break;
            }
        }
        if ((j == n) && (j - i > longest))
        {
longest = j - i;
start = i;
     }
}
cout<<start<<" "<<longest<<endl;
string temp = s.substr(start,longest);
cout<<temp<<endl;
}

int main()
{
string s("120135435");
LongestSubstring(s);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值