【067-Add Binary(二进制加法)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

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

  For example,

  a = "11"

  b = "1"

  Return "100"

题目大意

  给定两个二进制的字符串,返回它们的和,也是二进行制字符串。

解题思路

  先将相应的两个二进制字符串转换成相应的整数数组,从低位到高位进行相加,同一时候要考虑到最后相加还要扩展一位的情况。

详情请见代码实现。

代码实现

算法实现类

public class Solution {
public String addBinary(String a, String b) { int[] ca = new int[a.length()];
int[] cb = new int[b.length()]; // 将字符数组中的值转换了数值的0或者1
for (int i = 0; i < a.length(); i++) {
ca[i] = a.charAt(i) - '0';
} // 将字符数组中的值转换了数值的0或者1
for (int i = 0; i < b.length(); i++) {
cb[i] = b.charAt(i) - '0';
} // 使用ca保存的长度长
if (ca.length < cb.length) {
int[] tmp = ca;
ca = cb;
cb = tmp;
} int ai = ca.length - 1; // 字符数组ca最后一个索引下标
int bi = cb.length - 1; // 字符数组cb最后一个索引下标
int carry = 0; // 下位的进位标识
int result; // 载入的结果 // 计算比方:1010101101 + 10100
while (ai >= 0 && bi >= 0) {
result = ca[ai] + cb[bi] + carry;
ca[ai] = result % 2;
carry = result / 2; ai--;
bi--;
} // 处理余下的数字
while (ai >= 0) {
result = ca[ai] + carry;
ca[ai] = result % 2;
carry = result / 2; if (carry == 0) {
break;
} ai--;
} // 将字符数组中的值转换了字符的0或者1
for (int i = 0; i < ca.length; i++) {
ca[i] += '0';
} // 不须要扩展一位
if (carry == 0) { char[] ch = new char[ca.length];
for (int i = 0; i < ca.length; i++) {
ch[i] = (char) (ca[i]);
} return new String(ch);
}
// 须要扩展一位
else {
char[] ch = new char[ca.length + 1];
ch[0] = '1';
for (int i = 0; i < ca.length; i++) {
ch[i + 1] = (char) (ca[i]);
}
return new String(ch);
}
}
}

评測结果

  点击图片,鼠标不释放,拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47203323

【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】的更多相关文章

  1. 067 Add Binary 二进制求和

    给定两个二进制字符串,返回他们的和(用二进制表示).案例:a = "11"b = "1"返回 "100" .详见:https://leetc ...

  2. 【LeetCode每天一题】Add Binary(二进制加法)

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

  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"b =& ...

  5. leetCode 67.Add Binary (二进制加法) 解题思路和方法

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

  6. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  7. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

  8. 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】

    [030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...

  9. 【LeetCode-面试算法经典-Java实现】【118-Pascal&#39;s Triangle(帕斯卡三角形)】

    [118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given numRows, generate ...

  10. 【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】

    [056-Merge Intervals(区间合并)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a collection of intervals, ...

随机推荐

  1. iOS重签

    由于渠道推广需要,可能需要多个包做备份推广,区别是icon.游戏名称.登录logo.bundleid.签名证书.支付Consumables不同,其他游戏包体完全相同. 反复修改多次文件提交Jenkin ...

  2. Regular Expression Flavors

    Perl https://perldoc.perl.org/perlre.html PCRE http://www.pcre.org/current/doc/html/pcre2syntax.html ...

  3. formSelects-v4.js 基于Layui的多选解决方案

    https://hnzzmsf.github.io/example/example_v4.html

  4. CAD绘制固定圆形标注(网页版)

    js中实现代码说明: function DoFixCircleComment() { var ent = mxOcx.DrawCustomEntity("TestMxCustomEntity ...

  5. CAD插入背景图片(网页版)

    把图片作为背景图片可见但是不能编辑操作. 主要用到函数说明: _DMxDrawX::DrawImageToBackground 绘光栅图到背景.详细说明如下: 参数 说明 BSTR sFileName ...

  6. Linux C动态链接库实现一个插件例子

    实现一个简单的计算动态链接库:升级动态链接库后,在不重新编译主程序的情况下,直接生效. lib库: #cat math.c #include <stdio.h> int add(int x ...

  7. 计算机中的CPU

    今天写一下计算机中最核心的一部分,就是计算机的大脑---CPU.CPU也就是中央处理器(Central Processing Unit).中央处理器是一块超大规模的集成电路,是一台计算机的运算核心(C ...

  8. element--ui使用tab切换时如何获取当前对象的id或者其他属性

    1. 问题 当使用tab切换时,部分特殊场景需要获取当前元素的类名或者id. 2.解决思路,tab切换是绑定函数,函数会传递过去当前对象,通过当前对象获取对象属性 vue部分代码:本项目是在vue-c ...

  9. CSU 2018年12月月赛 D 2216 : Words Transformation

    Description There are n words, you have to turn them into plural form. If a singular noun ends with ...

  10. 零基础入门学习Python(3)--小插曲之变量和字符串

    前言 小甲鱼说,在对前边的小游戏改善前,先了解下,Python中的变量与字符串. 主要内容 变量 变量名就像我们现实社会的名字,把一个值赋值给一个名字时,Ta会存储在内存中,称之为变量(variabl ...