题目:

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

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

样例

给出"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;
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
  • 2021-12-17
  • 2022-12-23
猜你喜欢
  • 2021-09-20
  • 2022-02-15
  • 2021-07-28
  • 2022-12-23
  • 2021-08-17
  • 2021-08-09
  • 2022-12-23
相关资源
相似解决方案