判定字符是否唯一
题目描述
实现一个算法,确定一个字符串 s 的所有字符是否全都不同。
我的解题
class Solution {
public:
bool isUnique(string astr) {
vector<int>record(26, 0);
for(auto s: astr)
{
if(record[s-'a'] ==0) record[s-'a']+=1;
else return false;
}
return true;
}
};
执行用时 :4 ms, 在所有 C++ 提交中击败了61.29%的用户
内存消耗 :8.2 MB, 在所有 C++ 提交中击败了100.00%的用户
如何不使用额外的数据结构?
字符串 *2, 双重循环判断?时间