给定两个二进制字符串,返回他们的和(用二进制表示)。
案例:
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. 一些rtsp实现的开源代码

    * live.com   C/S   C++   http://www.live555.com     * darwin     S     C++   http://www.opensource.a ...

  2. 常见排序算法-php

    1.归并排序 $a = [1, 4, 6, 8, 10, 14, 16]; $b = [2, 3, 5, 8, 9, 11]; function merge_sort($a, $b) { $a_i = ...

  3. Linux网络编程socket错误分析

    socket错误码: EINTR: 阻塞的操作被取消阻塞的调用打断.如设置了发送接收超时,就会遇到这种错误. 只能针对阻塞模式的socket.读,写阻塞的socket时,-1返回,错误号为INTR.另 ...

  4. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  5. ACM学习历程——hihoCoder挑战赛10A 01串(策略)

    时间限制:7000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个整数n和m,求是否存在恰好包含n个0和m个1的01串S,使得S中不存在子串"001"和" ...

  6. AIM Tech Round 4 (Div. 2)

    A题 分析:暴力 #include "iostream" #include "cstdio" #include "cstring" #inc ...

  7. Words Gems

    所有的东西都来自抄袭.来自学习.不同的是用新的方法做其他公司做过的事情.很多公司做同样的事情,但只有一家公司最成功.你要发现一个有需求的服务,并做得比别人更好,而不是比别人更早.

  8. Elasticsearch官方安装

    Installationedit Elasticsearch requires at least Java 8. Specifically as of this writing, it is reco ...

  9. 使用二次封装的openStack发行版本网卡至少有2个

  10. PHP判断用户是手机端?还是浏览器端访问?

    function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ' ...