ITeye博客

删除字符串中的重复字符

薛定谔 · 2013-07-10 22:25 · algorithm · 编程语言 · Markdown

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

 

解答:

 

 

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;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。