【Multiply Strings】cpp

题目:

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) {
            const int n1 = num1.size(), n2 = num2.size();
            if ( num1=="0" || num2=="0") return "0";
            vector<char> ret;
            for ( int i=n1-1; i>=0; --i )
            {
                vector<char> local(1,‘0‘);
                int v = 0, carry = 0, curr = 0;
                for ( int j=n2-1; j>=0; --j )
                {
                    v= (num1[i]-‘0‘)*(num2[j]-‘0‘);
                    carry = v/10;
                    curr = v%10;
                    carry += ((local[0]-‘0‘)+curr)/10;
                    curr = ((local[0]-‘0‘)+curr)%10;
                    local[0] = curr+‘0‘;
                    local.insert(local.begin(), carry+‘0‘);
                }
                if (local[0]==‘0‘) local.erase(local.begin());
                // cout << string(local.begin(),local.end()) << endl;
                // add zeros
                for ( int z=n1-1; z>i; --z) local.push_back(‘0‘);
                // cout << string(local.begin(),local.end()) << endl;
                carry = 0, curr = 0;
                for ( int r=ret.size()-1, l=local.size()-1; r>=0 || l>=0; --r,--l )
                {
                    if ( r>=0 && l>=0 )
                    {
                        curr = (carry+(ret[r]-‘0‘)+(local[l]-‘0‘))%10;
                        carry = (carry+(ret[r]-‘0‘)+(local[l]-‘0‘))/10;
                        local[l] = curr+‘0‘;
                    }
                    else
                    {
                        curr = (carry+(local[l]-‘0‘))%10;
                        carry = (carry+(local[l]-‘0‘))/10;
                        local[l] = curr+‘0‘;
                    }
                }
                if (carry!=0) { local.insert(local.begin(), carry+‘0‘); }
                ret = local;
            }
            return string(ret.begin(),ret.end());
    }
};

tips:

就是一些字符串操作的细节,考虑进位,0这类的细节。

时间: 2024-10-02 18:33:03

【Multiply Strings】cpp的相关文章

【Add binary】cpp

题目: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 代码: class Solution { public: string addBinary(string a, string b) { std::string result; std::string::reverse_iter

【Scramble String】cpp

题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choo

【Divided Two】cpp

题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 代码: class Solution { public: int divide(int dividend, int divisor) { if (divisor==0) return dividend>=0 ? INT_MAX : INT_MIN; if (divis

【Word Break】cpp

题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true becau

【Sudoku Solver】cpp

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 代码: cla

【Subsets II】cpp

题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,2], a sol

【LRU Cache】cpp

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【Rotate List】cpp

题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 代码: /** * Definition for singly-linked list. * struct ListNode {

【Text Justification】cpp

题目: Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pa