No.67 Add Binary

No.67 Add Binary

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

For example,
a = "11"
b = "1"
Return "100".

  与No.2 Add Two Numbers的思路非常相似,不同在:对其进行运算时,其实是要逆向相加的!!!因为边界控制的原因,可先将字符串逆序reverse( )。

 1 #include "stdafx.h"
 2 #include <string>
 3 #include <iostream>
 4 #include <algorithm>
 5 class Solution
 6 {
 7 public:
 8     string addBinary(string a, string b)
 9     {
10         string result("");
11         reverse(a.begin(),a.end());//注意运算顺序的问题!!!
12         reverse(b.begin(),b.end());
13
14         auto ita = a.begin();
15         auto itb = b.begin();
16         int data1 = 0;
17         int data2 = 0;
18         int carry = 0;
19         int sum = 0;
20
21         while(ita != a.end() || itb != b.end())
22         {//从尾向前遍历
23             data1 = 0;
24             data2 = 0;
25             if(ita != a.end())
26             {
27                 data1 = *ita -‘0‘;
28                 ita++;
29             }
30             if(itb != b.end())
31             {
32                 data2 = *itb -‘0‘;
33                 itb++;
34             }
35             sum = data1+data2+carry;
36             //头插
37             result.insert(result.begin(),sum%2+‘0‘);//!!!格式问题
38             carry = sum/2;
39         }
40         if(carry== 1)
41             result.insert(result.begin(),‘1‘);
42
43         return result;
44     }
45 };
46 int main()
47 {
48     Solution sol;
49     cout << sol.addBinary(string("11"),string("1"))<<endl;
50     cout << sol.addBinary(string(""),string("1"))<<endl;
51     cout << sol.addBinary(string("111"),string("1111"))<<endl;
52     cout << sol.addBinary(string("111"),string("111"))<<endl;
53     cout << sol.addBinary(string(""),string(""))<<endl;
54     return 0;
55 }
时间: 2024-10-06 20:49:49

No.67 Add Binary的相关文章

67. Add Binary【LeetCode】

67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 1 public class Solution { 2 public String addBinary(String a, String b) { 3 String res ="";

leetCode 67. Add Binary 字符串

67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 1.将两个字符串按数组相加得到新数组. 2.将新数组转换成结果. 代码如下: class Solution { public:     string addBinary(string a, str

LeeCode from 0 —— 67. Add Binary

67. Add Binary 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. 解题思路: 1)将每一位字符都转换为数字,将两个字符串对应位的数字相加.若需要进位,则保存进位的值并在下一次相加时加上进位.对应位若不存在则设为0,相加. 2)若两个字符串所有对应位相加结

67. Add Binary

1. 问题描述 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". Tags: Math String 2. 解答思路 3. 代码 1 #include <stack> 2 #include <string> 3 #include <cctype&g

LeetCode 67. Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 这个题目只要注意各种情况你就成功了一大半,特别要注意的是对进位赋值后可能产生的变化,以及最后一位进位为1时, 我们要把这个1插进来.同时注意字符串的低位是我们数值的高位 class Solution { public: string

【LeetCode】67 - Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". Solution:二进制加法,和为2进1,和为3进1留1: 1 class Solution { 2 public: 3 string addBinary(string a, string b) { 4 int sizea=a.siz

[leedcode 67] Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { //时间复杂度O(n).空间复杂度O(1) //注意:1.心细,注意循环的写法,简化的方式如非

LeetCode 67 Add Binary(二进制相加)(*)

翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 分析 我一開始写了这个算法,尽管实现

【LeetCode】67. Add Binary 解题小结

题目: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". class Solution { public: string addBinary(string a, string b) { string res(a.size()+b.size(),'0'); int i = a.size