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

字符串合并处理

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Scanner;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String nextLine = in.nextLine();
            if (Objects.isNull(nextLine) || nextLine.equals("")) {
                break;
            }
            String[] s = nextLine.split(" ");
            String str = s[0] + s[1];
            List<String> strings = str.chars()
                .mapToObj(item -> "" + (char) item)
                .collect(Collectors.toList());
            List<String> strings1 = new ArrayList<>();
            List<String> strings2 = new ArrayList<>();
            for (int i = 0; i < strings.size(); i++) {
                if (i % 2 == 0) {
                    strings1.add(strings.get(i));
                } else {
                    strings2.add(strings.get(i));
                }
            }
            strings1 = strings1.stream()
                .map(item -> item.charAt(0))
                .sorted(Comparator.comparingInt(Character::charValue))
                .map(item -> item + "")
                .collect(Collectors.toList());
            strings2 = strings2.stream()
                .map(item -> item.charAt(0))
                .sorted(Comparator.comparingInt(Character::charValue))
                .map(item -> item + "")
                .collect(Collectors.toList());
            String regex = "[0-9A-Fa-f]";
            strings1 = strings1.stream()
                .map(item -> getString(regex, item))
                .collect(Collectors.toList());
            strings2 = strings2.stream()
                .map(item -> getString(regex, item))
                .collect(Collectors.toList());
            String result = "";
            Map<Integer,String> map1=new HashMap<>();
            for (int i = 0; i < strings1.size(); i++) {
                map1.put(i*2,strings1.get(i));
            }
            Map<Integer,String> map2=new HashMap<>();
            for (int i = 1; i <= strings2.size(); i++) {
                map2.put(i*2-1,strings2.get(i-1));
            }
            map1.putAll(map2);
            System.out.println(map1.entrySet()
                .stream()
                .sorted(Comparator.comparingInt(Entry::getKey))
                .map(item -> item.getValue())
                .collect(Collectors.joining()));

        }
    }

    private static String getString(String regex, String item) {
        // 对排序后的字符串中的'0'~'9'、'A'~'F'和'a'~'f'字符,需要进行转换操作。
        // 转换规则如下:
        // 对以上需要进行转换的字符所代表的十六进制用二进制表示并倒序,然后再转换成对应的十六进制大写字符(注:字符 a~f 的十六进制对应十进制的10~15,大写同理)。
        // 如字符 '4',其二进制为 0100 ,则翻转后为 0010 ,也就是 2 。转换后的字符为 '2'。
        // 如字符 ‘7’,其二进制为 0111 ,则翻转后为 1110 ,对应的十进制是14,转换为十六进制的大写字母为 'E'。
        // 如字符 'C',代表的十进制是 12 ,其二进制为 1100 ,则翻转后为 0011,也就是3。转换后的字符是 '3'。
        // 根据这个转换规则,由第二步生成的字符串 “abcedf” 转换后会生成字符串 "5D37BF"。
        if (item.matches(regex)) {
            String a = Integer.toBinaryString(Integer.parseInt(item, 16));
            while (a.length()<=3){
            a="0"+a;
        }
            List<String> collect = a.chars()
                .mapToObj(item1 -> "" + (char) item1)
                .collect(Collectors.toList());
            Collections.reverse(collect);
            // System.out.println(a);
            // System.out.println(String.join("",collect));
            String upperCase = Integer.toHexString(Integer.parseInt(String.join("", collect), 2))
                .toUpperCase();
            // System.out.println(upperCase);
            return upperCase;
        }
        return item;
    }

}

全部评论
就是一个优秀怎得了啊,楼主
点赞 回复 分享
发布于 2023-06-01 10:00 黑龙江
楼主这种优秀还爱分享的大佬就是我的方向啊
点赞 回复 分享
发布于 2023-06-01 09:45 陕西

相关推荐

纯真的河老师在喝茶:第一个是这个时间点岗位少,第二个是这个简历重复度太高了,10个有9个简历差不多的
点赞 评论 收藏
分享
迷茫的大四🐶:看来已经准备换人了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务