题解 | #字符串合并处理#
字符串合并处理
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);
while (in.hasNextLine()) {
String lineStr = in.nextLine();
// 第一步:将输入的两个字符串str1和str2进行前后合并。如给定字符串 "dec" 和字符串 "fab" , 合并后生成的字符串为 "decfab"
String mergerStr = lineStr.trim().replaceAll(" ", "");
// 第二步:对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。
// 这里的下标的意思是字符在字符串中的位置。注意排序后在新串中仍需要保持原来的奇偶性。例如刚刚得到的字符串“decfab”,
// 分别对下标为偶数的字符'd'、'c'、'a'和下标为奇数的字符'e'、'f'、'b'进行排序(生成 'a'、'c'、'd' 和 'b' 、'e' 、'f'),
// 再依次分别放回原串中的偶数位和奇数位,新字符串变为“abcedf”
char[] chs = mergerStr.toCharArray();
// 需要注意大小写
List<Character> oddIndexList = new ArrayList<>();
List<Character> evenIndexList = new ArrayList<>();
for (int i = 0; i < chs.length; i++) {
if (i % 2 == 0) {
evenIndexList.add(chs[i]);
} else {
oddIndexList.add(chs[i]);
}
}
// 分别排查
Collections.sort(oddIndexList);
Collections.sort(evenIndexList);
// 再合并
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chs.length; i++) {
if (i % 2 == 0) {
sb.append(evenIndexList.remove(0));
} else {
sb.append(oddIndexList.remove(0));
}
}
// 第三步:对排序后的字符串中的'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"。
char[] sortChs = sb.toString().toCharArray();
sb = new StringBuilder();
for (int i = 0; i < sortChs.length; i++) {
int ti = 0;
try {
ti = Integer.parseInt(String.valueOf(sortChs[i]).toUpperCase(), 16);
} catch (NumberFormatException e) {
sb.append(sortChs[i]);
continue;
}
String hexStr = Integer.toString(ti, 2);
StringBuilder hexStrSb = new StringBuilder();
if (hexStr.length() < 4) {
for (int j = 0; j < 4 - hexStr.length(); j++) {
hexStrSb.append("0");
}
hexStrSb.append(hexStr);
hexStr = hexStrSb.toString();
}
char[] hexChs = hexStr.toCharArray();
StringBuilder hexSb = new StringBuilder();
for (int j = hexChs.length - 1; j >= 0; j--) {
hexSb.append(hexChs[j]);
}
int reverseI = Integer.parseInt(hexSb.toString(), 2);
sb.append(Integer.toHexString(reverseI).toUpperCase());
}
System.out.println(sb);
}
}
}


