题目

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

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

题解

二进制加法都是从最低位(从右加到左)。所以对两个字符串要从最后一位开始加,如果遇见长度不一的情况,就把短的字符串高位补0.

每轮计算要加上进位,最后跳出循环后要坚持进位是否为1,以便更新结果。

代码如下(from discussion):

 1 public String addBinary(String a, String b) {
 2     int m = a.length();
 3     int n = b.length();
 4     int carry = 0;
 5     String res = "";
 6     // the final length of the result depends on the bigger length between a and b, 
 7     // (also the value of carry, if carry = 1, add "1" at the head of result, otherwise)
 8     int maxLen = Math.max(m, n);
 9     for (int i = 0; i < maxLen; i++) {
         // start from last char of a and b
         // notice that left side is int and right side is char
         // so we need to  minus the decimal value of '0'
         int p=0,q=0;
         if(i<m)
             p = a.charAt(m-1-i) - '0';
         else
             p = 0;
         
         if(i<n)
             q = b.charAt(n-1-i)-'0';
         else
             q = 0;
             
         int tmp = p + q + carry;
         carry = tmp / 2;
         res += tmp % 2;
     }
     return (carry == 0) ? res : "1" + res;
     }

Add Binary Leetcode java的更多相关文章

  1. LeetCode算法题-Add Binary(Java实现)

    这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...

  2. Add Binary <leetcode>

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

  3. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  4. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

  5. leetcode解题:Add binary问题

    顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  8. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  9. leetcode笔记:Add Binary

    一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...

随机推荐

  1. UI控件(ios)---UIImageView

    在实现网络异步存储中,突然发现对控件UIImageView有点生疏了,在这里复习一下. UIImageView,顾名思义是用来放置image的. 1.初始化UIImageView   UIImageV ...

  2. BZOJ3772精神污染

    参见http://blog.csdn.net/popoqqq/article/details/43122821 #include<bits/stdc++.h> using namespac ...

  3. CCCC 成都信息工程大学游记

    晚上刷智障25人本,刷到深夜四点,然后迷迷糊糊8点钟起床上车睡觉,然后就到了信息工程大学. 然后开始抢衣服,抢完衣服就开始拍照. 对了,这个学校看了下地图,好小呀,不过妹子好多呀. 然后就被老师带进机 ...

  4. 回顾JavsScript对象的克隆

    JS对象的数据类型分为两大类:原始类型(string, boolean, number,undefined, function)和 对象类型(array, object, null). 1.浅度克隆 ...

  5. POJ 1386 Play on Words (有向图欧拉路径判定)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8768   Accepted: 3065 Des ...

  6. Android 开源库获取途径整理

    介绍眼下收藏 Android 开源库比較多的 GitHub 项目.站点.Twitter.App 及怎样获取最新的 Android 开源库. 微信号: 1. GitHub Android 开源项目汇总 ...

  7. Revit API遍历系统族布置喷头

    系统族可以通过内参遍历,遍历出来是个FamilySymbol喷头属于系统族,但不能通过NewDuct();类似这样的方法布置.必须使用 NewFamilyInstance() );           ...

  8. Snmp学习总结(二)——WinXP安装和配置SNMP

    一.安装SNMP 今天讲解一下在XP下安装SNMP协议,安装步骤如下:

  9. VC2010中"Include Directories" 和 "Additional Include Directories"的区别

    右键一个Project,可以发现有两个地方设置Include的相关目录: 1. VC++ Directories -> Include Directories2. C/C++ -> Gen ...

  10. lufylegend:图形变形2

    下面来详细讲解一下drawtriangles函数的使用方法.并且使用drawtriangles函数实现下面这种处理效果 因为这个方法是从AS3移植而来,所以它的使用方法和AS3基本一致,这里是AS3的 ...