Java機試題:字符串按照指定規則排序


描述

編寫一個程序,將輸入字符串中的字符按如下規則排序。

規則 1 :英文字母從 A 到 Z 排列,不區分大小寫。
如,輸入: Type 輸出: epTy

規則 2 :同一個英文字母的大小寫同時存在時,按照輸入順序排列。
如,輸入: BabA 輸出: aABb

規則 3 :非英文字母的其它字符保持原來的位置。

如,輸入: By?e 輸出: Be?y

思路

1.將輸入的字符串拆分為兩類,字母類和非字母類。

2.字母類進行忽略大小寫類型進行排序,用於保證同一個英文字母的大小寫同時存在時,按照輸入順序排列。

3.對於非字母類的字符,需要記錄原始的位置。

4.將排序好的字母類和記錄了非字母類位置的兩部分合並,輸出結果。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

import javax.script.ScriptEngineManager;

public class Main {
    
    public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
     while(sc.hasNextLine()) {
         String str = sc.nextLine();
         // 字母類型的集合
         List<CharType> charTypes = new ArrayList<CharType>();
         // 非字母類型的集合
         List<CharType> charNoEns = new ArrayList<CharType>();
         for (int i = 0; i < str.length(); i++) {
            String charInfo = str.substring(i,i+1);
            CharType charType = new CharType();
            charType.setCharInfo(charInfo);
            charType.setPosition(i);
            if(charInfo.matches("[A-Za-z]")) {
                charTypes.add(charType);
            } else {
                charNoEns.add(charType);
            }
            
        }
        // 字符串排序,比較大小,忽略大小寫,同字母大小寫保持原來前后順序
        Collections.sort(charTypes,new Comparator<CharType>(){
            @Override
            public int compare(CharType o1, CharType o2) {
                return o1.getCharInfo().compareToIgnoreCase(o2.getCharInfo());
            } 
        });
        
        // 結果字符串
        StringBuffer strs = new StringBuffer();
        CharType charNoEn = new CharType();
        int charTypeCount = 0;
        
        // 結果集合長度為總長度
        for (int i = 0; i < charTypes.size() + charNoEns.size(); i++) {
            // 判斷當前位置是否有非字母類型
            final int curPos = i;
            charNoEn = charNoEns.stream().filter(o-> o.getPosition() == curPos).findFirst().orElse(null);
            // 有非字母,保持原有位置
            if(charNoEn != null) {
                strs.append(charNoEn.getCharInfo());
            } else {
                // 字母。則使用排序后的位置,需注意的是:需要使用計數變量來
                strs.append(charTypes.get(charTypeCount).getCharInfo());
                charTypeCount++;
            }
        }
        System.out.println(strs.toString());
        
     }
    
    }
    
    public static class CharType {
        private String charInfo; // 字符

        private int position; // 輸入時的位置

        public String getCharInfo() {
            return charInfo;
        }

        public void setCharInfo(String charInfo) {
            this.charInfo = charInfo;
        }

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }
    }
    
}

 題目來源:牛客網


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM