LZ_Jaja

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"
  1 class Solution {
  2 public:
  3     string addBinary(string a, string b) {
  4         int la = a.length();
  5         int lb = b.length();
  6         string res = "";
  7         int car = 0;
  8         if(la <= lb) {
  9             for(int i = la - 1; i >= 0; i--) {
 10                 if(a[i] == '1' && b[lb - (la - i)] == '1') {
 11                     if(car == 1)
 12                         res = '1' + res;
 13                     else {
 14                         res = '0' + res;
 15                         car = 1;
 16                     }
 17                 }
 18                 else if(a[i] != b[lb - (la - i)]) {
 19                     if(car == 1)
 20                         res = '0' + res;
 21                     else
 22                         res = '1' + res;
 23                 }
 24                 else {
 25                     if(car == 1) {
 26                         res = '1' + res;
 27                         car = 0;
 28                     }
 29                     else
 30                         res = '0' + res;
 31                 }
 32             }
 33             for(int i = lb - la - 1; i >= 0; i--) {
 34                 if(b[i] == '1') {
 35                     if(car == 1)
 36                         res = '0' + res;
 37                     else
 38                         res = '1' + res;
 39                 }
 40                 else {
 41                     if(car == 1) {
 42                         res = '1' + res;
 43                         car = 0;
 44                     }
 45                     else
 46                         res = '0' + res;
 47                 }
 48             }
 49             if(car == 1) {
 50                 res = '1' + res;
 51             }
 52             return res;
 53         }
 54         else if(lb < la) {
 55             for(int i = lb - 1; i >= 0; i--) {
 56                 if(b[i] == '1' && a[la - (lb - i)] == '1') {
 57                     if(car == 1)
 58                         res = '1' + res;
 59                     else {
 60                         res = '0' + res;
 61                         car = 1;
 62                     }
 63                 }
 64                 else if(b[i] != a[la - (lb - i)]) {
 65                     if(car == 1)
 66                         res = '0' + res;
 67                     else
 68                         res = '1' + res;
 69                 }
 70                 else {
 71                     if(car == 1) {
 72                         res = '1' + res;
 73                         car = 0;
 74                     }
 75                     else
 76                         res = '0' + res;
 77                 }
 78             }
 79             for(int i = la - lb - 1; i >= 0; i--) {
 80                 if(a[i] == '1') {
 81                     if(car == 1)
 82                         res = '0' + res;
 83                     else
 84                         res = '1' + res;
 85                 }
 86                 else {
 87                     if(car == 1) {
 88                         res = '1' + res;
 89                         car = 0;
 90                     }
 91                     else
 92                         res = '0' + res;
 93                 }
 94             }
 95             if(car == 1) {
 96                 res = '1' + res;
 97             }
 98             return res;
 99         }
100     }
101 };

 

posted on 2018-07-31 22:06  LZ_Jaja  阅读(159)  评论(0编辑  收藏  举报