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

字符串合并处理

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

一个思路清晰的分步骤解题思路

这道题难也难不到哪去,就是比较复杂,代码多了遇到点小错误容易改崩溃,比如StringBuilder设的是全局的,但是局部用过一次没有及时清空导致答案非常离谱。。。



import java.util.*;
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.next();
        String s2 = scanner.next();
        s1 = convert(asort(combine(s1,s2)));
        System.out.println(s1);
    }
    public static String combine(String str1, String str2){
        String str3 = str1 + str2;
        return str3;
    }

    public static String asort(String str){
        int len = str.length();
        StringBuilder odd = new StringBuilder();
        StringBuilder even = new StringBuilder();
        for(int i = 0; i < len; i++ ){
            if(i % 2 == 0)
                odd.append(str.charAt(i));
            else
                even.append(str.charAt(i));
        }
        String oddStr = odd.toString();
        String evenStr = even.toString();
        char[] oddChar = oddStr.toCharArray();
        char[] evenChar = evenStr.toCharArray();
        Arrays.sort(oddChar);
        Arrays.sort(evenChar);
        StringBuilder sb1 = new StringBuilder();
        for(int i = 0; i < len; i++){
            if(i % 2 == 0)
                sb1.append(oddChar[i / 2]);
            else
                sb1.append(evenChar[(i + 1) / 2 - 1]);
        }
        oddStr = sb1.toString();
        return oddStr;
    }

    public static String convert(String str){
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        String s;
        for(int i = 0; i < str.length(); i++){
            int j = 0;
            if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
                s = new StringBuilder(Integer.toBinaryString(Integer.parseInt(str.charAt(i) + ""))).toString();
                while(j++ < (4 - s.length()))
                    sb.append('0');
                sb.append(s);
                s = sb.reverse().toString();
                sb.delete(0,sb.length());
                int num = Integer.parseInt(s,2);
                sb1.append(Integer.toHexString(num).toUpperCase());
            }
            else if(str.charAt(i) >= 'a' && str.charAt(i) <= 'f'){
                s = str.charAt(i) - 'a' + 10 + "";
                s = new StringBuilder(Integer.toBinaryString(Integer.parseInt(s))).toString();
                sb.append(s);
                s = sb.reverse().toString();
                sb.delete(0,sb.length());
                int num = Integer.parseInt(s,2);
                sb1.append(Integer.toHexString(num).toUpperCase());
            }
            else if(str.charAt(i) >= 'A' && str.charAt(i) <= 'F'){
                s = str.charAt(i) - 'A' + 10 + "";
                s = new StringBuilder(Integer.toBinaryString(Integer.parseInt(s))).toString();
                sb.append(s);
                s = sb.reverse().toString();
                sb.delete(0,sb.length());
                int num1 = Integer.parseInt(s,2);
                sb1.append(Integer.toHexString(num1).toUpperCase());
            }
            else{
                sb1.append(str.charAt(i));
            }
        }
        return sb1.toString();
    }
}















全部评论

相关推荐

gcniz:一天写两千行你闹呢
点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务