Java字符串題目--去除重複字符並排序

去除重複字符並排序:

輸入:              字符串
輸出:              去除重複字符並排序的字符串
樣例輸入:       aabcdefff
樣例輸出:       abcdef


統計出現的過的字符串,並將相應的數組位置爲1,然後遍歷數組,爲1的將對應的字符輸出。


package com.huawei;

import java.util.Scanner;

/**
 * Created by A5313 on 2015/8/10.
 */
public class Norepeat {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("輸入任意字符串:");
        String input = sc.nextLine();
        System.out.println(noRepeat(input));
    }

    public static String noRepeat(String str){
        char[] chars = new char[255];
        char[] input = str.toCharArray();

        int temp;
        for(int i = 0;i< input.length;i++){
            temp = input[i];
            if(chars[temp] == 0)
                chars[temp] = 1;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i] == 1)
                sb.append((char)i);
        }
        return sb.toString();
    }
}


發佈了38 篇原創文章 · 獲贊 11 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章