题目:

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. scala for spark

    写了点scala的内容,方便看spark源码,估计一两天就能看完,内容比较粗浅. 下载地址: http://download.csdn.net/detail/lsshlsw/7983541

  2. 安卓仿QQ红包领取详情界面动画

    为了能清楚的看到这个效果,本人不惜几次花费重金给众群叼发放红包,来查看红包领取详情界面的动画效果,QQ效果如图: 图中我们可以看到,动画处的头像和文字是一起的,即同时并且是整体,注意,是整体进行缩放的 ...

  3. 08 ListView 优化的例子

    package com.fmy.homework; import java.util.List; import com.fmy.homework.httputil.HttpUtil; import c ...

  4. Oracle dblink详解

     database link概述 database link是定义一个数据库到另一个数据库的路径的对象,database link允许你查询远程表及执行远程程序.在任何分布式环境里,databas ...

  5. C语言中extern关键字的使用

    C语言中extern关键字的使用,直接上代码. file1.c文件 #include<stdio.h> extern long power(int); int A = 2; int mai ...

  6. UE4类修饰符

    官方文档链接: https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Reference/index.html ...

  7. WebLogic重启

     1,用户名密码登录. 2,切换到weblogic的bin目录下  cd bea/user_projects/domains/base_domain/bin/ 3,先停止服务器  ./stopWe ...

  8. 一个CSS+jQuery的放大缩小动画效果

    日期: 2013年9月23日 作者:铁锚 // 今天帮朋友写了一些代码,自己觉得写着写着,好几个版本以后,有点满意,于是就贴出来. // 都是定死了的.因为需求就只有4个元素.如果是要用CSS的cla ...

  9. C++ Primer 有感(标准库map类型)

    map是键-值对的集合.map类型通常可以理解为关联数组:可以使用键作为下标获取一个值,正如内置数组一样.而关联的本质在于元素的值于某个特定的键相关联,而并非通过元素在数组中的位置获取. 1.map对 ...

  10. Asp.Net中使用JQueryEasyUI--善良公社项目

    jQuery UI 是以 jQuery 为基础的开源 JavaScript 网页用户界面代码库.包含底层用户交互.动画.特效和可更换主题的可视控件.我们可以直接用它来构建具有很好交互性的web应用程序 ...