我对C++中的“!=”运算符有问题。我已经对函数中出现这个错误的区域进行了注释,但我找不到解决这个问题的方法。 谁能告诉我这里还有什么可以用的?
int lengthOfLongestSubstring(string s) {
int n = s.size();
string unique[27];
int length = 0;
for (int i = 0; i <27; i++)
{
unique[i] = "-1";
}
for (int i = 0; i <n; i++)
{
for (int j = 0; j <27; j++)
{
if (unique[j] != s[i]) //getting error here
{
if (unique[j] == "-1")
{
unique[j] = s[i];
length++;
}
}
else
{
return length;
}
}
}
return length;
}
void main()
{
string s = "wilnmmmp";
int n = lengthOfLongestSubstring(s);
cout << n << endl;
system("pause");
}
您正在尝试将字符串数组内容(唯一)与字符串的字符进行比较。 另外,如果您将唯一数组更改为char,它仍然会给出一个错误,因为“-1”是一个多字符,请尝试分配类似“0”或一个正数,并将唯一数组更改为char。 为了方便。 你可以看看下面我的代码。 我的实现