leetcode415_字符串运算

415. 字符串相加
image
模拟手算:
(1)注意对其最低项
(2)注意进位

 public String addStrings(String num1, String num2) {
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        int add = 0;
        StringBuilder sb = new StringBuilder();
        while(i >= 0 || j >= 0 || add != 0) {
            int x = i >= 0? num1.charAt(i) - '0' : 0;
            int y = j >= 0? num2.charAt(j) - '0' : 0;
            int sum = x + y + add;
            i --;
            j --;
            sb.append(sum % 10);
            add = sum / 10;
        }
        return sb.reverse().toString();
    }

相似问题
43.字符串相乘
解法一:
不由思索的方法:简单乘法 + 加法

 public String multiply(String num1, String num2) {
        // 考虑这一种极端的情况
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        List<String> list = new ArrayList<>();
        String les;
        String lon;
        for(int i = num2.length() - 1; i >= 0; i --) {
            String result = mul(num1,"" + num2.charAt(i));
            StringBuilder sb = new StringBuilder();
            for (int k = 0; k < list.size(); k ++) {
                sb.append(0);
            }
            result += sb.toString();
            list.add(result);
        }
        String result = list.get(0);
        for (int i = 1; i < list.size(); i ++){
            result = add(result,list.get(i));
        }
       return result;
    }

    public String mul (String num1, String num) {
        int i = num1.length() - 1;
        StringBuilder sb = new StringBuilder();
        int add = 0;
        int y = num.charAt(0) - '0';
        while(i >= 0 || add != 0) {
            int x = i >= 0? num1.charAt(i) - '0' : 0;
            int sum = x * y + add;
            sb.append(sum % 10);
            add = sum / 10;
            i --;
        }
        return sb.reverse().toString();
    }

    public String add (String num1, String num2) {
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        StringBuilder sb = new StringBuilder();
        int add = 0;
        while(i >= 0 || j >= 0 || add != 0) {
            int x = i >= 0? num1.charAt(i) - '0' : 0;
            int y = j >=0? num2.charAt(j) - '0' : 0;
            int sum = x + y + add;
            sb.append(sum % 10);
            add = sum / 10;
            i --;
            j --;
        }
        return sb.reverse().toString();
    }

解法二:
使用数组来进行存储:

    public String multiply(String num1, String num2) {
        // 考虑这一种极端的情况
        StringBuilder sb = new StringBuilder();
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int l1 = num1.length() - 1;
        int l2 = num2.length() - 1;
        int [] ans = new int [l1 + l2 + 2];
        for (int i = l2; i >= 0; i --) {
            int start = num2.length() - i - 1;
            String result = mul(num1,"" + num2.charAt(i));
            for (int j = result.length() - 1; j >= 0; j --) {
                ans[start] += (result.charAt(j) - '0');
                start ++;
            } 
        }
        for (int i = 1; i < ans.length; i ++) {
            ans[i] += (ans[i - 1] / 10);
            ans[i - 1] = ans[i - 1] % 10;
        }
        // 去0
        boolean flag = true;
        for (int i = ans.length - 1; i >= 0; i --) {
            if (flag && ans[i] == 0){continue;}
            else if (flag) {
                flag = false;
            }
            sb.append(ans[i]);
        }
       return sb.toString();
    }

    public String mul (String num1, String num) {
        int i = num1.length() - 1;
        StringBuilder sb = new StringBuilder();
        int add = 0;
        int y = num.charAt(0) - '0';
        while(i >= 0 || add != 0) {
            int x = i >= 0? num1.charAt(i) - '0' : 0;
            int sum = x * y + add;
            sb.append(sum % 10);
            add = sum / 10;
            i --;
        }
        return sb.reverse().toString();
    }

最终版方法(数组):

if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int m = num1.length(), n = num2.length();
        int[] ansArr = new int[m + n];
        for (int i = m - 1; i >= 0; i--) {
            int x = num1.charAt(i) - '0';
            for (int j = n - 1; j >= 0; j--) {
                int y = num2.charAt(j) - '0';
                ansArr[(m - i - 1) + (n -j - 1)] += x * y;
            }
        }
		// 进位操作
        for (int i = 1; i < ansArr.length; i ++){
            ansArr[i] += ansArr[i - 1] / 10;
            ansArr[i - 1] = ansArr[i - 1] % 10; 
        }
        boolean flag = true;
        StringBuilder sb = new StringBuilder();
		// 避免前导0
        for (int i = m + n - 1; i >= 0; i --) {
            if (ansArr[i] == 0 && flag) {continue;} 
            else if (flag) {
                flag = false;
            }
            sb.append(ansArr[i]);
        }
        return sb.toString();
    }
posted @ 2022-08-18 19:10  Kyara  阅读(19)  评论(0编辑  收藏  举报