当前位置:网站首页>[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 |
|---|
边栏推荐
- Binary search
- #886 Possible Bipartition
- Library cache lock brought by add trandata
- What did new do
- What are the barriers that Huawei terminals need to cross if they want to rely on the intelligent turnover of the whole house?
- 模块八:设计消息队列存储消息数据的MySQL表
- @loadbalance annotation of resttemplate
- Teamwork collaboration application experience sharing | community essay solicitation
- Access control system based on RFID
- torch. nn. Linear() function
猜你喜欢

leetcode:207. Class Schedule Card

torch. Finfo function

字符串基础知识

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

Test basis: unit test

Integrated monitoring solution for power environment of small and medium-sized computer rooms

leetcode:210. Schedule II

Design and practice of Hudi bucket index in byte skipping

一级指针&二级指针知识点梳理

竣达技术丨适用于“科士达”智能精密空调网络监控
随机推荐
结构体知识点all in
USB机械键盘改蓝牙键盘
Shell language
好数对的求解
The year of the outbreak of financial innovation! All dtinsight products of kangaroo cloud data stack have passed the special test of Xinchuang of ICT Institute
remote: Support for password authentication was removed on August 13, 2021
机器学习资料汇总
leetcode:210. 课程表 II
What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8
Pointer and array & pointer and const & struct and Const
Scope and scope chain
Mxnet record IO details
Preliminary understanding of regular expressions (regex)
leetcode:210. Schedule II
Distributed cloud service developer'allegro Xile technology 'received an angel round financing of US $3million
The service did not report any errors MySQL
Market trend report, technical innovation and market forecast of hydraulic chain hoist in China
Yanghui triangle code implementation
JS deep and shallow copy
Simplest ALV template