且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LintCode: Unique Characters

更新时间:2022-09-04 23:02:00

C++,

time: O(n^2)

space: O(0)

LintCode: Unique Characters
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;
    }
};
LintCode: Unique Characters

C++,

time: O(n)

space: O(n)

LintCode: Unique Characters
 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 };
LintCode: Unique Characters

 

本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4999745.html,如需转载请自行联系原作者