leetcode67 二進位制加法
阿新 • • 發佈:2018-12-24
//leetcode67 二進位制數相加
public static void main(String argv[]){
String a="1111";
String b="1111";
System.out.println(addBinary(a,b));
}
public static String addBinary(String a, String b) {
String c="";
int alen=a.length();
int blen=b.length();
int flag=0;
String pp="";
//位數不同,給字串補0
if(alen>blen){//給b補0
int ablen=alen-blen;
for(int p=0;p<ablen;p++){
pp=pp+"0";
}
b=pp+b;
blen=alen;
}else{
int ablen=blen-alen;
for(int p=0;p<ablen;p++){
pp=pp+"0";
}
a=pp+a;
alen=blen;
}
public static void main(String argv[]){
String a="1111";
String b="1111";
System.out.println(addBinary(a,b));
}
public static String addBinary(String a, String b) {
String c="";
int alen=a.length();
int blen=b.length();
int flag=0;
String pp="";
//位數不同,給字串補0
if(alen>blen){//給b補0
int ablen=alen-blen;
for(int p=0;p<ablen;p++){
pp=pp+"0";
}
b=pp+b;
blen=alen;
}else{
int ablen=blen-alen;
for(int p=0;p<ablen;p++){
pp=pp+"0";
}
a=pp+a;
alen=blen;
}