xx手撕题

//给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
//
//  注意:
//
// num1 和num2 的长度都小于 5100.
//
// num1 和num2 都只包含数字 0-9.
//
// num1 和num2 都不包含任何前导零。
//
// 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。

PS : 白天没睡中午觉,脑子的确不够灵光,然后算法的东西,只要给时间,似乎都能做出来,哪怕代码再辣鸡。
import java.util.HashMap;
import java.util.Map;

public class TEST {

    private static Map<String, Integer> mapString2Integer = new HashMap<>();

    static class AddResult {
        private Integer data;      // 进行加法后个位数值上的数据.
        private Boolean carryBit; // 是否有进位.

        AddResult(){
            reset();
        }

        AddResult(Integer data, Boolean carryBit){
            this.data = data;
            this.carryBit = carryBit;
        }

        public Integer getData() {
            return data;
        }

        public void setData(Integer data) {
            this.data = data;
        }

        public Boolean getCarryBit() {
            return carryBit;
        }

        public void setCarryBit(Boolean carryBit) {
            this.carryBit = carryBit;
        }

        public void reset() {
            data = 0;
            carryBit = false;
        }
    }

    public static void initMap() {
        mapString2Integer.put("0", 0);
        mapString2Integer.put("1", 1);
        mapString2Integer.put("2", 2);
        mapString2Integer.put("3", 3);
        mapString2Integer.put("4", 4);
        mapString2Integer.put("5", 5);
        mapString2Integer.put("6", 6);
        mapString2Integer.put("7", 7);
        mapString2Integer.put("8", 8);
        mapString2Integer.put("9", 9);
    }


    public static AddResult add(String ch1, String ch2) {
        Integer n1 = mapString2Integer.get(ch1);
        Integer n2 = mapString2Integer.get(ch2);
        assert (n1 != null);
        assert (n2 != null);
        Integer n = n1 + n2;
        return (new AddResult(n % 10, n >= 10 ? Boolean.TRUE : Boolean.FALSE));
    }

    public static AddResult add(String ch1, String ch2, String ch3) {
        Integer n1 = mapString2Integer.get(ch1);
        Integer n2 = mapString2Integer.get(ch2);
        Integer n3 = mapString2Integer.get(ch3);
        assert (n1 != null);
        assert (n2 != null);
        assert (n3 != null);
        Integer n = n1 + n2 + n3;
        return (new AddResult(n % 10, n >= 10 ? Boolean.TRUE : Boolean.FALSE));
    }


    public static String fun(String num1, String num2) {
        StringBuffer sbf = new StringBuffer();
        String strLong, strShort;
        if (num1.length() > num2.length()) {
            strLong  = num1;
            strShort = num2;
        } else {
            strLong  = num2;
            strShort = num1;
        }

        AddResult ar = new AddResult();
        int i = strShort.length() - 1;
        int j = 0;
        // 按倒序进行相加操作.
        for (; i >= 0; i--, j++) {
            String ch1 = String.valueOf(strShort.charAt(i));
            String ch2 = String.valueOf(strLong.charAt(strLong.length() - 1 - j));
            ar = add(ch1, ch2, ar.getCarryBit() == Boolean.TRUE ? "1" : "0" );
            sbf.append(String.valueOf(ar.getData()));
        }
        sbf.reverse();

        //  若上相加出现进位, 则需要重新检查进位情况.
        String str = strLong.substring(0, strLong.length() - strShort.length());
        if (i == -1 && ar.getCarryBit() == Boolean.TRUE) {
            int m = str.length() - 1;
            for (; m >= 0; m--) {
                String e = String.valueOf(str.charAt(m));
                if(ar.getCarryBit() == Boolean.TRUE) {
                    ar = add(e, "1", "0");
                    //sbf.append(ar.getData());
                    sbf.insert(0, ar.getData());
                } else {
                    //sbf.append(e);
                    sbf.insert(0, e);
                }
            }

            // 即时是遍历完一遍长数据高位了,还是要多检查一次进位情况.
            if(m == -1 && ar.getCarryBit() == Boolean.TRUE) {
                sbf.insert(0, "1");
            }
        }
        // 未发现进位则直接将多余的高位长度数据往前插入即可.
        else {
            sbf.insert(0,  str);
        }

        return sbf.toString();
    }

    public static void main(String[] args) {
        initMap();
        System.out.println(fun("456", "789"));
        System.out.println(fun("999", "1"));
//
        System.out.println(fun("55555", "55555"));
        System.out.println(fun("64", "36"));
        System.out.println(fun("123456789", "987654321"));
//
        System.out.println(fun("22", "333"));

        System.out.println(fun("666666666", "666"));
    }
}
View Code

 

posted @ 2022-04-25 04:09  v大喵  阅读(27)  评论(0)    收藏  举报