๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Leetcode

[Leetcode,c++] Multiply-Strings

by dkswnkk 2021. 11. 7.

๋ฌธ์ œ

 

Multiply Strings - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

์ฝ”๋“œ

class Solution {
public:
  string multiply(string num1, string num2) {
    if(num1 == "0" || num2 == "0") return "0";

    vector<int> v(num1.length() + num2.length(), 0);
    for(int i = num1.length()-1; i >= 0; i--){
      for(int k = num2.length()-1; k >= 0; k--){
        int mul = (num1[i]-'0') * (num2[k]-'0') + v[i+k+1];
        v[i+k+1] = mul % 10;
        v[i+k] += mul / 10;
      }
    }

    string result = "";

    for(int i = 0; i <v.size(); i++){
        if(v[i]==0&&i==0) continue;
      result += v[i]+'0';
    }

    return result;
  }
};

 

ํ’€์ด

BigInteger ๊ณฑ์…ˆ ์—ฐ์‚ฐ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ๋งˆ๋‹ค ๋ฐ”์ดํŠธ ์ œํ•œ์ด ์—†๋Š” python์ด๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•ด์ฃผ๋Š” Java๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ....

๋Œ“๊ธ€