LeetCode——Add Binary
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
求数字字符串的二进制和。
同之前的数组代表数字,两个数组相加一样。仅仅只是进位变成了2.可能两个串的长度不一样,故逆转。从左到右加下去。最后再逆转。
public static String addBinary(String a, String b) {
StringBuilder ar = new StringBuilder(a).reverse();
StringBuilder br = new StringBuilder(b).reverse();
StringBuilder result = new StringBuilder();
int len = Math.max(a.length(), b.length());
int carry = 0;//进位
for (int i = 0; i < len; i++) {
int t1 = (i >= a.length() ? 0 : (ar.charAt(i) - '0'));
int t2 = (i >= b.length() ? 0 : (br.charAt(i) - '0'));
int t3 = t1 + t2 + carry;
carry = t3 / 2;
t3 = t3 % 2;
result.append(t3);
}
if (carry != 0)
result.append(carry);
result.reverse();
return result.toString();
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
LeetCode——Add Binary的更多相关文章
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [leetcode]Add Binary @ Python
原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...
- Leetcode Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode Add Binary |My Solution
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- [Leetcode] add binary 二进制加法
Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...
- LeetCode Add Binary 两个二进制数相加
class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
随机推荐
- 初尝Java序列化/反序列化对象
看个类: package com.wjy.bytes; import java.io.Serializable; public class ObjTest implements Serializabl ...
- JAVA card 应用开发(三) 把APPLET(CAP文件)装载到卡片
依据前面两篇博文.我们能够在Eclipse上建立好Applet.而且能够有多个AID.能够选择不同的应用. 那么,以上我们都是基于模拟环境的逻辑,实际上有些函数接口是须要实际的环境.就是说我们须要把A ...
- LAMP配置参考地址
http://www.linuxidc.com/Linux/2014-07/104563.htm
- Java文件压缩分割(待)
http://blog.csdn.net/ycg01/article/details/1366648
- HDU 4643 GSM 暑期多校联合训练第五场 1001
点击打开链接 我就不说官方题解有多坑了 V图那么高端的玩意儿 被精度坑粗翔了 AC前 AC后 简直不敢相信 只能怪自己没注意题目For the distance d1 and d2, if fabs( ...
- 项目之软件project(我专业四年都未曾知道这四个字的含义,几句话便懂了)
潘鹏在CSDN上原创.如其它站点转载请注意排版和写明出处: 软件project的本质 一级标题 控制 质量 二级标题 成本 扩展 高内聚低耦合 效率 控制: 成本:企业要求的是以最快的速度完毕可 ...
- java中线程机制
java中线程机制,一开始我们都用的单线程.现在接触到多线程了. 多线性首先要解决的问题是:创建线程,怎么创建线程的问题: 1.线程的创建: 四种常用的实现方法 1.继承Thread. Thread是 ...
- CentOS 如何使用第三方软件库-EPEL与RPMForge、RPMFusion软件库
在CentOS下运行yum install flash-plugin或yum install mplayer的时候,提示库里没有找到这个软件?为什么会这样?因为CentOS是RHEL编译过来的,去掉了 ...
- ecshop购物流程中去掉email邮箱
首先打开includes\lib_order.php,在第1688行左右找到并删除 !empty($consignee['email']) && 接着打开js\shopping_flo ...
- ListView IllegalStateException
贴出源代码: android.widget.ListView ... if(mItemCount == 0){ resetList(); invokeOnItemScrollListener(); r ...