题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str1 = sc.next(); String str2 = sc.next(); String STR = str1 + str2; Sort(STR); } public static void Sort(String str) { String[] STR = str.split(""); ArrayList<String> list1 = new ArrayList<>(); ArrayList<String> list2 = new ArrayList<>(); for (int i = 0; i < STR.length; i++) { if (i % 2 == 0) { list1.add(STR[i]); } else { list2.add(STR[i]); } } Collections.sort(list1); Collections.sort(list2); ArrayList<String> target = new ArrayList<>(); for (int i = 0, j = 0, f = 0; i < str.length(); i++) { if (i % 2 == 0) { target.add(list1.get(j++)); } else { target.add(list2.get(f++)); } } ChangeStr(target); } public static void ChangeStr(ArrayList<String> target) { StringBuilder SB = new StringBuilder(); for (String s : target) { if (s.matches("[a-f]||[A-F]")) { SB.append(Integer.toHexString(Integer.parseInt(new StringBuffer( Integer.toBinaryString(Integer.parseInt(s, 16))).reverse().toString(), 2)).toUpperCase()); } else if (s.matches("[0-9]")) { SB.append(Integer.toHexString(Integer.parseInt(new StringBuffer("000" + Integer.toBinaryString(Integer.parseInt(s))).reverse().substring(0, 4), 2)).toUpperCase()); } else { SB.append(s); } } System.out.println(SB); } }