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的更多相关文章

  1. LeetCode: Add Binary 解题报告

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

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

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

  3. [leetcode]Add Binary @ Python

    原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...

  4. Leetcode Add Binary

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

  5. LeetCode Add Binary |My Solution

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

  6. [Leetcode] add binary 二进制加法

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

  7. LeetCode Add Binary 两个二进制数相加

    class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...

  8. LeetCode 面试:Add Binary

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

  9. leetcode解题:Add binary问题

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

随机推荐

  1. IIS总提示输入用户名和密码

    解决办法: 第一步,在"运行"中输入"gpedit.msc"调出组策略设置. 找到计算机配置-管理模块-windows 组件-internet控制面板-安全页- ...

  2. HDU3977(斐波那契数列模n的循环节长度)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3977 题意:求斐波那契数列模p的循环节长度,注意p最大是2*10^9,但是它的素因子小于10^6. 分析过 ...

  3. 为什么推荐std::string而不是char*

    例如如下: map<const char*, const char*> map_test; map_test["a"] = "a"; map_tes ...

  4. 求1e11以内的素数

    有两种做法,一种是打表,另一种是直接求. 打表 将1e11每隔len(len=2000w)个数字统计一下该区间内素数的个数,比如cnt[1] 表示[1,len]以内有多少个素数,cnt[2]表示[le ...

  5. ThreadSafeClientConnManager的20个例子

    Java Code Examples for org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager The following are ...

  6. Android横屏竖屏设置

    Android横竖屏设置: 方法一:onCreate()中 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // ...

  7. Hot Days Codeforces Round #132 (Div. 2) D(贪婪)

    Description The official capital and the cultural capital of Berland are connected by a single road ...

  8. Jsoup 抓取和数据页 认识HTTP头

    推荐一本书:黑客攻防技术宝典.Web实战篇  :       顺便留下一个疑问:能否通过jsoup大量并发訪问web或者小型域名server,使其瘫痪?其有用jsoup熟悉的朋友能够用它解析url来干 ...

  9. Codeforces Round#309 C Kyoya and Colored Balls

    给定一个k表示颜色的种类从1到k 然后接下来k行, 每行一个数字, 代表该颜色的球有多少个 这些球都放在一个包中,然后依次拿出.  要求颜色i的最后一个球, 必须要排在颜色i+1的最后一个球前面,   ...

  10. .net Work Flow 4.0

    工作流 依据 WfMC 的定义.工作流(WorkFlow)就是自己主动运作的业务过程部分或总体.表现为參与者对文件.信息或任务依照规程採取行动,并令其在參与者之间传递.官方的总是非常抽象,抽象是为了可 ...