Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

简单的二进制相加而已,只不过传入的参数是字符串而已。为了方便,先将string  reverse了一下,代码如下:

 class Solution {
public:
string addBinary(string a, string b) {
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int len1 = a.length();
int len2 = b.length();
string result;
int flag ,val;
flag = val = ;
int i;
for(i = ; i < len1 && i < len2; ++i){
val = (a[i] - '') + (b[i] - '') + flag;
result.append(, val % + '');
flag = val/;
}
while (i < len1) {
val = a[i] - ''+ flag;
result.append(, val % + '');
flag = val/;
++i;
}
while (i < len2 ){
val = b[i] - ''+ flag;
result.append(, val % + '');
flag = val/;
++i;
}
cout << "flag " << flag << endl;
if(flag != )
result.append(, '');
reverse(result.begin(), result.end());
return result;
}
};

java:以前的循环用的好蠢啊,居然用了三次循环,下面的java稍微有些改进:

 public class Solution {
public String addBinary(String a, String b) {
int len1 = a.length();
int len2 = b.length();
String ret = new String();
int i = 0;
int carry = 0;
a = ReverseStr(a);
b = ReverseStr(b);
while(i < len1 || i < len2 || carry != 0){
int val = ((i<len1)?(a.charAt(i)-'0'):0) + ((i<len2)?(b.charAt(i)-'0'):0) + carry;
carry = 0;
if(val > 1){
carry = val/2;
ret += (char)((val%2) + '0');
}else{
ret += (char)(val + '0');
}
i++;
}
return ReverseStr(ret);
} public String ReverseStr(String str){
return new StringBuffer(str).reverse().toString();
}
}

LeetCode OJ:Add Binary(二进制相加)的更多相关文章

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

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

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

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

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

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

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

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

  5. LeetCode 67. Add Binary (二进制相加)

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

  6. LeetCode 面试:Add Binary

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

  7. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  8. [LintCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

  9. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  10. LeetCode 67 Add Binary(二进制相加)(*)

    翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...

随机推荐

  1. HTML5之WEB Storage

    什么是HTML5 web storage? 使用HTML5,web页面能够使用用户的浏览器本地保存数据. 在曾经,通常我们使用cookie来保存用户数据.然而使用web存储更加安全和高速.数据不再包括 ...

  2. 最全的sublime插件整理

    Package Control 插件管理器 1)在Sublime中打开View –> Show Console,将以下代码复制到输入框中后按回车键 import urllib.request,o ...

  3. BLOG总结

    1.登录:http://www.cnblogs.com/shaojiafeng/p/7868195.html 2.注册 - urls -前端页面中写 username ,password,passwo ...

  4. go——切片(二)

    切片是一种数据结构,这种数据结构便于使用和管理数据集合. 切片是围绕动态数组的概念构建的,可以按需自动增长和缩小. 切片的动态增长是通过内置函数append来实现的.这个函数可以快速且高效地增长切片. ...

  5. yum安装mysql5.6

    1.检查系统是否安装其他版本的MYSQL数据 yum list installed | grep mysql yum -y remove mysql-libs.x86_64 2.安装及配置 wget ...

  6. Django:学习笔记(3)——REST实现

    Django:学习笔记(3)——REST实现 了解REST风格 按照传统的开发方式,我们在实现CURD操作时,会写多个映射路径,比如对一本书的操作,我们会写多个URL,可能如下 web/deleteB ...

  7. cdojQ - 昊昊爱运动 II

    地址:http://acm.uestc.edu.cn/#/contest/show/95 题目: Q - 昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others) ...

  8. Jconsle

    1. jconsole 远程连接: JConsole很好用,可以解决很多疑难杂症.但远程连接需要设置一下Java opt才可以使用.以下是步骤: 1). 在java opt下添加如下内容: 如果是无须 ...

  9. What is CRC and how does it works?

    What is CRC and how does it works? CRC errors refer to Layer 1 or 2 issues. Two things you should ch ...

  10. jQuery全屏图片焦点图

    在线演示 本地下载