题目

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. 正睿OI 提高 Day1T3 ZYB玩字符串(DP)

    题目链接 设可能的答案串为p,长为len.p一定是s的一个子串且len|n. 虽然一些p在s中可能被断成若干段,但删掉其中的若干段后,这段区间一定会被全部消掉. 于是枚举p后,可以用f[i][j]表示 ...

  2. vue项目开发之v-for列表渲染的坑

    不知道大家在用vue开发的过程中有没有遇到过在使用v-for的时候会出现大片的黄色警告,比如下图: 其实这是因为没有写key的原因 :key是为vue的响应式渲染提供方法,在列表中单条数据改变的情况下 ...

  3. 使用 Python 登录网站(转)

    对于大部分论坛,我们想要抓取其中的帖子分析,首先需要登录,否则无法查看. 这是因为 HTTP 协议是一个无状态(Stateless)的协议,服务器如何知道当前请求连接的用户是否已经登录了呢?有两种方式 ...

  4. USB with NXP Microcontrollers

    USB with NXP Microcontrollers NXP Advantages NXP's microcontroller portfolio features the latest USB ...

  5. CentOS 6.8 搭建 Git 代码托管系统 Gitea

    [荐] Gitea:Git with a cup of tea,在 Gogs 基础上,发展起来的 自助 Git 服务系统.Gogs是一个个人维护的版本,而Gitea是一个社区组织维护的,版本迭代更新快 ...

  6. Snmp学习总结(六)——linux下安装和配置SNMP

    一.安装SNMP 1.1.下载Net-SNMP的源代码 选择一个SNMP版本,比如5.7.1,下载地址如下:http://sourceforge.net/projects/net-snmp/files ...

  7. ASP.NET MVC遍历ModelState的错误信息

    在ASP.NET MVC中,ModelState中包含了验证失败的错误信息,具体被存储在ModelState.Values[i].Errors[j].ErrorMessage属性中.当然,通过打断点, ...

  8. Java异常(三) 《Java Puzzles》中关于异常的几个谜题

    概要 本章介绍<Java Puzzles>中关于异常的几个谜题.这一章都是以代码为例,相比上一章看起来更有意思.内容包括:谜题1: 优柔寡断谜题2: 极端不可思议谜题3: 不受欢迎的宾客谜 ...

  9. TStream实现多表提交

    TStream实现多表提交 function TynFiredac.SaveDatas(const ATableName, ATableName2: string; ADeltas: TStream; ...

  10. Nginx rewrite URL examples with and without redirect address

    原文地址: http://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-addre ...