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)若两个字符串所有对应位相加结束,判断最后一个进位是否为1,若为1,则在字符串最前面补1.

C++代码如下:

class Solution {
public:
string addBinary(string a, string b) {
                        int n=a.length()-1;
                        int m=b.length()-1;
                        int p=0;
                        string c="" ;
                        while(n>=0 || m>=0){
                                   int i= n>=0 ? a[n--]-‘0‘:0;
                                   int j= m>=0 ? b[m--]-‘0‘:0;
                                   int sum=i+j+p;
                                  c=to_string(sum%2)+c;
                                  p=sum/2;
                       }
                      c= p==1 ? ‘1‘+c :c;
                      return c;

}
};

原文地址:https://www.cnblogs.com/ssml/p/9301825.html

时间: 2024-10-04 17:27:54

LeeCode from 0 —— 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

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 "

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