给定两个用字符串表示的二进制数,返回他们的和。
数据范围:字符串长度满足 ,字符串中只含有 0 和 1,且保证除 0 以外的二进制数没有前导零的情况。
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ public String binaryAdd (String A, String B) { // write code here int n = A.length(), m = B.length(); // 竖式求和 int carry = 0; // 进位 Stack<Integer> stack = new Stack<>(); for(int i = n - 1, j = m - 1; i >= 0 || j >= 0; i--, j--){ int sum = (i >= 0? A.charAt(i) - '0': 0) + (j >= 0? B.charAt(j) - '0': 0) + carry; stack.push(sum % 2); carry = sum / 2; } if(carry > 0) { stack.push(carry); } StringBuilder res = new StringBuilder(); while(!stack.isEmpty()){ res.append(stack.pop()); } return res.toString(); } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ function binaryAdd(A, B) { // write code here let num_a = parseInt(A, 2); let num_b = parseInt(B, 2); let result = num_a + num_b; return result.toString(2); } module.exports = { binaryAdd: binaryAdd, };
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ string binaryAdd(string A, string B) { // write code here int n1 = A.length(); int n2 = B.length(); int i=n1-1, j=n2-1; string res; int carry = 0; while(i>=0 && j>=0) { int a = A[i--] - '0'; int b = B[j--] - '0'; int num = (a + b + carry) % 2; carry = (a + b + carry) / 2; res += num + '0'; } while(i>=0) { // res += A[i--] + carry; int a = A[i--] - '0'; int num = a + carry; res += num % 2 + '0'; carry = num / 2; } while(j>=0) { int b = B[j--] - '0'; int num = b + carry; res += num % 2 + '0'; carry = num / 2; } if(carry > 0) res += carry + '0'; reverse(res.begin(), res.end()); return res; } };
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ public String binaryAdd (String A, String B) { // write code here int lA = A.length(); int lB = B.length(); int len = Math.max(lA, lB) + 1; char[] res = new char[len]; int digit = 0; for(int i = 0; i < len; i++) { int val1 = 0; if(lA - i - 1 >= 0) val1 = A.charAt(lA - i - 1) - '0'; int val2 = 0; if(lB - i - 1 >= 0) val2 = B.charAt(lB - i - 1) - '0'; int val = val1 + val2 + digit; digit = val/2; val = val % 2; res[len - i - 1] = (char) (val + '0'); } // 去除前导0 if(res[0] == '0') return new String(res, 1, len - 1); return new String(res); } }
类似于大数加法 import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ public String binaryAdd (String A, String B) { if(A == "0"){ return B; }else if(B=="0"){ return A; } // write code here char[] as = A.toCharArray(); char[] bs = B.toCharArray(); int c = 0; int p1 = as.length-1; int p2 = bs.length-1; int num1 = 0; int num2 = 0; int sum = 0; StringBuffer sb = new StringBuffer(); while(p1 >=0 || p2 >= 0){ if(p1 < 0){ num1 = 0; }else{ num1 = as[p1--] - '0'; } if(p2 < 0){ num2 = 0; }else{ num2 = bs[p2--] - '0'; } sum = num1 + num2 + c; if(sum >= 2){ sb.append(sum % 2); c = sum / 2; }else{ sb.append(sum); c = 0; } } if(c > 0){ sb.append(c); } return sb.reverse().toString(); } }
import java.util.*; import java.math.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ public String binaryAdd (String A, String B) { // write code here //使用BigInteger将2进制转为10禁止 BigInteger num1=new BigInteger(A,2); BigInteger num2=new BigInteger(B,2); //将十进制转为2进制字符串 BigInteger num=num1.add(num2); String str=num.toString(2); return str; } }
#include <string> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ string binaryAdd(string A, string B) { // write code here string result; int carry=0; int i=A.size()-1; int j=B.size()-1; while(i>=0||j>=0||carry!=0) { int digitA=i>=0?A[i]-'0':0; int digitB=j>=0?B[j]-'0':0; int sum=digitA+digitB+carry; carry=sum>=2?1:0; sum=sum>=2?sum-2:sum; result+=to_string(sum); i--; j--; } reverse(result.begin(), result.end()); return result; } };
public String binaryAdd (String A, String B) { // write code here char[] cha = A.toCharArray(); char[] chb = B.toCharArray(); int i=0; int add=0; String res=""; while(cha.length-1-i>=0 || chb.length-1-i>=0){ int a=0; int b=0; if(cha.length-1-i>=0){ a=cha[cha.length-1-i]-'0'; } if(chb.length-1-i>=0){ b=chb[chb.length-1-i]-'0'; } int temp=a^b^add; res=temp+res; add=(a&b)|(a&add)|(b&add); i++; } return add==1?add+res:res; }
import java.util.*; import java.math.BigInteger; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ public String binaryAdd (String A, String B) { BigInteger a = new BigInteger(A,2); BigInteger b = new BigInteger(B,2); return (a.add(b)).toString(2); } }
# @param A string字符串 # @param B string字符串 # @return string字符串 # class Solution: def binaryAdd(self , A: str, B: str) -> str: # write code here a = int(A,2) b = int(B,2) c = a + b d = bin(int(str(c),10)) #print(d[2:]) return d[2:]
import java.util.*; public class Solution { public String binaryAdd (String A, String B) { int i = A.length()-1, j = B.length()-1; StringBuilder res = new StringBuilder(); int carry = 0, val = 0; while (i >= 0 || j >= 0) { val = 0; val += carry; if (i >= 0) val += A.charAt(i--)-'0'; if (j >= 0) val += B.charAt(j--)-'0'; carry = val / 2; val %= 2; res.append(val); } if (carry > 0) res.append(carry); return res.reverse().toString(); } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @param B string字符串 * @return string字符串 */ function binaryAdd( A , B ) { // write code here //利用大数加法 let f=0; let sum=0; let str=''; let maxLength = Math.max(A.length,B.length); let a = A.padStart(maxLength,0); let b = B.padStart(maxLength,0); for(let i=maxLength - 1;i>=0;i--){ sum = parseInt(a[i])+parseInt(b[i])+f; f=Math.floor(sum/2);//遇2则进位 str = sum%2+str; } if(f==1){ str = f+str; } return str; } module.exports = { binaryAdd : binaryAdd };