该函数接受字母数组。应返回不重复的字母数。也就是说,它们在数组中仅出现 1 次。您需要为解决方案使用更少的额外内存。现在,每次字母不匹配时,我都会显示并增加计数器。我不知道如何做到这一点以使该功能正常工作
int isCharacterUnique(const char* str)
{
const int size = strlen(str);
int count = 0;
for (int index = 0; index < size; ++index)
{
for (int j = index + 1; j < size; ++j)
{
if (str[index] != str[j])
{
count += 1;
}
}
}
return count;
}
主要我会这样写
const char* str = "abcacwqa";
std::cout << "isCharacterUnique: " << isCharacterUnique(str);
在控制台中:isCharacterUnique:24
例如,像这样:
超级大脑的选项)))
如果您节省内存(以性能为代价),那么根据您的解决方案,您可以执行以下操作:(检查)