题解 | #字符串合并处理#
字符串合并处理
http://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
题目本身其实并不是很难,主要是代码量太多了。细心读题,一步一步实现就好。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] strs = scan.nextLine().split(" ");
char[] original = (strs[0] + strs[1]).toCharArray();
char[] even = new char[(original.length + 1) / 2];
char[] odd = new char[original.length / 2];
int p1 = 0;
int p2 = 0;
for (int i = 0; i < original.length; i++) {
if (i % 2 == 0) {
even[p1++] = original[i];
} else {
odd[p2++] = original[i];
}
}
mergeSort(even);
mergeSort(odd);
p1 = 0;
p2 = 0;
StringBuffer sb = new StringBuffer("");
while (p1 < even.length && p2 < odd.length) {
sb.append(even[p1]);
sb.append(odd[p2]);
p1++;
p2++;
}
if (p1 < even.length) {
sb.append(even[p1]);
}
System.out.println(transform(new String(sb)));
}
public static void mergeSort(char[] chrs) {
if (null == chrs || chrs.length < 2) {
return;
}
process(chrs, 0, chrs.length - 1);
}
public static void process(char[] chrs, int start, int end) {
if (start >= end) {
return;
}
int mid = start + ((end - start) >> 1);
process(chrs, start, mid);
process(chrs, mid + 1, end);
merge(chrs, start, mid, end);
}
public static void merge(char[] chrs, int start, int mid, int end) {
char[] helper = new char[end - start + 1];
int index = 0;
int p1 = start;
int p2 = mid + 1;
while (p1 <= mid && p2 <= end) {
helper[index++] = chrs[p1] <= chrs[p2] ? chrs[p1++] : chrs[p2++];
}
while (p1 <= mid) {
helper[index++] = chrs[p1++];
}
while (p2 <= end) {
helper[index++] = chrs[p2++];
}
for (int i = 0; i < helper.length; i++) {
chrs[start + i] = helper[i];
}
}
public static String transform(String str) {
HashMap<Character, Integer> CharacterToInteger = new HashMap<>();
HashMap<Integer, Character> IntegerToCharacter = new HashMap<>();
for (int i = 0; i < 10; i++) {
CharacterToInteger.put((char) ('0' + i), i);
IntegerToCharacter.put(i, (char) ('0' + i));
}
for (int i = 0; i < 6; i++) {
CharacterToInteger.put((char) ('A' + i), i + 10);
CharacterToInteger.put((char) ('a' + i), i + 10);
IntegerToCharacter.put(i + 10, (char) ('A' + i));
}
StringBuffer sb = new StringBuffer("");
char[] chrs = str.toCharArray();
for (char chr : chrs) {
if ((chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'F') || (chr >= 'a' && chr <= 'f')) {
int val = CharacterToInteger.get(chr);
String tmpStr = DecimalToBinaryAndReverse(val);
int tmpVal = BinaryToDecimal(tmpStr);
sb.append(IntegerToCharacter.get(tmpVal));
} else {
sb.append(chr);
}
}
return new String(sb);
}
public static String DecimalToBinaryAndReverse(int num) {
StringBuffer sb = new StringBuffer("");
while (num != 0) {
sb.append(num % 2);
num /= 2;
}
while (sb.length() != 4) {
sb.append("0");
}
return new String(sb);
}
public static int BinaryToDecimal(String str) {
int ans = 0;
int acc = 0;
char[] chrs = str.toCharArray();
for (int i = chrs.length - 1; i > -1; i--) {
if (chrs[i] == '1') {
ans += (int) Math.pow(2, acc);
}
acc++;
}
return ans;
}
}