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

字符串合并处理

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

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
        String line = in.nextLine().replace(" ", "");
        char[] charList = new char[line.length()];
        List arr1 = new ArrayList<>();
        List arr2 = new ArrayList<>();
        for (int i = 0; i < line.length(); i++) {
            if (i % 2 == 1) {
                arr1.add(line.charAt(i));
                continue;
            }
            if (i % 2 == 0) {
                arr2.add(line.charAt(i));
                continue;
            }
        }
        Collections.sort(arr1);
        Collections.sort(arr2);
        
        for (int i = 0; i < arr2.size(); i++) {
            charList[i * 2] = (char)arr2.get(i);

        }
        for (int i = 0; i < arr1.size(); i++) {
            charList[i * 2 + 1] = (char)arr1.get(i);
        }

        String result = "";
        for (int i = 0; i < charList.length; i++) {
            char c = charList[i];
            String s = String.valueOf(c);
            if ((c >= 'a' && c <= 'f') || (c >= 'A' && c < 'F') || (c >= '1' &&
                    c <= '9')) {
                s = reverse(charList[i]);
            }
            result += s;
        }
        System.out.print(result);

    }
    private static String reverse(char c) {
        // String s = "";
        char[] temp = new char[4];
        String biary = Integer.toBinaryString(Integer.parseInt(String.valueOf(c), 16));
        int len = biary.length();
        for (int i = 0; i < 4 - len; i++) {
            biary = "0" + biary;
        }
        for (int i = biary.length() - 1; i >= 0; i--) {
            temp[3 - i] = biary.charAt(i);
        }
        return Integer.toHexString(Integer.parseInt(String.valueOf(temp), 2)).toUpperCase();

    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
03-28 13:48
hory权:校招vip纯神人了,还说自己是什么师范大学的
点赞 评论 收藏
分享
HaxyBT:那我提前下班总可以了吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务