题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String str1 = scanner.next(); String str2 = scanner.next(); String result = control1(str1, str2); result = control2(result); result = control3(result); System.out.println(result); } } private static String control1(String str1, String str2) { return str1+str2; } private static String control2(String result) { List<Character> list1 = new ArrayList<>(); List<Character> list2 = new ArrayList<>(); for (int i = 0; i < result.length(); i++) { if (i % 2 == 0) { list1.add(result.charAt(i)); } else { list2.add(result.charAt(i)); } } Collections.sort(list1); Collections.sort(list2); StringBuffer sb = new StringBuffer(); while (true) { if (!list1.isEmpty()) { sb.append(list1.get(0)); list1.remove(0); } if (!list2.isEmpty()) { sb.append(list2.get(0)); list2.remove(0); } if (list1.isEmpty() && list2.isEmpty()) { break; } } return sb.toString(); } private static String control3(String result) { StringBuffer result2 = new StringBuffer(); for (int i = 0; i < result.length(); i++) { char oldChar = result.charAt(i); char c = Character.toLowerCase(oldChar); if (c >= '0' && c <= '9' || c >= 'a' && c <= 'f') { StringBuffer sb = new StringBuffer(); sb.append(map.get(c)); sb.reverse(); char c2 = map2.get(sb.toString()); if (c2 >= 'a' && c2 <= 'f') { c2 = (char) (c2 - ('a' - 'A')); } result2.append(c2); } else { result2.append(oldChar); } } return result2.toString(); } public static Map<Character, String> map = new HashMap<>(); public static Map<String, Character> map2 = new HashMap<>(); static{ map.put('0',"0000"); map.put('1',"0001"); map.put('2',"0010"); map.put('3',"0011"); map.put('4',"0100"); map.put('5',"0101"); map.put('6',"0110"); map.put('7',"0111"); map.put('8',"1000"); map.put('9',"1001"); map.put('a',"1010"); map.put('b',"1011"); map.put('c',"1100"); map.put('d',"1101"); map.put('e',"1110"); map.put('f',"1111"); map.forEach((k,v)->{ map2.put(v, k); }); } }