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,布布扣,bubuko.com

时间: 2024-10-13 16:05:32

LeetCode——Add Binary的相关文章

[leetcode]Add Binary @ Python

原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 解题思路:提供两种实现方式吧. 代码一: class Solution: # @param a, a string # @pa

[LeetCode] [Add Binary 2012-04-02 ]

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". string 的操作,短string补位.两个"0"会输出一个"00",要特殊处理,plus如果最后为"1",要补上. ? 1 2 3 4 5 6 7 8 9 10 1

LeetCode: Add Binary 解题报告

Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". SOLUTION: 指针指到两个字符串的末尾,不断往前推进,用carry表示进位.用stringbuilder来记录结果. 使用insert(0, c)函数将加出的结果不断插入到STRINGBUILDER. 1 p

LeetCode Add Binary 结题报告

https://oj.leetcode.com/problems/add-binary/ Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 解题报告:二进制加法,分类为:Easy.没有任何算法,就是按照普通的方法计算即可. 一开始写了一个高低位reverse的,然后再从0到高位

LeetCode Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题意:二进制的加法. 思路:跟大数加法差点儿相同. class Solution { public: string addBinary(string a, string b) { stack<int> s; int

LeetCode Add Binary |My Solution

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". Show Tags Have you been asked this question in an interview? public class Solution { public String addBinary(Strin

[Leetcode] add binary 二进制加法

Given two binary strings, return their sum (also a binary string). For example,a ="11"b ="1"Return"100". 题意:将两个以字符串形式保存的二进制数进行相加. 思路:其实不管是以数组.字符串形式,加或者乘(multiply strings),一般的思路都是从后往前计算,用一个中间变量保存相加或者相乘的结果(加法是一个变量int 或string就行,

leetcode Add Binary (easy) /java

二进制的加法 经过测试之后,发现没有考虑整型数据溢出. leetcode经常会有一些意想不到的例外.我以为是一道解法很丰富的题,选择了:把string转为int,用int的加法,再转为string返回.因为我讨厌用字符串来进位.但是溢出了.可以改进一下,用BigInteger这个类. 先贴上我的错误代码. import java.io.*; import java.util.*; import java.lang.*; public class Solution { public static

Leetcode:Add Binary 二进制相加

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路同十进制的大数相加.代码如下: class Solution { public: string addBinary(string a, string b) { size_t len_a = a.length(); size_