LintCode Unique Characters 判断字符串是否没有重复字符
来源:互联网 发布:做淘宝如何提高销量 编辑:程序博客网 时间:2024/06/07 05:14
实现一个算法确定字符串中的字符是否均唯一出现
样例
给出”abc”,返回 true
给出”aab”,返回 false
挑战
如果不使用额外的存储空间,你的算法该如何改变?
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?
未用额外空间,时间复杂度O(n^2)
public class Solution { /** * @param str: a string * @return: a boolean */ public boolean isUnique(String str) { 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; }}
使用额外空间,时间复杂度O(n)
public class Solution { /** * @param str: a string * @return: a boolean */ public boolean isUnique(String str) { Set<Character> set = new HashSet<Character>(); for(int i = 0; i < str.length(); i++) { if(set.contains(str.charAt(i))) return false; set.add(str.charAt(i)); } return true; }}
0 0
- LintCode Unique Characters 判断字符串是否没有重复字符
- LintCode-判断字符串是否没有重复字符
- 【Lintcode】判断字符串是否没有重复字符
- 判断字符串是否没有重复字符-LintCode
- 157.Unique Characters-判断字符串是否没有重复字符(容易题)
- lintcode-简单-判断字符串是否没有重复字符
- Lintcode 容易 python 判断字符串是否没有重复字符
- python_lintcode_685First Unique Number In Stream_157判断字符串是否没有重复字符
- 题目:判断字符串是否没有重复字符
- 判断字符串是否没有重复字符
- LintCode_157_判断字符串是否没有重复字符
- 判断字符串是否没有重复字符
- 判断字符串是否没有重复字符
- 判断字符串是否没有重复字符
- 判断字符串是否没有重复字符
- 判断字符串是否没有重复字符(容易)
- 判断字符串是否没有重复字符
- 判断字符串中是否没有重复的字符
- leetcode[168]:Excel Sheet Column Title
- unity 5 打包apk 手动升级android sdk 5.0 api 21
- win8快捷键大全
- AngularJS简单介绍
- RGB-HSV转换
- LintCode Unique Characters 判断字符串是否没有重复字符
- UVa - 1617 - Laptop
- 获取联系人列表
- android WebView上传文件点击取消后按钮不能再次响应点击的解决办法
- 软件下载 地址存档
- 视频重定向设置
- html页面显示list,生成下拉列表
- IOS 四种保存数据的方式
- 命令生成keystore数字证书