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 ...
随机推荐
- 让框架内循环或者指定元素 指定CSS
p:nth-child(3n+) { background:#ff0000; } http://www.w3school.com.cn/cssref/selector_nth-child.asp
- phpstudycomposer thinkPHP5.1 使用
1.首先把php变成全局变量 2.打开phpstudy composer 的安装目录 E:\phpstudy\PHPTutorial\tools\composer 把里面的文件全部删除(或者备份一下) ...
- Java_Eclipse_Android安装
转自——链接:https://www.cnblogs.com/summary-2017/p/8073225.html
- Eclispe造成的tomcat占用端口 无法启动 强制终止进程 转载
很多时候运行tomcat 的时候总是会提示tomcat 的端口被占用 但是任务管理器里面还找不到是哪个端口被占用了 因此很多人就重新配置tomcat 或者去修改tomcat的端口号 ,其实这么做太麻 ...
- IPC之mq_sysctl.c源码解读
// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2007 IBM Corporation * * Author: Cedric ...
- zabbix 3.2.2 server端(源码包)安装部署 (一)
环境准备: 操作系统 CentOS 6.8 2.6.32-642.11.1.el6.x86_64 zabbix server 172.16.10.150 zabbix agent 172.16.10. ...
- Android异常与性能优化相关面试问题-bitmap面试问题详解
recycle: 对于Bitmap来说内存分为Java内存和Native内存,而当图片不用时建议调用一下recycle()方法来将native层的内存进行回收,下面看一下它的源码官方对它的解释: LR ...
- kotlin字符串和数字之间的转换和人机交互
继续基础学习~ 字符串和数字之间的转换 那如何转换呢,其实很简单: 编译木有报错,但是运行: 所以这里了解下. 人机交互 看这标题貌似高端的,其实也就是程序可以接受键盘的输入啦,下面开始: 首先提示用 ...
- linux shell介绍
Shell定义 Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行. 实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内 ...
- 谷歌插件学习笔记:把iframe干掉……
好久不写博客了,感觉自己变得越来越懒了,是没有时间吗?不是,是自己变得越来越懒了,好多东西不愿意去总结了,可能也是学的不精总结不出来什么玩意儿.不过,一切都是借口.还是坚持学习,坚持写博客吧,虽然写的 ...