leetcode821

vector<int> shortestToChar(string S, char C) {
    vector<int> V;

    const int N = 10001;
    int AYC[N];
    int countC = 0;
    for (int i = 0; i < S.size(); i++)
    {
        if (S[i] == C)
        {
            AYC[countC] = i;
            countC++;
        }
    }
    for (int i = 0; i < S.size(); i++)
    {
        int min = INT_MAX;
        for (int j = 0; j < countC; j++)
        {
            int dif = abs(i - AYC[j]);
            if (dif < min)
            {
                min = dif;
            }
        }
        V.push_back(min);
    }
    return V;
}

猜你喜欢

转载自www.cnblogs.com/asenyang/p/9715987.html