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. iOS中的几种锁的总结,三种开启多线程的方式(GCD、NSOperation、NSThread)

    学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary OC中的几种锁 为什么要引入锁 ...

  2. python3语法学习第四天--字符串

    字符串:是python中的常用数据类型 Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用 访问字符串的值: 下标和分片截取 字符串的连接:‘+’ 字符串内置函数挺多,选 ...

  3. screen命令两种用法

    screen命令用法举例 screen命令,故名思议于屏幕有关. 1. screen用于关闭shell应用不退出 思路:创建一个单独的shell窗口,在此窗口中启动应用,然后把这个shell放置于后台 ...

  4. ubuntu docker相关错误记录

    执行下面命令 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - 报错: gpg: can't c ...

  5. Python内置函数列表

    函数 用途 abs() 返回数字绝对值 all() 判断给定的可迭代参数 iterable 中的所有元素是否都为 TRUE,如果是返回 True,否则返回 False any() 判断给定的可迭代参数 ...

  6. struts2 全局拦截器,显示请求方法和参数

    后台系统中应该需要一个功能那就是将每个请求的url地址和请求的参数log出来,方便系统调试和bug追踪,使用struts2时可以使用struts2的全局拦截器实现此功能: import java.ut ...

  7. java调用oracle存储过程返回多条结果集

    oracle版本:11g oracle存储过程,使用游标的方式返回多行.多列数据集合: CREATE OR REPLACE PROCEDURE SP_DATA_TEST( /*P_ID IN INT, ...

  8. Spring全家桶之springMVC(四)

      路径变量PathVariable PathVariable   Controller除了可以接收表单提交的数据之外,还可以获取url中携带的变量,即路径变量,此时需要使用@PathVariable ...

  9. hdu2138 How many prime numbers 米勒测试

    hdu2138 How many prime numbers #include <bits/stdc++.h> using namespace std; typedef long long ...

  10. [转载] Objectiv-C 入门一二三

    http://my.oschina.net/fuckphp/blog/92993 http://my.oschina.net/fuckphp/blog/93217 http://my.oschina. ...