题目:

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

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

思路:

  • 题意:对字符串的二进制数字,计算
  • 把二进制转化为整数,设置变量carry位进位,sum%2,是相应的数字,carry = sum/2

    -

代码:

public class Solution {
    public String addBinary(String a, String b) {
        if(a.length() < b.length()){
            String tmp = a;
            a = b;
            b = tmp;
        }
        int lengthA = a.length()-1;
        int lengthB = b.length()-1;
        int carry = 0;
        String all = "";
        while(lengthB >= 0){
            int num = (int)(a.charAt(lengthA)-'0')+(int)(b.charAt(lengthB)-'0')+carry;
            int k = num%2;
            carry = num/2;
            all = String.valueOf(k)+all;
            lengthA--;
            lengthB--;
        }
         while(lengthA >= 0){
            int num = (int)(a.charAt(lengthA)-'0')+carry;
            int k = num%2;
            carry = num/2;
            all = String.valueOf(k)+all;
            lengthA--;
        }
        if(carry == 1){
            all = "1"+all;
        }
        return all;
    }
}

LeetCode(56)-Add Binary的更多相关文章

  1. LeetCode 面试:Add Binary

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

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

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

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

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

  4. 【leetcode】Add Binary

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

  5. Java for LeetCode 067 Add Binary

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

  6. LeetCode 67. Add Binary

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

  7. Java [Leetcode 67]Add Binary

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

  8. (String) leetcode 67. Add Binary

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

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

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

随机推荐

  1. iOS中使用iCloud一些需要注意的地方(Xcode7.2)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在自己的App中如何使用iCloud有很多文章可以查阅,这里把 ...

  2. FFmpeg源代码简单分析:avformat_open_input()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  3. Android开发学习之路--React-Native之初体验

      近段时间业余在学node.js,租了个阿里云准备搭建后端,想用node.js,偶尔得知react-native可以在不同平台跑,js在iOS和android上都可以运行ok,今天就简单学习下rea ...

  4. Linux内核分配内存的方式

    page = alloc_pages(GFP_KERNEL, get_order(1234));分配失败返回NULLGFP_KERNEL  ---> 分配标志,当没有足够内存分配时,睡眠阻塞,直 ...

  5. Spark 1.0 开发环境构建:maven/sbt/idea

    因为我原来对maven和sbt都不熟悉,因此使用两种方法都编译了一下.下面记录一下编译时候遇到的问题.然后介绍一下如果使用IntelliJ IDEA 13.1构建开发环境. 首先准备java环境和sc ...

  6. JUI/DWZ介绍、简单使用

    简介 由于开发的项目使用JUI,所以学习了. DWZ富客户端框架(jQuery RIAframework), 是中国人自己开发的基于jQuery实现的Ajax RIA开源框架.(现在更名为JUI) 可 ...

  7. PHP(PHP-FPM)手动编译安装

    1安装PHP 1.1下载解压 wget http://museum.php.net/php5/php-5.3.5.tar.gz tarxzvf php-5.3.5.tar.gz cdphp-5.3.5 ...

  8. 【Unity技巧】调整画质(贴图)质量

    写在前面 当我们在Unity中,使用图片进行2D显示时,会发现显示出来的画面有明显的模糊或者锯齿,但是美术给的原图却十分清晰. 要改善这一状况实际上很简单. 造成这样的原因,是Unity在导入图片(或 ...

  9. 【一天一道LeetCode】#130. Surrounded Regions

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  10. Oracle PL/SQL Articles

    我是搬运工....http://www.oracle-base.com/articles/plsql/articles-plsql.php Oracle 8i Oracle 9i Oracle 10g ...