【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
【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(二进制加法)】的更多相关文章
- 067 Add Binary 二进制求和
给定两个二进制字符串,返回他们的和(用二进制表示).案例:a = "11"b = "1"返回 "100" .详见:https://leetc ...
- 【LeetCode每天一题】Add Binary(二进制加法)
Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...
- [leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- [Leetcode] add binary 二进制加法
Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
[015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...
- 【LeetCode-面试算法经典-Java实现】【118-Pascal's Triangle(帕斯卡三角形)】
[118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given numRows, generate ...
- 【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】
[056-Merge Intervals(区间合并)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a collection of intervals, ...
随机推荐
- this常用的用法
1.函数作为对象的方法时,this指的是该对象: var obj ={ name:"bob", age:25, getName:function(){ console.log(th ...
- zabbix_sender
转载一个python写的zabbix sender脚本 #!/usr/bin/env python # -*- coding: utf-8 -*- import socket import struc ...
- CREATE CAST - 定义一个用户定义的转换
SYNOPSIS CREATE CAST (sourcetype AS targettype) WITH FUNCTION funcname (argtype) [ AS ASSIGNMENT | A ...
- webgl 的空间变换(下):空间变换
在网上看了很多关于在三维世界中怎么把一个顶点经过一步步变化,最终呈现在我们的屏幕上的. 其实很多博客或者书籍已经讲的很清楚了,那为什么我还要特别再写一次博客来阐述自己观点呢?(这里只针对那些学习web ...
- vue-cli 中使用less
(1)安装Less模块: npm install less (2)安装less和less-loader,命令如下 npm install less less-loader --sava-dev (3) ...
- vuec常用插件
1. 实现下拉刷新和下拉加载效果 iscroll-probe.js 2.手势密码插件 patternLock.js 3.实现复制 clipboard.min.js
- 216种Web安全颜色
216种Web安全颜色 全部 JavaScript HTML5 jQuery CSS EXT Ajax Web综合 界面设计 DWR 锁定老帖子 主题:216种Web安全颜色 精华帖 (0) :: ...
- BZOJ2212【POI2011】ROT:Tree Rotation 线段树合并
题意: 给一棵n(1≤n≤200000个叶子的二叉树,可以交换每个点的左右子树,要求叶子遍历序的逆序对最少. 分析: 求逆序对我们可以想到权值线段树,所以我们对每个点建一颗线段树(为了避免空间爆炸,采 ...
- spring Boot 不认Mapper.xml
很久以前的笔记了,大约就是用Generatro工具自动生成代码的时候,springboot找不到mapper.xml 之前,由于用mybatis-generator自动生成了entity,dao,ma ...
- python爬虫(三)
Requests模块 这个库的标准文档有个极其幽默的地方就是它的中文翻译,我就截取个开头部分,如下图: 是不是很搞笑,在正文中还有许多,管中窥豹,可见一斑.通过我的使用,感觉Requests库的确是给 ...