描述
实现一个算法确定字符串中的字符是否均唯一出现
您在真实的面试中是否遇到过这个题?
是
样例
给出"abc"
,返回 true
给出"aab"
,返回 false
挑战
如果不使用额外的存储空间,你的算法该如何改变?
不使用额外的存储空间,最简单的想法就是两次循环。
class Solution {
public:
/*
* @param str: A string
* @return: a boolean
*/
bool isUnique(string &str) {
// write your code here
for(int i=0;i<str.length();i++){
for(int j=i+1;j<str.length();j++){
if(str[i]==str[j]) return false;
}
}
return true;
}
};