当前位置:网站首页>[leetcode] 573 complex multiplication (conversion between characters and numbers)
[leetcode] 573 complex multiplication (conversion between characters and numbers)
2022-06-12 21:23:00 【Young white horse】
537. Complex multiplication
The plural Can be expressed as a string , follow Real component + Imaginary part i" In the form of , And meet the following conditions :
Real componentIt's an integer , The value range is [-100, 100]Imaginary partIt's also an integer , The value range is [-100, 100]- i2 == -1
- Give you a complex number represented by two strings
num1andnum2, Please follow the plural form , Returns a string representing their product .
Example 1:
Input :num1 = "1+1i", num2 = "1+1i"
Output :"0+2i"
explain :(1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i , You need to convert it to 0+2i In the form of .
Example 2:
Input :num1 = "1+-1i", num2 = "1+-1i"
Output :"0+-2i"
explain :(1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i , You need to convert it to 0+-2i In the form of .
Tips :
num1andnum2Are valid plural representations .
Topic analysis :
The first time I saw the title , Thought it would be troublesome , But the string interception function is not well remembered , The function of converting characters into numbers has also been forgotten , So when I came and went, I thought of violence , All implementations are implemented in code , Instead of calling the function , Cause the code to stink and long
The idea of the topic is very simple , It's multiplication of plural numbers , It can be regarded as two one variable first-order equations , Multiply the corresponding coefficient , Then we get the final expression , So the trouble is how to extract numbers from a string of characters
Method 1 : violence
class Solution {
public:
string complexNumberMultiply(string num1, string num2) {
int n=num1.size(),m=num2.size();
int a=0,b=0,c=0,d=0;// Find four numbers
int flag1=0,flag2=0;// Mark the , It is convenient to find two numbers of the same string
int ans1=1,ans2=1;// The sign , See if it's a negative number or a positive number
int cnt1=0,cnt2=0;
char sss[100000];
for(int i=0;i<n;++i){
if(num1[i]>='0'&&num1[i]<='9'&&flag1==0){
// Find the first number
a=a*10+(num1[i]-'0');
}
else{
if(i!=0){
// Find the second number
if(num1[i]=='-'||num1[i]=='+'&&num1[i+1]=='-')// Determine whether the second number is positive or negative
ans1=-1;
flag1=1;
if(num1[i]>='0'&&num1[i]<='9'){
// Find the second number
b=b*10+(num1[i]-'0');
}
}
}
}
if(num1[0]=='-')// If the first character is -, So it's plural
a=-a;
b*=ans1;// Here is to see ans1 The state of , If there is no negative sign , that ans1 It won't change , conversely
for(int i=0;i<m;++i){
if(num2[i]>='0'&&num2[i]<='9'&&flag2==0){
c=c*10+(num2[i]-'0');
}
else{
if(i!=0){
if(num2[i]=='-'||num2[i]=='+'&&num2[i+1]=='-')
ans2=-1;
flag2=1;
if(num2[i]>='0'&&num2[i]<='9'){
d=d*10+(num2[i]-'0');
}
}
}
}
if(num2[0]=='-')
c=-c;
d*=ans2;
cnt1=a*c-b*d;
cnt2=a*d+c*b;
return to_string(cnt1)+"+"+to_string(cnt2)+"i";
}
};
Method 2 : Call function
Calling a function makes the code look simple , High readability
find(): Find the position where the current character first appears in the string
stoi(): Convert a string into a number
substr(): This function has two arguments , The first parameter represents the starting position of the intercepted string , The second parameter represents the length of the string to be intercepted
The second number is from pos1+1 Began to intercept , And the intercept length is pos1+1 To n-1(n-1 Is the last character of the string ), So the length is n-pos1-2
class Solution {
public:
string complexNumberMultiply(string num1, string num2) {
int n=num1.size(),m=num2.size();
int pos1=num1.find("+");
int pos2=num2.find("+");
int a = stoi(num1.substr(0,pos1)),b=stoi(num1.substr(pos1+1,n-pos1-2));
int c = stoi(num2.substr(0,pos2)),d=stoi(num2.substr(pos2+1,m-pos2-2));
int cnt1=a*c-b*d;
int cnt2=a*d+c*b;
return to_string(cnt1)+"+"+to_string(cnt2)+"i";
}
};
I have even uglier code , What kind of human suffering is this
class Solution {
public:
string complexNumberMultiply(string num1, string num2) {
vector<string>ve;
string xz="",xz1="",xz2="";
int n=num1.size(),m=num2.size();
int a=0,b=0,c=0,d=0;
int flag1=0,flag2=0;
int ans1=1,ans2=1;
int cnt1=0,cnt2=0;
char sss[100000];
for(int i=0;i<n;++i){
if(num1[i]>='0'&&num1[i]<='9'&&flag1==0){
a=a*10+(num1[i]-'0');
}
else{
if(i!=0){
if(num1[i]=='-'||num1[i]=='+'&&num1[i+1]=='-')
ans1=-1;
flag1=1;
if(num1[i]>='0'&&num1[i]<='9'){
b=b*10+(num1[i]-'0');
}
}
}
}
if(num1[0]=='-')
a=-a;
b*=ans1;
for(int i=0;i<m;++i){
if(num2[i]>='0'&&num2[i]<='9'&&flag2==0){
c=c*10+(num2[i]-'0');
}
else{
if(i!=0){
if(num2[i]=='-'||num2[i]=='+'&&num2[i+1]=='-')
ans2=-1;
flag2=1;
if(num2[i]>='0'&&num2[i]<='9'){
d=d*10+(num2[i]-'0');
}
}
}
}
if(num2[0]=='-')
c=-c;
d*=ans2;
cnt1=a*c-b*d;
cnt2=a*d+c*b;
cout<<a<< " "<<b<<" "<<c<<" "<<d<<endl;
cout<<a*d<<" "<<c*b<<endl;
cout<<cnt1<<" "<<cnt2<<endl;
int sum1=0,sum2=0;
string q1;
if(cnt1<0){
xz+="-";
cnt1=-cnt1;
}
if(cnt1==0){
xz+="0";
}
else{
while(cnt1){
int nnn=cnt1%10;
if(nnn==0)
q1="0";
if(nnn==1)
q1="1";
if(nnn==2)
q1="2";
if(nnn==3)
q1="3";
if(nnn==4)
q1="4";
if(nnn==5)
q1="5";
if(nnn==6)
q1="6";
if(nnn==7)
q1="7";
if(nnn==8)
q1="8";
if(nnn==9)
q1="9";
xz1+=q1;
cnt1/=10;
}
for(int j=xz1.size()-1;j>=0;--j)
xz+=xz1[j];
}
xz+="+";
if(cnt2!=0){
if(cnt2<0){
xz+="-";
cnt2=-cnt2;
}
string q;
while(cnt2){
int nnn1=cnt2%10;
if(nnn1==0)
q="0";
if(nnn1==1)
q="1";
if(nnn1==2)
q="2";
if(nnn1==3)
q="3";
if(nnn1==4)
q="4";
if(nnn1==5)
q="5";
if(nnn1==6)
q="6";
if(nnn1==7)
q="7";
if(nnn1==8)
q="8";
if(nnn1==9)
q="9";
xz2+=q;
cnt2/=10;
}
for(int j=xz2.size()-1;j>=0;--j)
xz+=xz2[j];
}else{
xz+="0";
}
xz+="i";
return xz;
}
};
| If you try , But it still has no effect , It shows that you are atoning for your previous laziness , As long as you stick to it, fight your head and blood , After this time, you will find , You climbed to an unimaginable height |
|---|
边栏推荐
- 大小端转换
- 金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试
- Data visualization - biaxial comparison effect
- China hydraulic press market trend report, technical innovation and market forecast
- Solution of multi machine room dynamic loop status network touch screen monitoring
- EU officially released the data act, Ukraine was attacked by DDoS again, kitchen appliance giant Meiya was attacked, internal data leakage network security weekly
- CUDA out of memory
- Vs2017 environmental issues
- 二分查找
- 在同花顺开户安全么,买股票怎么网上开户
猜你喜欢

Digital intelligence data depth | Bi goes down the altar? It's not that the market has declined, it's that the story has changed

Leetcode: 210. Programme II

GPU giant NVIDIA suffered a "devastating" network attack, and the number one malware shut down its botnet infrastructure | global network security hotspot on February 28

Data visualization - histogram

Scope and scope chain

ASCII 码对照表

Summary of machine learning materials

新品发布丨竣达智能综合环境监测终端

The salted fish has been transmitted for 5W times, and the latest spring recruit face-to-face test questions of bytes have been leaked

Distributed cloud service developer'allegro Xile technology 'received an angel round financing of US $3million
随机推荐
USB mechanical keyboard changed to Bluetooth Keyboard
String Basics
Integrated monitoring solution for power environment of small and medium-sized computer rooms
HR SaaS unicorn is about to emerge. Will the employee experience be the next explosive point?
大小端转换
Composer version degradation
China hydraulic press market trend report, technical innovation and market forecast
To delete a character from a string
Simplest ALV template
Bluetooth for Delphi xe7
New product release Junda intelligent integrated environmental monitoring terminal
The salted fish has been transmitted for 5W times, and the latest spring recruit face-to-face test questions of bytes have been leaked
选择排序
中小型机房动力环境综合监控解决方案
一级指针&二级指针知识点梳理
Distributed cloud service developer'allegro Xile technology 'received an angel round financing of US $3million
Module 8: Design message queue MySQL table for storing message data
@loadbalance annotation of resttemplate
风控建模十:传统建模方法存在的问题探讨及改进方法探索
#981 Time Based Key-Value Store