题目

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. BZOJ.1901.Dynamic Rankings(整体二分)

    题目链接 BZOJ 洛谷 (以下是口胡) 对于多组的询问.修改,我们可以发现: 假设有对p1,p2,p3...的询问,在这之前有对p0的修改(比如+1),且p0<=p1,p2,p3...,那么我 ...

  2. B+/-Tree原理

    B-Tree介绍 B-Tree是一种多路搜索树(并不是二叉的):       1.定义任意非叶子结点最多只有M个儿子:且M>2:       2.根结点的儿子数为[2, M]:       3. ...

  3. C++ Curiously Recurring Template Prattern(CRTP)例程

    简单介绍和例子请参考:C++ 惯用法 CRTP 简介 下面例子为兼顾CRTP和多态的例子. #include <iostream> #include <vector> usin ...

  4. Jmeter自定义编写Java代码调用socket通信

    一.前言 最近需要测试一款手机游戏的性能,找不到啥录制脚本的工具,然后,另外想办法.性能测试实际上就是对服务器的承载能力的测试,和各种类型的手机客户端没有啥多大关系,手机再好,服务器负载不了,也不能够 ...

  5. CentOS安装CLI

    #使用root账号 vim /etc/yum.repos.d/epel.repo [epel] name=epel baseurl=http://mirrors.sohu.com/fedora-epe ...

  6. Ubuntu Java7 SDK环境变量配置(转)

    1.去甲骨文官网下载java7 sdk http://www.oracle.com/technetwork/java/javase/downloads/index.html 这里笔者下载了最新的jav ...

  7. USBDM RS08/HCS08/HCS12/Coldfire V1,2,3,4/DSC/Kinetis Debugger and Programmer -- Software Install

    Installation of USBDM for Windows Under Windows, installation is done with a standard Windows MSI fi ...

  8. securecrt中文乱码以及ubuntu设置locale

    参考文献 http://wiki.ubuntu.org.cn/%E4%BF%AE%E6%94%B9locale http://www.bootf.com/547.html 强烈建议 ubuntu下面不 ...

  9. systemtap 脚本示例

    .[root@localhost ~]# stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}' Pass ...

  10. delphi:临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别

    临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别 TRtlCriticalSection 是一个结构体,在windows单元中定义: 是I ...