LC 537. Complex Number Multiplication
Given two strings representing two complex numbers.
You need to return a string representing their multiplication. Note i2 = -1 according to the definition.
Example 1:
Input: "1+1i", "1+1i"
Output: "0+2i"
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
Example 2:
Input: "1+-1i", "1+-1i"
Output: "0+-2i"
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
Note:
- The input strings will not have extra blank.
- The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.
Runtime: 4 ms, faster than 14.90% of C++ online submissions for Complex Number Multiplication.
class Solution {
public:
pair<int,int> get_ab(string s){
int addpos = ;
for(int i=; i<s.size(); i++){
if(s[i] == '+') addpos = i;
}
auto p = make_pair<int,int>(,);
p.first = stoi(s.substr(,addpos));
p.second = stoi(s.substr(addpos+,s.size()-addpos-));
return p;
}
string complexNumberMultiply(string a, string b) {
auto a_ab = get_ab(a);
auto b_ab = get_ab(b);
int x1 = a_ab.first, y1 = a_ab.second, x2 = b_ab.first, y2 = b_ab.second;
string reta = to_string(x1*x2 - y1*y2);
string retb = to_string(x1*y2 + x2*y1);
return reta + "+" + retb + "i";
}
};
LC 537. Complex Number Multiplication的更多相关文章
- 【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...
- 537. Complex Number Multiplication
题目大意: 给出a, b两个用字符串表示的虚数,求a*b 题目思路: 偷了个懒,Python3的正则表达式匹配了一下,当然acm里肯定是不行的 class Solution: def complexN ...
- 537 Complex Number Multiplication 复数乘法
详见:https://leetcode.com/problems/complex-number-multiplication/description/ C++: class Solution { pu ...
- LeetCode 537. 复数乘法(Complex Number Multiplication)
537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...
- [LeetCode] Complex Number Multiplication 复数相乘
Given two strings representing two complex numbers. You need to return a string representing their m ...
- [Swift]LeetCode537. 复数乘法 | Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
- LeetCode Complex Number Multiplication
原题链接在这里:https://leetcode.com/problems/complex-number-multiplication/description/ 题目: Given two strin ...
- [leetcode-537-Complex Number Multiplication]
Given two strings representing two complex numbers. You need to return a string representing their m ...
- ural 1748 The Most Complex Number 和 丑数
题目:http://acm.timus.ru/problem.aspx?space=1&num=1748 题意:求n范围内约数个数最多的那个数. Roughly speaking, for a ...
随机推荐
- ffmpeg生成视频封面图
ffmpeg 是一个视频处理软件 php-ffmpeg 是一个让 php 可以操作 ffmpeg 的 php插件,封装好了各种操作视频的名命令.直接调用对应的方法即可. 使用过程很曲折也很简单 曲折在 ...
- Linux编译安装GCC
1. 下载gcc安装包 网址:http://ftp.gnu.org/gnu/gcc/ ,下载对应的安装包,我选择gcc-5.5.0.tar.gz 2. 下载依赖库 一个是mpc,一个是gmp,一个是m ...
- SQL语句复习【专题七】
SQL语句复习[专题七] 完整性约束分类1)域完整性约束(非空not null,检查check)2)实体完整性约束(唯一unique,主键primary key)3)参照完整性约束(外键foreign ...
- 05-【session、cookie】
session.cookie 1.HttpSession概述>HttpSession是由JavaWeb提供的,用来会话跟踪的类.session是服务器端对象,保存在服务器端!!!>Http ...
- 【vue lazyload】插件的使用步骤
VUE图片懒加载-vue lazyload插件的简单使用
- javascript代码实用方法实现
javascript代码实用方法实现 针对现在大家平时开发中,都会写一些重复性的js处理代码,今天总结了几个比较常用的方法实现.获取get请求参数.去字符串空格. 1.获取get请求中的参数 ...
- IDA 头像是谁
IDA图标上的女子:Ada Lovelace Ada Lovelace 简介: 阿达·奥古斯塔,19世纪诗人拜伦的女儿,数学家.穿孔机程序创始人,建立了循环和子程序概念.为计算程序拟定“算法”,写作的 ...
- 201871010104-陈园园《面向对象程序设计(java)》第十七周学习总结
201871010104-陈园园<面向对象程序设计(java)>第十七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- axios 请求多个接口
axios.all([ axios.get('https://api.github.com/xxx/1'), axios.get('https://api.github.com/xxx/2') ]) ...
- 通过CSS实现 文字渐变色 的两种方式
说明 这次的重点就在于两个属性, background 属性 mask 属性这两个属性分别是两种实现方式的关键. 方式一 解释 <!DOCTYPE html> <html> & ...