67. Add Binary

Posted wilderness

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了67. Add Binary相关的知识,希望对你有一定的参考价值。

计算二进制

 

java:

 1 class Solution {
 2     public String addBinary(String a, String b) {
 3         int i = a.length()-1;
 4         int j = b.length()-1;
 5         char[] flag = {‘0‘, ‘1‘};
 6         int an = 0, bn = 0;
 7         char ac = ‘0‘, bc = ‘0‘;
 8         int carry = 0, bit_res = 0;
 9         StringBuilder res = new StringBuilder();
10         
11         while(i>=0||j>=0||carry!=0){
12             ac = i>=0?a.charAt(i):‘0‘;
13             bc = j>=0?b.charAt(j):‘0‘;
14             bit_res = carry+ ac-‘0‘+bc-‘0‘;
15             res.insert(0, flag[bit_res%2]);
16             carry= bit_res / 2;
17             i--;
18             j--;
19         }
20         return res.toString();
21         
22     }
23 }

 

以上是关于67. Add Binary的主要内容,如果未能解决你的问题,请参考以下文章

leetcode67. Add Binary

67. Add Binary

LeetCode----67. Add Binary(java)

67. Add Binary

67. Add Binary

leetcode67. Add Binary