/*
* @lc app=leetcode.cn id=67 lang=c
*
* [67] 二进制求和
*
* https://leetcode-cn.com/problems/add-binary/description/
*
* algorithms
* Easy (46.67%)
* Total Accepted: 17.6K
* Total Submissions: 37.8K
* Testcase Example: '"11"\n"1"'
*
* 给定两个二进制字符串,返回他们的和(用二进制表示)。
*
* 输入为非空字符串且只包含数字 1 和 0。
*
* 示例 1:
* 输入: a = "11", b = "1"
* 输出: "100"
*
* 示例 2:
*
* 输入: a = "1010", b = "1011"
* 输出: "10101"
*
*/
#define max(a, b) ((a) > (b) ? (a) : (b))
char* addBinary(char* a, char* b) {
if(a == NULL || *a == NULL)
return b;
if(b == NULL || *b == NULL)
return a;
int flag = ;
int len1 = strlen(a), len2 = strlen(b);
int len = max(len1, len2) + ;
char* result = (char*)malloc(len * sizeof(char));
result[len - ] = '\0';
int i = , j = , index = len - ;
while(len1 || len2 || flag) {
int t = flag;
if(len1)
t += (a[--len1] - '');
if(len2)
t += (b[--len2] - '');
flag = t / ;
result[index--] = '' + t % ;
}
if(index == ) {
char* temp = (char*)malloc((len - ) * sizeof(char));
memcpy(temp, result + , (len - ) * sizeof(char));
free(result);
return temp;
}
return result;
}

这里思路是:

创建一个字符串数组result 其长度等于函数传进的两个数组中长度更长的+2,末位len-1设为\0为字符串终止符。循环从len-2开始,循环的条件是 len1,len2,flag其中有一者不为0即可。

循环内的两个if,很好的规避了补0的问题,因为如果数组位数不足的话,那么肯定那里就是0了,只要判断另一组就行。

t最开始等于0,然后每次都和a,b中当前位置的数进行和运算,当t=0或者1的时候,flag都为0,也就代表没有进位,这时result数组中,就带入t%2的值(要么0,要么1)

在下一次循环中,如果flag=1的话,那么t=flag,也就是附带着上一次循环中进位的1,这样就好理解了。

之后如果首位是0的话,就把result数组加1然后复制给temp数组,返回temp (首位进1)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=67 lang=python3
#
# [67] 二进制求和
#
# https://leetcode-cn.com/problems/add-binary/description/
#
# algorithms
# Easy (46.67%)
# Total Accepted: 17.6K
# Total Submissions: 37.8K
# Testcase Example: '"11"\n"1"'
#
# 给定两个二进制字符串,返回他们的和(用二进制表示)。
#
# 输入为非空字符串且只包含数字 1 和 0。
#
# 示例 1:
#
# 输入: a = "11", b = "1"
# 输出: "100"
#
# 示例 2:
#
# 输入: a = "1010", b = "1011"
# 输出: "10101"
#
#
class Solution:
def addBinary(self, a: str, b: str) -> str:
return bin(int(a,2)+int(b,2))[2:]

做到这心态有些崩溃。。。python一行代码就可以搞定。但是还是要用c的,煅炼算法思维。python确实好用。

这里int(a,2)就是用2进制表示,相加后 用bin函数就可以表示成二进制,但是都是带 0bxxxxxx这样的形式,所以取第二位往后的所有数,才有了[2:]

Leecode刷题之旅-C语言/python-67二进制求和的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. BeginInvoke和EndInvoke方法

    本系列教程主要包括如下内容:1. BeginInvoke和EndInvoke方法 2. Thread类 3. 线程池 4. 线程同步基础 5. 死锁 6. 线程同步的7种方法 7. 如何在线程中访问G ...

  2. Laravel 教程 - 实战 iBrand 开源电商 API 系统

    iBrand 简介 IYOYO 公司于2011年在上海创立.经过8年行业积累,IYOYO 坚信技术驱动商业革新,通过提供产品和服务助力中小企业向智能商业转型升级. 基于社交店商的核心价值,在2016年 ...

  3. Selenium2学习(十八)-- js处理内嵌div滚动条

    前言 前面有篇专门用js解决了浏览器滚动条的问题,生活总是多姿多彩,有的滚动条就在页面上,这时候又得仰仗js大哥来解决啦. 一.内嵌滚动条 1.下面这张图就是内嵌div带有滚动条的样子,记住它的长相. ...

  4. python操作excel (openpyxl)

    最近看到好几次群里有人问xlwt.wlrd的问题,怎么说呢,如果是office2007刚出来,大家用xlsx文件用不习惯,还可以理解,这都10年过去了喂,就算没有进化到office2016,还在用of ...

  5. Infinity 与 NAN

    System.out.println(5.0/0.0+''-"+0.0/0.0); 正确的输出结果是Infinity-NaN 1.为什么不是java.lang.ArithmeticExcep ...

  6. 6 - 常用模块(os,sys,time&datetime,random,json&picle,shelve,hashlib)

    导入模块 想使用 Python 源文件,只需在另一个源文件里执行 import 语句 import module1[, module2[,... moduleN] from语句让你从模块中导入一个指定 ...

  7. scrum 第二次冲刺

    scrum 第二次冲刺 1.本周工作 本周正式开始了开发工作.首先设计了类图,建好了数据库,将整个小组的分工传到了禅道上,我主要负责后台的挂号操作. 本周分工如下: 首先搭建好了ssm框架,其中遇到了 ...

  8. *92. Reverse Linked List II (follow up questions)

    Reverse a linked list from position m to n. Do it in one-pass and in-place Note: 1 ≤ m ≤ n ≤ length ...

  9. 关于c++对文件读写的封装

    namespace { UINT_T GetWriteSizeForNoBuf(UINT_T fsize) { UINT_T write_buf_size = ; == ) { write_buf_s ...

  10. 【[SDOI2016]排列计数】

    一眼题,答案就是\(C_m^m*d_{n-m}\) 就是从\(n\)个中选取\(m\)个在位,剩下的错排,之后就是乘法原理了 但是我发现我的错排公式竟然一直不会推 这个递推式很简单,就是\(d[1]= ...