Leecode刷题之旅-C语言/python-67二进制求和
/*
* @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二进制求和的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
随机推荐
- input file 类型为excel表格
以下为react写法,可自行改为html的 <div className="flag-tip"> 请上传excel表格, 后缀名为.csv, .xls, .xlsx的都 ...
- php 获取毫秒时间戳
function getMsec(){//返回毫秒时间戳 $arr = explode(' ',microtime()); $hm = 0; foreach($arr as $v){ $hm += f ...
- March 7 2017 Week 10 Tuesday
Age is a very high price to pay for maturity. 年纪是成熟的代价. A high price, indeed a high price. It is bes ...
- 了解git /github
一 GIT是什么? Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.Git ...
- (第三场) A PACM Team 【dp,五维背包】
链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- XCode项目配置可访问 非 https 接口的方法
打开项目的info.plist文件,右键- open as sourceCode .在代码中添加: <key>NSAppTransportSecurity</key> < ...
- Oracle连接问题
ORA-01034: ORACLE not availableORA-27101: shared memory realm does not exist sqlplus /nolog conn /as ...
- Hinge Loss
http://blog.csdn.net/luo123n/article/details/48878759 https://en.wikipedia.org/wiki/Hinge_loss ...
- 使用canvas上传图片+上传进度
实现效果: 速度过快,调式浏览器方式:F12 后台java代码 public String imageshangchuan(@RequestPart("xxx") Multipar ...
- asp.net 过滤器
asp.net 制作过滤器原理:重写ASP.net管道事件 1.通过HttpApplicationFactory创建一个HttpApplication对象,负责处理整个请求. 2.调用ProcessR ...