#include <array>
#include <iostream>
class Different {
public:
bool checkDifferent(string iniString) {
// write code here
std::array<int, 128> buf{};
for (auto& ch : iniString) {
buf[ch] += 1;
if (buf[ch] > 1) {
return false;
}
}
return true;
}
};
因为ASCII码共有128个,所有创建一个128长度的数组。随后遍历该字符串,字符会隐式转换为int型,可以当做下标,当前下标的值加1。当再次遇到相同字母时,会使值大于1,则返回false。

京公网安备 11010502036488号