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

力扣题-12.14

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

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

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

在这里插入图片描述

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();
    }
};
  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值