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. 【Hadoop离线基础总结】MapReduce增强(下)

    MapReduce增强(下) MapTask运行机制详解以及MapTask的并行度 MapTask运行流程 第一步:读取数据组件InputFormat(默认TextInputFormat)会通过get ...

  2. GP1UM26(78)1RK远程红外遥控接收IC数据手册学习

    1.Features 该系列IC具有多种BMP带通频率可供选择,典型的GP1UM261RK带通频率为38KHz,内部的前置放大器等放大电路工作频率均为38KHz. Compact紧凑型,体积小 2.i ...

  3. [csu1508 地图的四着色]二分图染色

    抽象后的题意:给一个不超过30个点的图,A从中选不超过5个点涂红绿两种颜色,B用黑白两种颜色把剩下的涂完,任意一条边两端的颜色不同,求每种颜色至少用涂一次的方案数 思路:枚举A涂的点的集合,将原图分成 ...

  4. csu1617]强连通分量

    题意:定义域属于一个集合S={0,1,...,n-1},求S的子集个数,满足以子集的元素为定义域的函数P(x)的值域等于子集本身. 思路:以元素为点,x到P(x)连一条有向边,不难发现,如果有一个有向 ...

  5. Spring Boot 之 Spring Batch 批处理实践

    实践内容 从 MariaDB 一张表内读 10 万条记录,经处理后写到 MongoDB . 具体实现 1.新建 Spring Boot 应用,依赖如下: <!-- Web 应用 --> & ...

  6. MyBatis缓存机制(一级缓存,二级缓存)

    一,MyBatis一级缓存(本地缓存) My Batis 一级缓存存在于 SqlSession 的生命周期中,是SqlSession级别的缓存.在操作数据库时需要构造SqlSession对象,在对象中 ...

  7. sound of the genuine

    “There is something in every one of you that waits and listens for the sound of the genuine in yours ...

  8. React实践debug:JSX输出的限制(存疑)

    今天在练习React构建组件的时候遇到一个问题. 由于文档中反复提倡将组件尽可能按功能单位分解复用.在练习用React做一个todolist时候,我把todolist分解成两部分: class Tod ...

  9. NodeJS的概述

    1.NodeJS概述 基于谷歌V8引擎,运行在服务器端的环境 对比JS和NodeJS (1)JS运行在浏览器端,存在多种浏览器解释器,容易产生兼容性的问题:而NodeJS运行在服务器端,只有V8引擎一 ...

  10. shell脚本定时执行|关闭jar文件

    编写shell脚本,用于启动.关闭jar程序: #!/bin/bash #description: 启动重启server服务 #需要配置环境变量后才能直接使用java这些变量 export JAVA_ ...