537. 复数乘法

给定两个表示复数的字符串。

返回表示它们乘积的字符串。注意,根据定义 i2 = -1 。

示例 1:

输入: “1+1i”, “1+1i”

输出: “0+2i”

解释: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。

示例 2:

输入: “1±1i”, “1±1i”

输出: “0±2i”

解释: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0±2i 的形式。

注意:

输入字符串不包含额外的空格。

输入字符串将以 a+bi 的形式给出,其中整数 a 和 b 的范围均在 [-100, 100] 之间。输出也应当符合这种形式。

class Solution {
public String complexNumberMultiply(String a, String b) { StringBuilder res = new StringBuilder(); int aspit = a.indexOf('+');
int bspit = b.indexOf('+'); int aa = Integer.parseInt( a.substring(0,aspit) );
int ab = Integer.parseInt( a.substring(aspit+1, a.length()-1) );
int ba = Integer.parseInt( b.substring(0,bspit) );
int bb = Integer.parseInt( b.substring(bspit+1, b.length()-1) ); res.append(aa * ba - ab * bb);
res.append('+');
res.append(aa * bb + ab * ba);
res.append('i');
return res.toString();
}
}

Java实现 LeetCode 537 复数乘法(关于数学唯一的水题)的更多相关文章

  1. LeetCode 537. 复数乘法(Complex Number Multiplication)

    537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...

  2. Java实现 LeetCode 467 环绕字符串中唯一的子字符串

    467. 环绕字符串中唯一的子字符串 把字符串 s 看作是"abcdefghijklmnopqrstuvwxyz"的无限环绕字符串,所以 s 看起来是这样的:"-zabc ...

  3. Java实现 LeetCode 781 森林中的兔子(分析题)

    781. 森林中的兔子 森林中,每个兔子都有颜色.其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色.我们将这些回答放在 answers 数组里. 返回森林中兔子的最少数量. 示例: ...

  4. Java实现 LeetCode 770 基本计算器 IV(暴力+分析题)

    770. 基本计算器 IV 给定一个表达式 expression 如 expression = "e + 8 - a + 5" 和一个求值映射,如 {"e": ...

  5. CodeForces 706A Beru-taxi (数学计算,水题)

    题意:给定一个固定位置,和 n 个点及移动速度,问你这些点最快到固定点的时间. 析:一个一个的算距离,然后算时间. 代码如下: #pragma comment(linker, "/STACK ...

  6. Leetcode——171.Excel表列序号【水题】

    @author: ZZQ @software: PyCharm @file: leetcode171_Excel表列序号.py @time: 2018/11/22 15:29 要求: 给定一个Exce ...

  7. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. QTreeWidget更新后保存节点的展开状态

    class Xx : public QWidget { Q_OBJECT struct ItemState{ ItemState(); int _id; bool _isExpend; }; publ ...

  2. Linux dts 设备树详解(二) 动手编写设备树dts

    Linux dts 设备树详解(一) 基础知识 Linux dts 设备树详解(二) 动手编写设备树dts 文章目录 前言 硬件结构 设备树dts文件 前言 在简单了解概念之后,我们可以开始尝试写一个 ...

  3. [zoj3596]DP(BFS)

    题意:求n的最小倍数,满足性质P:十进制的每一位上的数有m种(0<m<=10). 思路:直接枚举n的最小倍数,然后检测是否满足性质P,n一大很容易超时,并且无法判断无解的情况.巧妙的做法是 ...

  4. angular foreach的使用

    var myAppModule = angular.module('myApp', []); var values = { name : 'misko', gender : 'male', " ...

  5. ol,li,ul,dl,dt,dd

    块级元素div尽量少用,其实和table一样,嵌套越少越好,它也是会影响速度的! ol 有序列表. <ol> <li>……</li> <li>……< ...

  6. js读取json

    Json字符串是: [{"n":"aaa","un":"aaa"},{"n":"yang& ...

  7. 用window.print()打印如何去掉页眉和页脚

    用window.print()打印如何去掉页眉和页脚 2007-07-12 11:44:52|  分类: javascript 知识|举报|字号 订阅     <script language= ...

  8. Java开发架构篇:领域驱动设计架构基于SpringCloud搭建微服务

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言介绍 微服务不是泥球小单体,而是具备更加清晰职责边界的完整一体的业务功能服务.领域驱动 ...

  9. UVALive 3295

    题目大意:见刘汝佳<算法竞赛入门经典——训练指南>P173 解题思路: 每一个合法的三角形的三个顶点都不在同一直线上,那么问题其实就是在求所有不全在同一直线上的三点的组合数. 我们可以利用 ...

  10. 一文读懂Java注解

    什么是注解 Java官方文档上说,注解是元数据的一种形式,它提供不属于程序一部分的数据,注解对被注解的代码没有直接的影响. 准确上说,注解只不过是一种特殊的注释而已,如果没有解析它的代码,它可能连注释 ...