面试题 01.01. Is Unique LCCI

Problem

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

Example1

Input: s = “leetcode”
Output: false

Example2

Input: s = “abc”
Output: true

Solution

哈希表或排序。

class Solution {
public:
    bool isUnique(string astr) {
        if(astr.empty())
            return true;
        sort(astr.begin(),astr.end());
        for(int i = 0;i<astr.size() - 1;++i)
        {
            if(astr[i] == astr[i+1])
                return false;
        }
        return true;
    }
};
发布了526 篇原创文章 · 获赞 215 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/sjt091110317/article/details/105060945