資料結構›Ch1 演算法基礎第 22 題/共 57 題
22. Permutation、Algorithm Correctness、Time Complexity
#DS-01-022中PermutationAlgorithm CorrectnessTime Complexity
(6%) This problem considers permutations of . Let be a permutation from . can be represented as a 0-1 string of length , where:
- if , and
- if .
For example, a permutation can be represented as a string 110. Note that this representation is not one to one, i.e., different permutations may be represented as the same string. Given a 0-1 string , we are interested in reconstructing a possible permutation .
Consider the following C++ procedure.
vector<int> findPermutation(string S) {
int left = count(S.begin(), S.end(), '0'), right = left;
vector<int> res = {left};
for (char &c : S)
res.push_back(c == '1' ? ++right : --left);
return res;
}
Which of the following statements is/are correct?
📄 交大114
▤完整推導請見《WH 資工筆記 · 資料結構》Ch1 演算法基礎