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 };

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2021-07-28
  • 2021-11-02
  • 2021-11-11
  • 2021-10-07
  • 2021-08-21
猜你喜欢
  • 2021-06-16
  • 2022-12-23
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
相关资源
相似解决方案