• LeetCode题解之Multiply Strings


    1、题目描述

    2、问题分析

    按照手算乘法的过程进行计算,首先计算乘法,然后计算加法。

    3、代码

      1 string multiply(string num1, string num2) {
      2         string small ;
      3         string big;
      4         if( num1.size() < num2.size() ){
      5             small = num1;
      6             big = num2;
      7         }else if( num2.size() < num1.size() ){
      8             small = num2;
      9             big = num1;
     10         }else{
     11             small = num1;
     12             big = num2;
     13         }
     14         
     15         int n = small.size() ;
     16         vector<string> re;
     17         
     18         string::reverse_iterator it_s = small.rbegin() ;
     19         while( it_s != small.rend() ){
     20             string  s;
     21             int up = 0;
     22             string::reverse_iterator it_b = big.rbegin() ;
     23             while( it_b != big.rend() ){
     24                 int m = (*it_s - '0') * (*it_b - '0') + up;
     25                 if(  m < 10 ){
     26                     s = std::to_string(m) + s;
     27                     up = 0;
     28                 }else{
     29                     s = std::to_string( m%10 )  + s;
     30                     up = m/10;
     31                 }
     32                 ++it_b;
     33             }
     34             if( up != 0 ){
     35                 s = std::to_string( up ) + s;
     36             }
     37             re.push_back( s );
     38             ++it_s;
     39         }
     40         
     41         string zero;
     42         for( vector<string>::iterator it = re.begin() ; it != re.end() ; it++ ){
     43             *it += zero;
     44             zero += "0";
     45         }
     46         
     47         string result = "0" ;
     48         for( int i = 0 ; i < re.size() ; i++ ){
     49             result = binaryAdd( result , re[i] );
     50         }
     51         
     52         int q = 0;
     53         for( string::iterator it = result.begin() ; it != result.end() ; ++it ){
     54             q += (*it - '0');
     55         }
     56         if( q == 0)
     57             result = "0";
     58         return result;
     59         
     60         
     61     }
     62     
     63     string binaryAdd( string s1 , string s2 ){
     64         string s;
     65         string::reverse_iterator it1 = s1.rbegin();
     66         string::reverse_iterator it2 = s2.rbegin();
     67         int up = 0;
     68         while( it1 != s1.rend() && it2 != s2.rend() ){
     69             int a = (*it1 - '0') + (*it2 - '0') + up;
     70             if( a < 10 ){
     71                 s  = std::to_string(a) + s;
     72                 up = 0;
     73             }else{
     74                 s = std::to_string( a - 10 ) + s;
     75                 up = 1;
     76             }
     77             ++it1 ;
     78             ++it2 ;
     79         }
     80         
     81         while( it1 != s1.rend() ){
     82             if( *it1 - '0' + up < 10 ){
     83                 s = std::to_string( *it1 - '0' + up ) + s;
     84                 up = 0;
     85             }else{
     86                 s = std::to_string( *it1 - '0' + up - 10 ) + s;
     87                 up = 1;
     88             }
     89             ++it1;
     90         }
     91         
     92         while( it2 != s2.rend() ){
     93             if( *it2 - '0' + up < 10 ){
     94                 s = std::to_string( *it2 - '0' + up ) + s;
     95                 up = 0;
     96             }else{
     97                 s = std::to_string( *it2 - '0' + up -10  ) + s;
     98                 up = 1;
     99             }
    100             ++it2;
    101         }
    102         
    103         if( up == 1){
    104             s = std::to_string(1) + s;
    105         }
    106         return s;
    107     }
    pp
  • 相关阅读:
    php实现邮件自动发送之PHPMailer
    wnmp配置(windows+nginx+mysql+php开发环境)
    _stdcall和_cdecl,extern “C”的联系
    DLL导出的方式
    C++哈夫曼编码
    Python CGI 环境搭建
    Java学习日记——第二记:基本类型的类型转换
    测试技术的应用
    软件测试的分类
    软件测试生命周期
  • 原文地址:https://www.cnblogs.com/wangxiaoyong/p/9298517.html
Copyright © 2020-2023  润新知