题目

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. Java 操纵XML之修改XML文件

    Java 操纵XML之修改XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...

  2. [CC-XXOR]Chef and Easy Problem

    [CC-XXOR]Chef and Easy Problem 题目大意: 给你一个长度为\(n(n\le10^5)\)的序列\(A(A_i<2^{31})\).\(m(m\le10^5)\)次询 ...

  3. bzoj4289 Tax

    Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...

  4. 吴恩达-coursera-机器学习-week1

    一.引言(Introduction) 1.1 欢迎 1.2 机器学习是什么? 1.3 监督学习 1.4 无监督学习 二.单变量线性回归(Linear Regression with One Varia ...

  5. sqlserver -- 查看当前数据库的数据表(备忘)

    @_@||... 记性不好,备忘... 语句功能:查看当前数据库的所有表(根据所需,进行语句改写即可) SELECT * FROM sysobjects WHERE (xtype = 'U') 备注: ...

  6. 喵哈哈村的魔法考试 Round #2 (Div.2) 题解

    喵哈哈村的魔法考试 Round #2 (Div.2) 题解 A.喵哈哈村的战争 题解: 这道题就是for一遍,统计每个村子的战斗力的和,然后统计哪个村子的战斗力和大一点就好了. 唯一的坑点,就是这道题 ...

  7. Codeforces Round #258 (Div. 2) A. Game With Sticks 水题

    A. Game With Sticks 题目连接: http://codeforces.com/contest/451/problem/A Description After winning gold ...

  8. OpenNI2 + NiTE2开发教程

    发现了一个非常不错的关于自然交互OpeNI2+NiTE2的资源,非常感谢Heresy,这里分享链接: OpenNI 2.x 教学文章(转载自:Heresy博客,地址:https://kheresy.w ...

  9. tapd

    注册公司-TAPD   https://www.tapd.cn/registers/register_company_finish/maolingzhi@meizu.com?type=email_va ...

  10. Java+Windows+ffmpeg实现视频转换

    最近由于项目需要,研究了一下如何用Java实现视频转换,“着实”废了点心思,整理整理,写出给自己备忘下. 思路 由于之前没有没法过相关功能的经验,一开始来真不知道从哪里入手.当然,这个解决,googl ...