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

来源:互联网 发布:仁爱路99号碧格网络 编辑:程序博客网 时间:2024/06/05 11:17

实现一个算法确定字符串中的字符是否均唯一出现
样例
给出”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
原创粉丝点击