67. Add Binary

Easy

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

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"
package leetcode.easy;

public class AddBinary {
@org.junit.Test
public void test() {
String a1 = "11";
String b1 = "1";
String a2 = "1010";
String b2 = "1011";
System.out.println(addBinary(a1, b1));
System.out.println(addBinary(a2, b2));
} public String addBinary(String a, String b) {
StringBuffer buffer = new StringBuffer();
int sum = 0;
int carry = 0;
for (int i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0; i--, j--) {
sum = carry;
if (i >= 0) {
sum += a.charAt(i) - '0';
}
if (j >= 0) {
sum += b.charAt(j) - '0';
}
buffer.append(sum % 2);
carry = sum / 2;
}
if (carry != 0) {
buffer.append(carry);
}
return buffer.reverse().toString();
}
}

LeetCode_67. Add Binary的更多相关文章

  1. leetcode解题:Add binary问题

    顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...

  2. [LintCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

  3. Add Binary

    Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...

  4. LeetCode 面试:Add Binary

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

  5. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. 2016.6.21——Add Binary

    Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...

  8. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  9. leetcode笔记:Add Binary

    一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...

随机推荐

  1. [原创]extjs htmleditor增加截图快速粘贴功能 插件

    因客户需求,需要QQ截图后,能直接粘贴到htmleditor编辑器中,不要保存本地文件再上传,再插入到编辑器,太过麻烦. 常规做法:QQ截图-->选择保存路径-->确定保存文件--> ...

  2. ElementUI2.0组件库el-table表格组件如何自定义表头?

    效果图: npm run dev 编译项目之后,报错,要使用jsx语法需要先安装编译插件 1.安装下列安装包 npm install babel-plugin-syntax-jsx --save-de ...

  3. CSS float详解

    前言:在我们写CSS样式的时候,float,position,display,overflow这几个关键字用得比较多. 弄清楚他们之间的原理,我们可以更高效的写出我们想要的布局. 作者:Ry-yuan ...

  4. Centos7 安装相关软件

    1.安装 wget : yum -y install wget

  5. PostgreSQL 进程结构

    本文主要讲述了PG的几个主要进程,以及PG的核心架构.进程和体系结构详见下图: 从上面的体系结构图可以看出来,PG使用经典的C/S架构,进程架构.在服务器端有主进程.服务进程.子进程.共享内存以及文件 ...

  6. Linux core dump 诊断进程奔溃退出

           最近项目中出现了一个问题,服务器端程序会突然崩溃退出,我们采取了coredump技术以找到崩溃原因,即确定进程退出时正在执行的函数是哪个,其状态如何.       如果系统开启了core ...

  7. LibreOJ #116. 有源汇有上下界最大流

    二次联通门 : LibreOJ #116. 有源汇有上下界最大流 /* LibreOJ #116. 有源汇有上下界最大流 板子题 我也就会写写板子题了.. 写个板子第一个点还死活过不去... 只能打个 ...

  8. Django系列(三):单表操作

    1.ORM简介 MTV或者MTV框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...

  9. bootstrap中tab切换的使用

    文档地址:https://v3.bootcss.com/javascript/#tabs 简单实例: <!DOCTYPE html> <html lang="en" ...

  10. 常用的os库笔记

    1.创建文件 import os os.mkdir('d:/log') 2.重命名文件 import os os.rename('d:/log','d:/newlog') 3.删除文件 import ...