class Solution { public: vector<int> shortestToChar(string S, char C) { vector<int>vc(S.size(),10001); for(int i=0;i < S.size();i++){ if(S[i] == C){ vc[i] = 0; int cnt = 0; //左 for(int j=i-1;j >= 0;j--){ if(S[j] == C) break; vc[j] = min(++cnt,vc[j]); } cnt = 0; //右 for(int j=i+1;j < S.size();j++){ if(S[j] == C) break; vc[j] = min(++cnt,vc[j]); } } } for(int i=0;i < vc.size();i++){ if(vc[i] == 106){ cout << i << " " << S[i-1] << " " << S[i] << " " << S[i+1] << endl; } } return vc; } };
N次遍历