力扣题:数字与字符串间转换-12.14

力扣题-12.14

[力扣刷题攻略] Re:从零开始的力扣刷题生活

力扣题1:442. 数组中重复的数据

解题思想:从字符串中能够正确提取数字即可

力扣题:数字与字符串间转换-12.14_第1张图片

class Solution(object):
    def complexNumberMultiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        temp1_1 = int(num1.split("+")[0])
        temp1_2 = int(num1.split("+")[1].split("i")[0])
        temp2_1 = int(num2.split("+")[0])
        temp2_2 = int(num2.split("+")[1].split("i")[0])
        num_1 = temp1_1*temp2_1 - temp1_2*temp2_2
        num_2 = temp1_1*temp2_2+temp1_2*temp2_1
        return str(num_1)+'+'+str(num_2)+"i"
class Solution {
public:
    string complexNumberMultiply(string num1, string num2) {
        int temp1_1, temp1_2, temp2_1, temp2_2;
        std::istringstream iss1(num1), iss2(num2);

        char plus1, i1, plus2, i2;
        iss1 >> temp1_1 >> plus1 >> temp1_2 >> i1;
        iss2 >> temp2_1 >> plus2 >> temp2_2 >> i2;

        int num_1 = temp1_1 * temp2_1 - temp1_2 * temp2_2;
        int num_2 = temp1_1 * temp2_2 + temp1_2 * temp2_1;

        std::ostringstream result;
        result << num_1 << "+" << num_2 << "i";

        return result.str();
    }
};

你可能感兴趣的:(leetcode,数据库,算法)