题解 | #字符串合并处理#借鉴了map表,不说G的处理恶心

字符串合并处理

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

// 一时没想到 for 循环遍历 补0的思路, 借鉴了用map表的兄弟,不过只用到了8以下的数据
// 题目不给人说大于G非16进制字母的处理, 坑挺多,够恶心的。
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {

            String mergedStr = mergeStr(str);
            char[] sortStr = sortStr(mergedStr);
            transformArr(sortStr);

            // System.out.println(mergedStr);
        }
    }

    private static char[] sortStr(String mergedStr) {

        int len = mergedStr.length();
        char[] doubleArr = new char[(len + 1) / 2];
        char[] oddArr = new char[(len) / 2];
        for (int i = 0; i < len; i++) {
            if (i % 2 == 0) {
                doubleArr[i / 2] = mergedStr.charAt(i);
            } else {
                oddArr[i / 2] = mergedStr.charAt(i);
            }
        }
        Arrays.sort(doubleArr);
        Arrays.sort(oddArr);

        char[] mergedArr = mergeArr(doubleArr, oddArr, len);

        // System.out.println(Arrays.toString(mergedArr));
        return mergedArr;
    }

    private static char[] mergeArr(char[] doubleArr, char[] oddArr, int len) {

        char[] megerdArr = new char[len];
        for (int i = 0; i < len; i += 2) {
            megerdArr[i] = doubleArr[i / 2];
            if (i + 1 < len) {
                megerdArr[i + 1] = oddArr[i / 2];
            }
        }

        return megerdArr;

    }

    private static String mergeStr(String rawStr) {
        String[] strSplit = rawStr.split(" ");
        String mergedStr = strSplit[0] + strSplit[1];
        return mergedStr;
    }

    public static int convertBinaryToDecimal(String binaryString) {
        int decimalResult = 0;

        for (int i = binaryString.length() - 1; i >= 0; i--) {
            char digit = binaryString.charAt(i);
            int power = binaryString.length() - 1 - i;
            int value = Character.getNumericValue(digit);
            int product = (int) Math.pow(2, power) * value;
            decimalResult += product;
        }

        return decimalResult;
    }

    private static void transformArr(char[] sortStr) {
        // char[] b = Integer.toBinaryString('c' - 'a' + 10).toCharArray();
        char[] result = new char[sortStr.length];

        for (int i = 0; i < sortStr.length; i++) {
            char beforeTransform = sortStr[i];

            // 考虑 数字和 字母
            String binaryStr = "";
            if (Character.isDigit(beforeTransform)) {
                // binaryStr = Integer.toBinaryString(beforeTransform - 48);
                if (beforeTransform - '0' > 7) {
                    binaryStr = Integer.toBinaryString(beforeTransform - 48);
                } else {
                    binaryStr = String.valueOf(getBinaryStr(beforeTransform));
                }
            } else {
                if (Character.toUpperCase(beforeTransform) - 'A' >= 6) {
                    System.out.print(beforeTransform);
                    continue;
                } else {
                    binaryStr = Integer.toBinaryString((Character.toUpperCase(
                                                            beforeTransform) - 'A' + 10));
                }
            }

            String reversedBinary = "";
            for (int j = binaryStr.length() - 1; j >= 0; j--) {
                reversedBinary += binaryStr.charAt(j);
            }

            char res;
            int decimalDigt = convertBinaryToDecimal(reversedBinary);
            if (decimalDigt > 9) {
                reversedBinary = Integer.toHexString(decimalDigt);
                System.out.print(reversedBinary.toUpperCase());
            } else {
                System.out.print(decimalDigt);
            }

        }
    }

    private static char getBinaryStr(char c) {

        Map<Character, Character> map = new HashMap<Character, Character>() {
            {
                put('0', '0');
                put('1', '8');
                put('2', '4');
                put('3', 'C');
                put('4', '2');
                put('5', 'A');
                put('6', '6');
                put('7', 'E');
                // put('8', '1');
                // put('9', '9');
                // put('a', '5');
                // put('b', 'D');
                // put('c', '3');
                // put('d', 'B');
                // put('e', '7');
                // put('f', 'F');
                // put('A', '5');
                // put('B', 'D');
                // put('C', '3');
                // put('D', 'B');
                // put('E', '7');
                // put('F', 'F');
            }
        };

        return map.get(c);
    }
}

全部评论

相关推荐

比亚迪汽车新技术研究院 硬件工程师 总包21左右 硕士
点赞 评论 收藏
分享
我也曾抱有希望:说的好直白
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务