zoukankan      html  css  js  c++  java
  • LeetCode

    题目:

    Given two numbers represented as strings, return multiplication of the numbers as a string.

    Note: The numbers can be arbitrarily large and are non-negative.

    思路:

    做乘法

    package manipulation;
    
    public class MultiplyStrings {
    
        public String multiply(String num1, String num2) {
            int len1 = num1.length();
            int len2 = num2.length();
            int len = len1 + len2;
            int[] res = new int[len];
            for (int i = len1 - 1; i >= 0; --i) {
                for (int j = len2 - 1; j >= 0; --j) {
                    multiply(num1, i, num2, j, res, len);
                }
            }
            StringBuilder sb = new StringBuilder();
            int i = len - 1;
            while (i >= 0 && res[i] == 0) --i;
            
            if (i == -1) sb.append('0');
            for (; i >= 0; --i) {
                sb.append((char)(res[i] + '0'));
            }
            
            return sb.toString();
        }
        
        private void multiply(String num1, int i, String num2, int j, int[] res, int len) {
            int m = num1.charAt(i) - '0';
            int n = num2.charAt(j) - '0';
            int value = m * n;
            int index = len - i - j - 2;
            res[index] = res[index] + value;
            res[index + 1] = res[index + 1] + res[index] / 10;
            res[index] = res[index] % 10;
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MultiplyStrings m = new MultiplyStrings();
            System.out.println(m.multiply("11", "11"));
        }
    
    }
  • 相关阅读:
    JavaScript核心参考
    面向对象的程序设计之工厂模式
    ES6中promise的使用方法
    详解 Vue 2.4.0 带来的 4 个重大变化
    Vue.js 1.x 和 2.x 实例的生命周期
    表单控件的全面分析
    元素的一些常用属性
    为表格增加的方法
    Element类型知识大全
    6-3.斜体标签
  • 原文地址:https://www.cnblogs.com/null00/p/5075539.html
Copyright © 2011-2022 走看看