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 };
时间: 2024-08-11 01:35:27

LintCode: Unique Characters的相关文章

[Lintcode 1 easy]Unique Characters

[Problem] Unique Characters Implement an algorithm to determine if a string has all unique characters. Example Given "abc", return true. Given "aab", return false. Challenge What if you can not use additional data structures? ---------

Determine if a string has all unique characters

Problem: Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? <pre name="code" class="cpp">#include<iostream> #include<set> #include<string&

[CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure? 这道题让我们判断一个字符串中是否有重复的字符,要求不用特殊的数据结构,这里应该是指哈希表之类的不让用.像普通的整型数组应该还是能用的,这道题的小技巧就是用整型数组来代替哈希表,在之前Bitwise AND of Numbers Range 数

lintcode 容易题:Unique Characters 判断字符串是否没有重复字符

题目: 判断字符串是否没有重复字符 实现一个算法确定字符串中的字符是否均唯一出现 样例 给出"abc",返回 true 给出"aab",返回 false 挑战 如果不使用额外的存储空间,你的算法该如何改变? 解题: 定义一个集合最简单. Java程序: public class Solution { /** * @param str: a string * @return: a boolean */ public boolean isUnique(String st

LintCode : Unique Paths II

Problem Description: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. Code: public class Solutio

LintCode : Unique Paths

Problem Description: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid

[LintCode] Read Characters From File - Multiple Calls

The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the read4 API, implement the function

[LintCode]unique paths with obstacles

http://www.lintcode.com/zh-cn/problem/unique-paths-ii/ 在上一题的基础上加入了障碍物. 同样可采用递归实现,递推公式不变,但是需要加入对障碍物的判断. 下面是实现的代码: 1 #include <cstring> 2 #include <cmath> 3 class Solution { 4 public: 5 /** 6 * @param obstacleGrid: A list of lists of integers 7

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.

思路: 假设给定字符串用的是ASCII编码,那么总共就只有256个字符,新建一个256个元素的boolean数组, 遍历字符串,将出现的字符在boolean数组所在位置置 1.如果碰到已经置一,表明出现重复字符,返回false. public class IsUniqueChars_1 { public boolean solu(String s) { boolean table[] = new boolean[256]; for (int i = 0; i < s.length(); i++)