public class Solution {
public String addBinary(String a, String b) {
StringBuilder sum = new StringBuilder();
int i = a.length() - 1;
int j = b.length() - 1;
int carry = 0;
while (i >= 0 || j >= 0 || carry == 1) {
int digitA = i < 0 ? 0 : a.charAt(i--) - '0';
int digitB = j < 0 ? 0 : b.charAt(j--) - '0';
sum.append((digitA + digitB + carry) % 2);
carry = (digitA + digitB + carry) / 2;
}
return sum.reverse().toString();
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode head = dummy;
int carry = 0;
while(l1 != null || l2 != null || carry > 0){
int num1 = (l1 == null) ? 0 : l1.val;
int num2 = (l2 == null) ? 0 : l2.val;
ListNode node = new ListNode((num1 + num2 + carry) % 10);
carry = (num1 + num2 + carry) / 10;
if(l1 != null) l1 = l1.next;
if(l2 != null) l2 = l2.next;
head.next = node;
head = head.next;
}
return dummy.next;
}
}
提交时候遇到的一个 bug 是 "9133" x "0" 的情况,要记得在 multiply() 函数中如果输入是 0 就直接返回 "0",这是乘法和加法运算的一个不同之处,否则会返回“0000”.
public class Solution {
public String multiply(String num1, String num2) {
String longStr = (num1.length() > num2.length()) ? num1 : num2;
String shortStr = (num1.length() > num2.length()) ? num2 : num1;
String rst = "";
int index = 0;
while(shortStr.length() - 1 - index >= 0){
int num = shortStr.charAt(shortStr.length() - 1 - index) - '0';
String cur = multiplyOne(longStr, num);
for(int i = index; i > 0; i--){
cur += "0";
}
rst = addTwo(rst, cur);
index ++;
}
return rst;
}
private String multiplyOne(String src, int num){
if(num == 0) return "0";
StringBuilder sb = new StringBuilder();
int carry = 0;
for(int i = src.length() - 1; i >= 0; i--){
int digit = src.charAt(i) - '0';
sb.append((digit * num + carry) % 10);
carry = (digit * num + carry) / 10;
}
if(carry > 0) sb.append(carry);
return sb.reverse().toString();
}
private String addTwo(String num1, String num2){
StringBuilder sb = new StringBuilder();
int i = num1.length() - 1;
int j = num2.length() - 1;
int carry = 0;
while(i >= 0 || j >= 0 || carry > 0){
int digit1 = (i >= 0) ? num1.charAt(i--) - '0': 0;
int digit2 = (j >= 0) ? num2.charAt(j--) - '0': 0;
sb.append((digit1 + digit2 + carry) % 10);
carry = (digit1 + digit2 + carry) / 10;
}
return sb.reverse().toString();
}
}
前面的做法对于每一个 digit 都做了一个乘法和加法操作,并没有特别好的利用到乘法的特点,中间操作过多而且生成了好多不必要的 String.
public class Solution {
public String multiply(String num1, String num2) {
if(num1 == null || num2 == null) return null;
int maxLength = num1.length() + num2.length();
int[] nums = new int[maxLength];
int i, j, product, carry;
for(i = num1.length() - 1; i >= 0; i--){
// 中间部分相当于多位数乘一位数,起始 carry 为 0
carry = 0;
for(j = num2.length() - 1; j >= 0; j--){
int a = num1.charAt(i) - '0';
int b = num2.charAt(j) - '0';
product = nums[i + j + 1] + a * b + carry;
nums[i + j + 1] = product % 10;
carry = product / 10;
}
// 循环结束,最左面为当前最高位数,如果 carry 还有就设过去
nums[i + j + 1] = carry;
}
StringBuilder sb = new StringBuilder();
int index = 0;
while(index < maxLength - 1 && nums[index] == 0) index ++;
while(index < maxLength) sb.append(nums[index++]);
return sb.toString();
}
}