翻译

给定两个二进制字符串,返回它们的和(也是二进制字符串)。

比如,
a = "11"
b = "1"
返回 "100".

原文

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

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

分析

我一開始写了这个算法,尽管实现了功能,只是不符合题目的用意。

int ctoi(char c) {
return (int)c - 48;
} int pow2(int n) {
int sum = 1;
while ((n--) > 0)
sum *= 2;
return sum;
} int btoi(string s) {
int sum = 0, len = s.size();
for (int i = 0; i < len; ++i) {
sum += ctoi(s[i]) * pow2(len - i - 1);
}
return sum;
} string itob(int n) {
if (n == 0) return "0";
string s = "";
while (n >= 1) {
if (n % 2 == 1)
s += "1";
else s += "0";
n /= 2;
}
string newStr = "";
for (int i = s.size() - 1; i >= 0; i--)
newStr += s[i];
return newStr;
} string addBinary(string a, string b) {
return itob(btoi(a) + btoi(b));
}

然后改了改,写出这么脑残的代码我自己都醉了……

string addBinary(string a, string b) {
if (a.size() < b.size()) swap(a, b);
int len1 = a.size(), len2 = b.size();
int comLen = len1 < len2 ? len1 : len2;
int pos = 0;
string str = "";
for (int index1 = len1 - 1, index2 = len2 - 1; index1 > len1 - comLen, index2 >= 0; index1--, index2--) {
if (pos == 0) {
if (a[index1] == '1' && b[index2] == '1') {
str += "0";
pos = 1;
}
else if (a[index1] == '0' && b[index2] == '0') {
str += "0";
}
else {
str += "1";
}
}
else if (pos == 1) {
if (a[index1] == '1' &&b[index2] == '1') {
str += "1";
pos = 1;
}
else if (a[index1] == '0' && b[index2] == '0') {
str += "1";
pos = 0;
}
else {
str += "0";
pos = 1;
}
}
} for (int index = len1 - comLen-1; index >= 0; index--) {
if (pos == 0) {
if (a[index] == '1') {
str += "1";
}
else {
str += "0";
}
}
else if (pos == 1) {
if (a[index] == '1') {
str += "0";
pos = 1;
}
else {
str += "1";
pos = 0;
}
}
}
if (pos == 1) str += "1";
string newStr = "";
for (int i = str.size() - 1; i >= 0; i--)
newStr += str[i];
return newStr;
}

转了一圈也没发现很简洁的代码,那就先这样了……

LeetCode 67 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 (二进制相加)

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

  5. (String) leetcode 67. Add Binary

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

  6. leetcode 67. Add Binary (高精度加法)

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

  7. Leetcode 67 Add Binary 大数加法+字符串处理

    题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...

  8. LeetCode 67. Add Binary

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

  9. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

随机推荐

  1. Window服务程序(windows service application)如何调试

    服务程序不能通过常规的按F5或F11的方式来进行调试和运行,也无法立即运行一个服务或逐步调试它的代码. 因此,你必须安装并启动你的服务,然后附属(attach)一个Debugger到这个服务的进程上.

  2. SQL Server数据库的除法默认向下取整,要返回小数的解决方法

    num1; / 1000.0 num2; * 1.0 num3; num4; 结果:

  3. HTML 5 <a> 标签

    href 属性 定义和用法 href 属性规定链接的目标地址. 如果未使用 href 属性,则 <a> 标签不是链接,而是链接的占位符. HTML 4.01 与 HTML 5 之间的差异 ...

  4. 【C语言项目】贪吃蛇游戏(上)

    目录 00. 目录 01. 开发背景 02. 功能介绍 03. 欢迎界面设计 3.1 常用终端控制函数 3.2 设置文本颜色函数 3.3 设置光标位置函数 3.4 绘制字符画(蛇) 3.5 欢迎界面函 ...

  5. Spoj8093 Sevenk Love Oimaster

    题目描述 题解: 对于所有n串建广义后缀自动机. (广义后缀自动机唯一区别就是每次将las附成1,并不需要在插入时特判) 建完后再建出parent树,然后用dfs序+树状数组搞区间不同种类. 其实就是 ...

  6. C#对象初始化的探讨

    最近在弄MQ的性能监测数据埋点,无疑中用到一个Nstatsd的客户端,看到里面写过里面一种嵌套类的写法.代码如下: 客户端Client是一个密封的类,并且构造函数私有访问.然后又用一个嵌套类Curre ...

  7. 关于linux安装kettle的总结

    一.部署准备 1.1 JDK安装配置 命令行键入“cd /etc”进入etc目录 命令行键入“vi profile”打开profile文件 敲击键盘ctrl+F到文件末尾 在末尾处,即第一个~的地方, ...

  8. Mysql索引研究总结

    闲来无事,研究了一下mysql索引,场景如下: 有一张MyISAM 类型的zt_action表,数据大约230W行,建两个索引,CREATE INDEX `read` ON zt_action(`re ...

  9. js中的constructor 和prototype

    参考 http://www.cnblogs.com/yupeng/archive/2012/04/06/2435386.html function a(c){ this.b = c; this.d = ...

  10. LR百分比模式

    1  场景模式切换 Vuser Group Mode转换为Percentage Mode:如下 Scenario->Convert Scenairio to the VuserGroup Mod ...