`
薛定谔
  • 浏览: 22452 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

删除字符串中的重复字符

 
阅读更多

题目:删除一个字符串中重复字符,要求不能使用额外的缓冲区,不能复制字符串,可以使用一两个变量。

 

解答:

 

 

function String removeDuplicates(char[] str){
    if(str != null && str.length > 1){
        for(int i=0; i<str.length-1; i++){
            char c = str[i];
            if(c != 0){
                for(int j=i+1; j<str.length; j++){
                    if(c == str[j]){
                       str[j] = 0;
                    }
                }
            }
        }
    }
    return str;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics