48:最长不含重复字符的子字符串

int longestdis(string str)
{
    int *position = new int[str.length()];
    fill(position, position + str.length(), -1);
    int curlength = 0, maxlength = 0;
    for (int i = 1; i < str.length(); i++)
    {
        int now = str[i] - 'a';
        if (position[now] == -1)curlength += 1;
        if (i - position[now] > curlength)curlength += 1;
        else if (position[now] != -1 && i - position[now] <= curlength)
            maxlength=max(maxlength,curlength),curlength = i - position[now];
        position[now] = i;
        maxlength = max(curlength, maxlength);
    }
    delete[]position;
    return maxlength;
}

你可能感兴趣的:(48:最长不含重复字符的子字符串)