题目:

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

实现一个算法确定字符串中的字符是否均唯一出现

样例

给出"abc",返回 true

给出"aab",返回 false

挑战

如果不使用额外的存储空间,你的算法该如何改变?

解题:

定义一个集合最简单。

Java程序:

public class Solution {
/**
* @param str: a string
* @return: a boolean
*/
public boolean isUnique(String str) {
// write your code here
TreeSet set = new TreeSet();
for(int i=0;i<str.length();i++)
if(set.add(str.charAt(i))==false)
return false;
return true;
}
}

总耗时: 2209 ms

Java程序:

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

总耗时: 1095 ms

这样应该不算额外存储空间吧,时间复杂度O(n2)

Python程序:

利用字典

class Solution:
# @param s: a string
# @return: a boolean
def isUnique(self, str):
# write your code here
d = {}
for s in str:
if s not in d:
d[s] = 1
else:
return False
return True

总耗时: 255 ms

lintcode:Unique Characters 判断字符串是否没有重复字符的更多相关文章

  1. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  2. JAVA----编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符

    package com.pb.demo.packclass.demo1; import java.util.HashSet; /** * 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符 ...

  3. c# 过滤字符串中的重复字符

    有字符串"a,s,d,v,a,v",如果想去除其中重复的字符,怎么做? 下面是一个方法,用Hashtable来记录唯一字符,排除重复字符,仅供参考. 1.过滤方法: public ...

  4. C#中判断字符串中包含某个字符

    C#判断字符串是否存在某个字符,如果存在进行替换.   //定义一个字符串 string  str=".net/Java/asp.net"; //检验“/” if(str.Cont ...

  5. 三种java 去掉字符串中的重复字符函数

    三种java 去掉字符串中的重复字符函数 public static void main(string[] args) { system.out.println(removerepeatedchar( ...

  6. 检测传入字符串是否存在重复字符,返回boolean

    检测传入字符串是否存在重复字符,返回boolean,比如"abc"返回true:"aac"返回false 这里提供两种思路: 第一种: import java. ...

  7. Java:判断字符串中包含某字符的个数

    Java:判断字符串中包含某字符的个数 JAVA中查询一个词在内容中出现的次数: public int getCount(String str,String key){ if(str == null ...

  8. c# 数组 字符串 C#中判断字符串中包含某个字符

    string str = "1,2,3,4,5,6,7";            string[] strArray = str.Split(','); //字符串转数组      ...

  9. 【转载】C#通过StartWith和EndWith方法判断字符串是否以特定字符开始或者结束

    C#开发过程中针对字符串String类型的操作是常见操作,有时候业务需要判断某个字符串是否以特定字符开头或者特定字符结束,此时就可使用StartsWith方法来判断目标字符串是否以特定字符串开头,通过 ...

随机推荐

  1. c#获取今天星期几

    System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek)

  2. phpStudy 2016 更新下载,新版支持php7.0

    目标:让天下没有难配的php环境. phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 『软件简介』该程序包集成 ...

  3. IIS支持PHP

    1. 解压php-5.2.6.zip到D:\php5,找到php.ini-dist改名为php.ini并将它放到C:\WINDOWS目录下. 2. 将D:\ php5目录下的libmcrypt.dll ...

  4. ibatis.net demo

    1. download ibatis.nethttps://code.google.com/p/mybatisnet/ 2. add all dll as reference to your proj ...

  5. JDBC增删改查

    /* db.properties的配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/day14 username=root ...

  6. Django 学习笔记之五 Django中数据库中ManyToManyField及ForeignKey

    1.model里面的代码: from __future__ import unicode_literalsimport django.utils.timezone as timezonefrom dj ...

  7. C#WinForm中在dataGridView中添加中文表头

    第一步: 注意事项:(1)如果使用数据库,那么第三步的名称可以是任意的,但是不能和数据库中的列名一样,否则会报错:    (2)第四步的页眉文本就是你想用的中文列名,自己定: (3)第六步尤其重要,不 ...

  8. C# abstract function VS virtual function?

    An abstract function has to be overridden while a virtual function may be overridden. Virtual functi ...

  9. C# 连接 Oracle 的几种方式[转]

    本文转自:http://www.cnblogs.com/storys/archive/2013/03/06/2945914.html 一:通过System.Data.OracleClient(需要安装 ...

  10. oracle——merge

      一.概述 使用merge声明从一个或者更多个表或视图中筛选记录,以用来更新或者插入到一个表或视图中.你可以指定条件以决定是执行update操作还是insert操作到目标表或视图中. 这个声明是一个 ...