unique-characters

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

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

 

您在真实的面试中是否遇到过这个题? 

Yes
样例

给出"abc",返回 true

给出"aab",返回 false

挑战

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

没有特别的难度,需要掌握的知识点是JAVA中string的相关函数比如spilt,toCharArray等

 1 public class Solution {
 2     /**
 3      * @param str: a string
 4      * @return: a boolean
 5      */
 6     public boolean isUnique(String str) {
 7         // write your code here
 8         char[] strchar = str.toCharArray();//将字符串转为数组
 9         int len = strchar.length;
10         for(int i = 0;i < len ;i ++ ){
11             for(int j = i+1 ;j < len ; j++ ){
12                 if(strchar[i]==strchar[j]){
13                     return false;
14                 }
15             }
16         }
17         return true;
18     }
19 }

 

posted @ 2015-11-17 03:51  码代码的banana  阅读(298)  评论(0编辑  收藏  举报