题解 | #字符串合并处理#

字符串合并处理

https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<Character, String> binMap = new HashMap<Character, String>();
        HashMap<String, Character> hexMap = new HashMap<String, Character>();
        binMap.put('0',"0000");
        binMap.put('1',"1000");
        binMap.put('2',"0100");
        binMap.put('3',"1100");
        binMap.put('4',"0010");
        binMap.put('5',"1010");
        binMap.put('6',"0110");
        binMap.put('7',"1110");
        binMap.put('8',"0001");
        binMap.put('9',"1001");
        binMap.put('A',"0101");
        binMap.put('B',"1101");
        binMap.put('C',"0011");
        binMap.put('D',"1011");
        binMap.put('E',"0111");
        binMap.put('F',"1111");
        hexMap.put("0000",'0');
        hexMap.put("0001",'1');
        hexMap.put("0010",'2');
        hexMap.put("0011",'3');
        hexMap.put("0100",'4');
        hexMap.put("0101",'5');
        hexMap.put("0110",'6');
        hexMap.put("0111",'7');
        hexMap.put("1000",'8');
        hexMap.put("1001",'9');
        hexMap.put("1010",'A');
        hexMap.put("1011",'B');
        hexMap.put("1100",'C');
        hexMap.put("1101",'D');
        hexMap.put("1110",'E');
        hexMap.put("1111",'F');
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.nextLine().replaceAll(" ","");
            char[] odd = new char[str.length()];
            char[] even = new char[str.length()];
            char[] sorted = new char[str.length()];
            for(int i=0, j=0, k=0;i<str.length();i++) {
                if(i%2==0) {
                    even[j]=str.charAt(i);
                    j++;
                } else {
                    odd[k]=str.charAt(i);
                    k++;
                }
            }
            odd = String.valueOf(odd).replaceAll("\\u0000", "").toCharArray();
            even = String.valueOf(even).replaceAll("\\u0000", "").toCharArray();
            for(int i=0;i<odd.length;i++) {
                char temp = odd[i];
                for(int j=i-1;j>=0&&odd[j]>temp;j--) {
                    odd[j+1]=odd[j];
                    odd[j]=temp;
                }
            }
            for(int i=0;i<even.length;i++) {
                char temp = even[i];
                for(int j=i-1;j>=0&&even[j]>temp;j--) {
                    even[j+1]=even[j];
                    even[j]=temp;
                }
            }
            for(int i=0, j=0, k=0;i<str.length();i++) {
                if(i%2==0) {
                    sorted[i]=even[j];
                    j++;
                } else {
                    sorted[i]=odd[k];
                    k++;
                }
            }
            for(int i=0;i<str.length();i++) {
                Character c =  Character.toUpperCase(sorted[i]);
                if((c>='A' && c<='F') || (c>='0' && c<='9')) {
                    String bin = binMap.get(c);
                    sorted[i]=hexMap.get(bin);
                }
            }

            System.out.println(String.valueOf(sorted));
        }
    }

}

全部评论

相关推荐

头像
06-12 10:39
Java
给你们全都来一刀:你了解回暖的核心逻辑吗,读过回暖的源码吗,上线过回暖相关的项目吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务