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 ...
随机推荐
- Nginx自动加载配置文件方案
nginx自动加载配置文件方案一.nginx+consul+consul-template实现过程:consul作为服务发现软件,consul-template作为nginx配置文件的模板,consu ...
- Windows live Writer Tips
http://lehsys.blogspot.com/2013/03/windows-live-writer-how-to-change.html http://www.carlosag.net/to ...
- 第十章· MySQL的主从复制
一.主从复制简介  2015年5月28日11时,12小时后恢复,损失:平均每小时106.48W$ 1)高可用 2)辅助备份 3)分担负载 复制是 MySQL 的一项功能,允许服务器将更改从一个实例复 ...
- sql注入搞事情(连载一)
SQL注入搞事情(连载一) 概述 写在最前面 为了有个合理的训练计划,山人准备长期开放自己的训练计划以及内容以供大家参考.山人专业是信息对抗技术,不是web方向的博客保证句句手打,如有问题请及时小窗. ...
- linux之网络命令
本文整理了在实践过程中使用的Linux网络工具,这些工具提供的功能非常强大,我们平时使用的只是冰山一角,比如lsof.ip.tcpdump.iptables等. 本文不会深入研究这些命令的强大用法,因 ...
- Linux学习--第九天--du、df、fsck、dumpe2fs、mount、NTFS-3G、fdisk、partprobe、/etc/fstab、free、mkswap、swapon
分区类型 主分区:最多只能分四个 扩展分区:只能有一个,如果有了扩展分区,主分区只能有三个.扩展分区不能格式化和存储数据,再划分为逻辑分区才能进行相应操作. 逻辑分区:IDE硬盘,linux最多支持5 ...
- SDK更新失败问题解决
环境:1. win102. Have over fire wall 解决办法:1.启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『A ...
- IIS无法启动解决方案
1.第一步 2.第二步
- java程序连接Liunx服务器并且执行命令
JSch 介绍 JSch 是SSH2的一个纯Java实现.它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等.你可以将它的功能集成到你自己的 程序中.同时该项目也提供一个J2M ...
- itop4412编译内核时出现“recipe for target 'arch/arm/mach-exynos/cpu-exynos4.o' failed”的解决方法
依次执行如下命令 #su root 输入root用户密码 #cd #vim .bashrc 到达最底行,确保环境变量如下图所示 保存退出后,执行如下指令 #source .bashrc 重启Termi ...