实现一个算法确定字符串中的字符是否均唯一出现
样例
给出”abc”,返回 true
给出”aab”,返回 false
挑战
如果不使用额外的存储空间,你的算法该如何改变?
#ifndef C157_H
#define C157_H
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
/**
* @param str: a string
* @return: a boolean
*/
bool isUnique(string &str) {
// write your code here
if (str.empty())
return 0;
int len = str.size();
for (int i = 0; i < len; ++i)
{
for (int j = i + 1; j < len; ++j)
{
if (str[i] == str[j])
{
return false;
break;
}
}
}
return true;
}
};
#endif