zoukankan      html  css  js  c++  java
  • [LeetCode] Multiply Strings

    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.

    class Solution {
    public:
        string multiply(string num1, string num2) {
              string result;
              int len1 = num1.size(),len2 = num2.size();
              int flag = 0, n=0,num=0;
              if(num1=="0" || num2=="0")
                 return "0";
              for(int i=len2-1;i>=0;i--){//for(1)       num2  乘数
                  int len = result.size();
                  int k = len-(len2-i);
                  for(int j= len1-1;j>=0;j--){//for(2)  num1  被乘数
                      if(i==len2-1){
                          n = (num1[j]-'0')*(num2[i]-'0')+flag;
                          flag = (n-n%10)/10;
                          num  =  n-flag*10;
                          result.insert(result.begin(),num+'0');
                          if(j==0 && flag!=0){
                              result.insert(result.begin(),flag+'0');
                              flag = 0;
                          }         
                      }else{ 
                          if(k>=0){
                              n = (num1[j]-'0')*(num2[i]-'0')+flag+(result[k]-'0');
                              flag = (n-n%10)/10;
                              num  =  n-flag*10;
                              result[k] =  num+'0';
                              if(j==0 && flag!=0){
                                  result.insert(result.begin(),flag+'0');
                                  flag = 0;
                              } 
                          }else if(k<0){
                              n = (num1[j]-'0')*(num2[i]-'0')+flag;
                              flag = (n-n%10)/10;
                              num  =  n-flag*10;
                             result.insert(result.begin(),num +'0');
                             if(j==0 && flag!=0){
                                  result.insert(result.begin(),flag+'0');
                                  flag = 0;
                              }
                          }
                              
                          k--;
                      }
                  }//end for(2)
              }//end for(1)
            
              return result;
        }//end func
    };
  • 相关阅读:
    MQTT Client软件-MQTTBox
    Eclipse
    Ant + ivy的安装
    常用消息中间件比较
    各种MQTT server功能比較
    消息中间件的对比
    RabbitMQ Performance Testing Tool 性能测试工具
    Eureka 简介
    win10 localhost 解析为::1 的解决办法
    JSP中过滤器的设置
  • 原文地址:https://www.cnblogs.com/Xylophone/p/3924001.html
Copyright © 2011-2022 走看看