lintcode-157-判断字符串是否没有重复字符

157-判断字符串是否没有重复字符

实现一个算法确定字符串中的字符是否均唯一出现

样例

给出"abc",返回 true
给出"aab",返回 false

挑战

如果不使用额外的存储空间,你的算法该如何改变?

标签

数组 字符串处理 Cracking The Coding Interview

思路

由于题目要求不采取额外的空间,所以使用二重遍历,时间复杂度是O(n^2)

code

class Solution {
public:
    /**
     * @param str: a string
     * @return: a boolean
     */
    bool isUnique(string &str) {
        // write your code here
        int size = str.size();
        if (size <= 0) {
            return true;
        }
        for (int i = 0; i < size - 1; i++) {
            for (int j = i + 1; j < size; j++) {
                if (str[i] == str[j]) {
                    return false;
                }
            }
        }
        return true;
    }
};
原文地址:https://www.cnblogs.com/libaoquan/p/7262523.html