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

来源:互联网 发布:windows xp系统重装 编辑:程序博客网 时间:2024/05/22 09:03
class Solution {public:    /**     * @param str: a string     * @return: a boolean     */    bool isUnique(string &str) {        // write your code here        int ch[128] = {0};        for (int i=0; i<str.length(); ++i)             if (ch[str[i]]!=0) return false;            else ch[str[i]]=1;        return true;    }};

1. int ch[128] = {0};表示初始化一个大小为128的数组,且每个元素都是0!

2. 我们可以用ch[str[i]]!=0 是因为每个字符都有对应的ASCII码和其对应的十进位数字。

0 0
原创粉丝点击