LeetCode Add Binary
阿新 • • 發佈:2018-12-23
Problem
Given two binary strings, return their sum (also a binary string).
For example, a = “11” b = “1” Return “100”.
題目的意思就是按二進位制的方式計算兩個數相加。
Java 實現
package com.coderli.leetcode.algorithms.easy;
/**
* Given two binary strings, return their sum (also a binary string).
* <p>
* For example,
* a = "11"
* b = "1"
* Return "100".
*
* @author OneCoder 2017-11-07 21:26
*/
public class AddBinary {
public static void main(String[] args) {
AddBinary addBinary = new AddBinary();
System.out.println(addBinary.addBinary("11", "1"));
System.out.println(addBinary.addBinary("11", "11"));
System.out.println(addBinary.addBinary("11", "111"));
System.out.println(addBinary.addBinary("1", "111"));
System.out.println(addBinary.addBinary("0", "0"));
}
public String addBinary(String a, String b) {
int aLength = a.length();
int bLength = b.length();
int length = aLength >= bLength ? aLength : bLength ;
char[] chars = new char[length];
int carryDigit = 0;
for (int i = 0; i < length; i++) {
int tempSum = 0;
if (i < aLength && i < bLength) {
tempSum = a.charAt(aLength - 1 - i) + b.charAt(bLength - 1 - i) + carryDigit - 96;
} else if (aLength > bLength) {
tempSum = a.charAt(aLength - 1 - i) + carryDigit - 48;
} else if (aLength < bLength) {
tempSum = b.charAt(bLength - 1 - i) + carryDigit - 48;
}
carryDigit = tempSum >= 2 ? 1 : 0;
chars[length - i - 1] = tempSum % 2 == 0 ? (char) 48 : (char) 49;
}
String result = new String(chars);
if (carryDigit != 0) {
result = "1" + result;
}
return result;
}
}
分析
跟題目Plus One很相近,只是把固定的加1變成了另一個字串。處理方式都完全一致。只是額外需要注意的是char轉int 利用 ‘0’ = 48 額外計算一下即可。