翻译

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

比如,
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. shell $() vs ${}

    reference $( )与` `(反引号)都是用来做命令替换(command substitution)用的 run command 3, 2, 1 command1 $(command2 $(c ...

  2. upupoo(网页壁纸)自主修改一:农历

    最近在使用一款upupoo的壁纸软件,感觉还可以,主要是对其中html可设置为壁纸方面情有独钟 前几天在它的网页区发现了一个壁纸,感觉挺好: 感觉内容有点少,今天在工作空余时间就在其中加上了农历,同时 ...

  3. Swift详解之NSPredicate

    言:谓词在集合过滤以及CoreData中有着广泛的应用.本文以Playground上的Swift代码为例,讲解如何使用NSPredicate. 准备工作 先在Playground上建立一个数组,为后文 ...

  4. 深搜DFS

    POJ-1321 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有 ...

  5. [LOJ] 分块九题 4

    https://loj.ac/problem/6280 区间修改,区间求和. 本来线段树的活. //Stay foolish,stay hungry,stay young,stay simple #i ...

  6. 微信小程序wx.request请求服务器json数据并渲染到页面

    [原文出自]: https://blog.csdn.net/weixin_39927850/article/details/79766259 微信小程序的数据总不能写死吧,肯定是要结合数据库来做数据更 ...

  7. 4 SQL 数据更新

    4 数据更新 4-1 数据的插入(INSERT语句的使用方法) 通过create table语句创建出来的表,可以将其比作一个空空如也的箱子.只有把数据装入到这个箱子后,它才能称为数据库.用来装入数据 ...

  8. Centos 7 编译php 7.2.10

    步骤一:安装依赖 yum install -y wget gcc gcc-c++ gd-devel zlib-devel libjpeg-devel libpng-devel libiconv-dev ...

  9. scp免密码拷贝和ssh免密码登录

    版权声明:本文为博主原创文章,未经允许不得转载. 在平常的工作中经常在两个服务器之间传输文件,而且经常从本地远程登录服务器,每次都要输入密码显然很没效率,这时候该怎么办呢? 首先假设服务器A和B,要想 ...

  10. 如何禁用python警告

    有-W选项. python -W ignore foo.py 所属网站分类: python基础 > 综合&其它 作者:jiem 链接:http://www.pythonheidong.c ...