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

Java实现:

class Solution {
public String addBinary(String a, String b) {
String s="";
int c=0;
int i=a.length()-1;
int j=b.length()-1;
while(i>=0||j>=0||c==1){
c+=i>=0?a.charAt(i)-'0':0;
c+=j>=0?b.charAt(j)-'0':0;
s=(char)(c%2+'0')+s;
c/=2;
--i;
--j;
}
return s;
}
}

067 Add Binary 二进制求和的更多相关文章

  1. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

  2. lintcode:Add Binary 二进制求和

    题目: 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 解题: 和求两个链表的和很类似 考虑进位,考虑最后一项的进位 0+0 = 0 不 ...

  3. Add Strings大整数加法十进制求和 & Add Binary二进制求和

    [抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...

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

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

  5. Java for LeetCode 067 Add Binary

    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).The input strings are both non-emp ...

  7. [leetcode]67. Add Binary 二进制加法

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

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

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

  9. LeetCode 面试:Add Binary

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

随机推荐

  1. Mybatis-Generator_学习_01_mybatis-generator笔记

    一.generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  2. HDU5692 Snacks

    HDU5692 Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的 ...

  3. bzoj3629

    dfs 跟上道题很像有木有 同样地,我们暴力枚举约数 根据约数和公式,得出$S=\prod_{i=1}^{n}{(1+p+p^{2}+...+p^{a_{i}})}$ 所以每次我们暴力枚举是哪个约数, ...

  4. VS2005打开VS2008项目的2种方法

    vs2008支持.net3.5,而vs2005支持.net2.0,所以使用vs2005打开vs2008的项目,要确定你的项目是.net2.0的. 下面介绍2种方法: 方法1:用记事本打开.sln文件, ...

  5. 《精通Spring4.X企业应用开发实战》读后感第二章

  6. qpython 读入数据问题: EOF error with input / raw_input

    直接使用input会报错 EOF error with input / raw_input 原因是在qpy里console mode 命令行模式不是完全和pc上的命令行一致,所以input和raw_i ...

  7. 项目debug1

    QuestionController代码如下: @RequestMapping(value = "/question/{qid}", method = {RequestMethod ...

  8. 星级评分--封装成jquery插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. http verbs--Method Definitions

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html part of Hypertext Transfer Protocol -- HTTP/1. ...

  10. POJ1251 Jungle Roads Kruskal+scanf输入小技巧

    Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...