题目链接:https://leetcode-cn.com/problems/complex-number-multiplication/

前言:很基础的题目,主要是字符串和数字之间的互相转化。

一. 函数学习

1.字符串按子串分割

/* +、*、|、\等符号在正则表达示中有相应的不同意义,使用时要进行转义处理,否则会报错。*/
String[] str = num1.split("\\+");
/*同时进行两种不同分割时也可以使用|。*/
 String[] str = num1.split("\\+|i");

2.String和int类型互相转换

/*String类型转int,两种方式*/
int a = Integer.valueOf(str[0]);
int b = Integer.parseInt(str[0]);
/*int类型转String,三种方式*/
String s1 = String.valueOf(a);
String s2 = Integer.toString(a);
String s3 = "" + a;

3.格式化字符串,文本处理

String.format("%d+%di", a * c - b * d, a * d + b * c);

二. 题解思路

字符串分割后转为int类型进行运算,然后直接使用format转字符串。

三. 心得

int转string其实是冗余的一步,返回时直接使用+连接就会自动转过去。共勉。

posted on 2022-02-25 10:40  SelmaS  阅读(54)  评论(0编辑  收藏  举报