给出两个用字符串表示的二进制数,返回他们的和(也用字符串表示)
例如:
a ="11"
b ="1"
返回"100".
b ="1"
返回"100".
public class Solution { public String addBinary(String a, String b) { if(a==null){ return b; } if(b==null){ return a; } String tempa=""; String tempb=""; for(int i=0;i<a.length();i++){ tempa=a.charAt(i)+tempa; } a=tempa; for(int i=0;i<b.length();i++){ tempb=b.charAt(i)+tempb; } b=tempb; int index=0; String res=""; int numa=0,numb=0; for(int i=0;i<a.length()||i<b.length()||index!=0;i++){ if(i<a.length()){ numa=Integer.parseInt(String.valueOf(a.charAt(i))); } else{ numa=0; } if(i<b.length()){ numb=Integer.parseInt(String.valueOf(b.charAt(i))); } else{ numb=0; } if(numa+numb+index<2){ res=String.valueOf(numa+numb+index)+res; index=0; } else{ res=String.valueOf((numa+numb+index)%2)+res; index=1; } } return res; } }
public String addBinary(String a, String b) { int n1 = a.length() - 1; int n2 = b.length() - 1; StringBuilder sb = new StringBuilder(); int d = 0; while(n1>=0 || n2>=0 || d!=0){ int a1 = n1>=0 ? (a.charAt(n1--)=='1' ? 1 : 0) : 0; int b1 = n2>=0 ? (b.charAt(n2--)=='1' ? 1 : 0) : 0; sb.append(a1^b1^d); d = (a1+b1+d)>1 ? 1 : 0; } return sb.reverse().toString(); }
public class Solution { public String addBinary(String a, String b) { int len = a.length() - b.length(); StringBuilder sb = new StringBuilder(); if (len != 0) { for (int i = 0; i < Math.abs(len); i++) { sb.append("0"); } } if (len > 0) { b = sb.toString() + b; } else { a = sb.toString() + a; } sb = new StringBuilder(); char carry = '0'; for (int i = a.length() - 1; i >= 0; i--) { if (a.charAt(i) == '0' && b.charAt(i) == '0') { sb.insert(0, carry); carry = '0'; }else if (a.charAt(i) == b.charAt(i)) { sb.insert(0, carry); carry = '1'; }else if (a.charAt(i) != b.charAt(i) && carry == '0') { sb.insert(0, '1'); carry = '0'; }else if (a.charAt(i) != b.charAt(i) && carry == '1') { sb.insert(0, '0'); carry = '1'; } } if (carry == '1') sb.insert(0, '1'); return sb.toString(); } }
public String addBinary(String a, String b) { if (a==null ||a.length()==0) return b; if (b==null||b.length()==0) return a; int i=a.length()-1; int j=b.length()-1; int carry=0; StringBuilder res=new StringBuilder(); while (i>=0&&j>=0){ int digit=(a.charAt(i)-'0'+b.charAt(j)-'0'+carry); carry=digit/2; digit%=2; res.append(digit); i--; j--; } while (i>=0){ int digit=(a.charAt(i)-'0'+carry); carry=digit/2; digit%=2; res.append(digit); i--; } while (j>=0){ int digit=(b.charAt(j)-'0'+carry); carry=digit/2; digit%=2; res.append(digit); j--; } if (carry>0){ res.append(carry); } return res.reverse().toString(); }
public class Solution { public String addBinary(String a, String b) { StringBuilder sb = new StringBuilder(); int i = a.length() - 1, j = b.length() -1, carry = 0; while (i >= 0 || j >= 0) { int sum = carry; if (j >= 0) sum += b.charAt(j--) - '0'; if (i >= 0) sum += a.charAt(i--) - '0'; sb.append(sum % 2); carry = sum / 2; } if (carry != 0) sb.append(carry); return sb.reverse().toString(); } }
Computation from string usually can be simplified by using a carry as such.
public class Solution { public static String addBinary(String a, String b) { while (a.length() > b.length()) b = "0" + b; while (a.length() < b.length()) a = "0" + a; String res = ""; boolean jinwei = false; for (int i = a.length() - 1; i >= 0; i -- ) { if(a.charAt(i) - '0' + b.charAt(i) - '0' == 1) { res = jinwei ? "0" + res : "1" + res; } else if(a.charAt(i) - '0' + b.charAt(i) - '0' == 2) { res = jinwei ? "1" + res : "0" + res; jinwei = true; } else { res = jinwei ? "1" + res : "0" + res; jinwei = false; } } if(jinwei) res = "1" + res; return res; } }