LintCode: Unique Characters

C++,

time: O(n^2)

space: O(0)

class Solution {
public:
    /**
     * @param str: a string
     * @return: a boolean
     */
    bool isUnique(string &str) {
        // write your code here
        for (int i=0; i<str.size(); i++) {
            for (int j=i+1; j<str.size(); j++) {
                if (str[i] == str[j]) {
                    return false;
                }
            }
        }
        return true;
    }
};

C++,

time: O(n)

space: O(n)

 1 class Solution {
 2 public:
 3     /**
 4      * @param str: a string
 5      * @return: a boolean
 6      */
 7     bool isUnique(string &str) {
 8         // write your code here
 9         string tmp;
10         for (int i=0; i<str.size(); i++) {
11             if (-1 == tmp.find(str[i])) {
12                 tmp.push_back(str[i]);
13             } else {
14                 return false;
15             }
16         }
17         return true;
18     }
19 };

 

posted @ 2015-11-27 09:54  ZH奶酪  阅读(245)  评论(0编辑  收藏  举报