一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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 ret;
        int alen = a.length();
        int blen = b.length();
        int i = alen-1 , j = blen-1;//从后往前加
        int carry = 0;
        while(i>=0 &&j>=0)//同位上都有数
        {
            int sum = a[i]-'0'+b[j]-'0'+carry;
            ret+=sum%2+'0';
            carry = sum>=2?1:0;//考虑进位
            i--;j--;
        }
        while(i==-1&&j>=0)//a到最高位了,b还有
        {
            int sum = b[j]-'0'+carry;
            ret+=sum%2+'0';
            carry = sum==2?1:0;//考虑进位
            j--;
        }
        while(i>=0&&j==-1)//b到最高位了,a还有
        {
            int sum = a[i]-'0'+carry;
            ret+=sum%2+'0';
            carry = sum==2?1:0;//考虑到进位
            i--;
        }
        if(carry==1) ret+='1';
        reverse(ret.begin(),ret.end());//注意对结果进行翻转,才是正确的结果
        return ret;
    }
};

【一天一道LeetCode】#67. Add Binary的更多相关文章

  1. LeetCode 67. Add Binary (二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  2. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  3. LeetCode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  4. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  5. (String) leetcode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  6. [leetcode]67. Add Binary 二进制加法

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  7. LeetCode - 67. Add Binary(4ms)

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  8. leetcode 67. Add Binary (高精度加法)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

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

    翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...

  10. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

随机推荐

  1. Node.js TTY

    稳定性: 2 - 不稳定 tty 模块包含 tty.ReadStream 和 tty.WriteStream 类.多数情况下,你不必直接使用这个模块. 当 node 检测到自己正运行于 TTY 上下文 ...

  2. Android简易实战教程--第五十一话《使用Handler实现增加、减少、暂停计数》

    转载博客请注明出处:道龙的博客 之前,写过一篇使用异步任务AysncTask实现倒计时的小案例,喜欢的话可以参考博客:Android简易实战教程--第三十三话< AsyncTask异步倒计时&g ...

  3. Django中过期@cache_page中缓存的views数据

    django的缓存系统中,cache_page 这个装饰器非常好用,只要添加一个装饰器就可以缓存views的响应内容,但是django没有提供过期这个views缓存数据的功能. @cache_page ...

  4. 让你的代码量减少3倍!使用kotlin开发Android(二) --秘笈!扩展函数

    本文承接上一篇文章:让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程 本文同步自博主的私人博客wing的地方酒馆 上一节说到,kotlin可以省去getter,se ...

  5. Android Multimedia框架总结(十五)Camera框架之Camera2补充

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52751055 前言:监于5.0之 ...

  6. WebView 的使用案例

    package com.example.day20_webview; import android.os.Bundle; import android.annotation.SuppressLint; ...

  7. Apache DbUtils 探秘

    听说Apache的DbUtils很好用,而且是对jdbc的简单的封装,所以可以和jdbc一起混搭,多以今天就来尝试一下,关于DbUtils 是如何使用的. 准备 数据库: MySQL 依赖: mysq ...

  8. ORACLE数据库学习之备份与恢复

     oracle数据库的备份与恢复 第一部分:数据库的备份 备份的必要性 因为各种人为或外界的因素可能会造成数据库中灾难性的数据丢失,为了保证数据库中数据的安全,必须采取备份措施保证RDBMS中包含 ...

  9. JDBC-数据库的编程(一)

    因为我使用的mysql数据库客户端程序是workBench,所以会用workBench来进行讲解. create table tbl_user( id int(11) unsigned not nul ...

  10. LM算法与非线性最小二乘问题

    摘录的一篇有关求解非线性最小二乘问题的算法--LM算法的文章,当中也加入了一些我个人在求解高精度最小二乘问题时候的一些感触: LM算法,全称为Levenberg-Marquard算法,它可用于解决非线 ...