/*
* @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. SQL Server ->> 建立linked server到Azure SQL Server

    EXEC master.dbo.sp_addlinkedserver @server = N'<nick_name_to_use>', @srvproduct=N'', @provider ...

  2. windows网络命令汇总

    分类: 网络技术2011-10-26 09:43 2557人阅读 评论(0) 收藏 举报 windows网络路由器dns服务器internetinterface Ping命令: ping命令通过发送I ...

  3. Vaadin学习笔记——Page、UI和View在用法上的区别

    前言 在Vaadin技术框架中会出现三种不同的类,用于架构Web应用.它们分别是:Page.UI.View.本文将对这三者从使用角度进行比较,试图分析三者的异同.本文完全原创,我可不是在强调版权,我只 ...

  4. Python3条件控制语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. if语句 if 条件: 代码块 elif 条件: 代码块 else: 代码块 python中用elif ...

  5. java 解压缩Zip文件 ziputil

    package com.lanyuan.assembly.util; import java.io.BufferedOutputStream;import java.io.File;import ja ...

  6. Perl实用中文处理步骤(修改版)

    发信人: FenRagwort (泽), 信区: Perl标  题: Perl实用中文处理步骤(修改版)发信站: 水木社区 (Mon Feb 14 12:52:14 2011), 转信 (修改版 感谢 ...

  7. 开始学习git

    今天看着廖雪峰的git使用教程开始学习git.不过没有将项目托管在github上,而是选择托管在了码云上. 看着明白做起来还是出了些问题,不过好在最后都解决了.果然眼高手低要不得. 试着将自己平时学习 ...

  8. 广义mandelbrot集,使用python的matplotlib绘制,支持放大缩小

    迭代公式的指数,使用的1+5j,这是个复数.所以是广义mandelbrot集,大家能够自行改动指数,得到其它图形.各种库安装不全的,自行想办法,能够在这个站点找到差点儿全部的python库 http: ...

  9. 多层感知机MLP的gluon版分类minist

    MLP_Gluon .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bord ...

  10. HDU 3336 KMP

    题意:求每一个前缀,跟前缀相同的每个子串. 此题:网上很多都是假程序,不过也AC了,的确我测试几个案例之后的的确确是存在这个问题. 分析:每一个前缀,可以考虑KMP,f失配指针,如何求得它出现了多少次 ...