翻译

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

比如,
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. 数位DP || Gym 101653R Ramp Number

    每一位都大于等于前一位的数叫Ramp Number 给一个数,如果不是Ramp Number输出-1,如果是Ramp Number输出比它小的Ramp Number的个数 只和每一位上的数字有关 #i ...

  2. python interview questions

    referce:python interview questions top 50 refercence:python interview questions top 15 summary Q: wh ...

  3. 收集的WEB前端程序员需要的网站整理

    前端学习资源实在是又多又广,在这样的一个知识的海洋里,我们像一块海绵一样吸收,想要快速提高效率,平时的总结不可缺少,以下总结了一些,排版自我感觉良好,推送出来. 一.插件类网站 jQuery插件库:h ...

  4. 题解 洛谷P3622/BZOJ1151【[APIO2007]动物园】

    这一道题,我也是搞了很久才搞懂的(也就两个多小时). 感谢Rayment大佬的题解! 我们进入正题. 对于一个笼子里的动物,我们可以选择撤走或不撤走,可以用0和1来表示,很容易就想到二进制,想到状压d ...

  5. 树莓派搭建Seafile个人网盘

    步骤一.安装Seafile依赖包 yum install python-setuptools python-ldap python-memcached MySQL-python mariadb mar ...

  6. Django框架基础知识10-内置分页系统

    from django.shortcuts import render, redirect, reversefrom datetime import datetime# Create your vie ...

  7. 数据结构( Pyhon 语言描述 ) — — 第2章:集合概览

    集合类型 定义 个或多个其他对象的对象.集合拥有访问对象.插入对象.删除对象.确定集合大小以及遍历或访问集合的对象的操作 分类 根据组织方式进行 线性集合 线性集合按照位置排列其项,除了第一项,每一项 ...

  8. POJ 3169 Layout(差分约束 线性差分约束)

    题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...

  9. Mybatis 缓存策略

    听极客学院笔记 使用mybatis的缓存需要以下三步 一.在mybatis的config.xml中开启缓存 <settings> <setting name="cacheE ...

  10. json串转成list

    List<BaseStaEmpEntity> baseStaEmpEntities = new ArrayList<>(); //json转List<baseStaEmp ...