Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Реализуйте функцию определяющую все ли символы неповторяющиеся в заданной строке
map<char,int> histogram ну и дальше вы понимаете.bool are_unique_characters(const std::string& str)
{
// assume all chars are unique by default
bool result = true;
// flags for found chars, where index is char's ASCII code
std::vector<bool> char_set(256, false);
// iterate through string and set flags for found chars to true
for (int i = 0; i < str.size() && result; ++i)
{
// get ASCII code for i-th char
int val = str.at(i);
// get flag at val-th index
result = !char_set[val];
// set flag at val-th index to true
char_set[val] = true;
}
return result;
}
Use of break eliminates the possibility of treating a loop as a black box. Limiting yourself to only one statement to control a loop’s exit condition is a powerful way to simplify your loops. Using a break forces the person reading your code to look inside the loop for an understanding of the loop control. That makes the loop more difficult to understand. Use break only after you have considered the alternatives. To paraphrase the nineteenth-century Danish philosopher Søren Kierkegaard, you don’t know with certainty whether continue and break are virtuous or evil constructs. Some computer scientists argue that they are a legitimate technique in structured programming; some argue that they are not. Because you don’t know in general whether continue and break are right or wrong, use them, but only with a fear you might be wrong. It really is a simple proposition: If you can’t defend a break or a continue, don’t use it.
Для размышления — классическая задача: «Переверните C-строку (имеется в виду, что “abcd” – это пять символов, пятый – завершающий нуль-символ ‘\0’)».
Сравните ваше решение с этим и ответьте на вопрос, почему ваш код лучше?
Скромное руководство по прохождению интервью: часть 1