美文网首页程序员
力扣 44 通配符匹配

力扣 44 通配符匹配

作者: zhaojinhui | 来源:发表于2020-08-11 10:42 被阅读0次

    题意:给定两个字符串,查看他们是否匹配

    思路:具体思路可见代码

    思想:动态规划

    复杂度:时间O(n2),空间O(n2)

    public class Solution {
         public String multiply(String num1, String num2) {
            int n1 = num1.length(), n2 = num2.length();
            // 结果数组
            int[] products = new int[n1 + n2];
            // 计算数组中每两个数的乘积,并把乘积加入到对应的index
            for (int i = n1 - 1; i >= 0; i--) {
                for (int j = n2 - 1; j >= 0; j--) {
                    int d1 = num1.charAt(i) - '0';
                    int d2 = num2.charAt(j) - '0';
                    products[i + j + 1] += d1 * d2;
                }
            }
            int carry = 0;
            // 处理每一个product元素,使每一位都是一个数字
            for (int i = products.length - 1; i >= 0; i--) {
                int tmp = (products[i] + carry) % 10;
                carry = (products[i] + carry) / 10;
                products[i] = tmp;
            }
            // 结果字符串
            StringBuilder sb = new StringBuilder();
            // 把每一位的数字加入的结果
            for (int num : products) sb.append(num);
            // 移除头部的0
            while (sb.length() != 0 && sb.charAt(0) == '0') sb.deleteCharAt(0);
            return sb.length() == 0 ? "0" : sb.toString();
        }
    }
    

    相关文章

      网友评论

        本文标题:力扣 44 通配符匹配

        本文链接:https://www.haomeiwen.com/subject/qaairktx.html