当前位置:网站首页>【C语言刷LeetCode】67. 二进制求和(E)
【C语言刷LeetCode】67. 二进制求和(E)
2022-07-29 05:53:00 【kinbo88】
【
给你两个二进制字符串,返回它们的和(用二进制表示)。
输入为 非空 字符串且只包含数字 1 和 0。
示例 1:
输入: a = "11", b = "1"
输出: "100"
示例 2:
输入: a = "1010", b = "1011"
输出: "10101"
提示:
每个字符串仅由字符 '0' 或 '1' 组成。
1 <= a.length, b.length <= 10^4
字符串如果不是 "0" ,就都不含前导零。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/add-binary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
】
一道简单题,居然代码写了有一会,太菜了
1. 申请字符数组而不是int数组
2. 运算前需要-'0',运算后需要+'0'
3. 当a[i]+b[i]这种需要减两次'0'
4. 申请的长度 len + 2,记得初始化
5. 逆转操作需要熟记,而不是还要思考一会才能写
char * addBinary(char * a, char * b){
int lena = strlen(a);
int lenb = strlen(b);
int i, j;
int idx = 0;
int len = fmax(lena, lenb);
char *retarr = malloc(sizeof(char) *(len + 2));
int idxtmp = 0;
int flag = 0;
memset(retarr, 0, sizeof(char) *(len + 2));
i = lena - 1;
j = lenb - 1;
while (i >= 0 || j >= 0) {
if (i >= 0 && j < 0) {
idxtmp = a[i] - '0';
i--;
} else if (i < 0 && j >= 0) {
idxtmp = b[j] - '0';
j--;
} else if (i >= 0 && j >= 0){
idxtmp = a[i] + b[j] - '0' -'0';
i--;
j--;
}
idxtmp = idxtmp + flag;
flag = idxtmp / 2;
retarr[idx++] = idxtmp % 2 + '0';
}
if (flag != 0) {
retarr[idx++] = flag + '0';
}
i = 0; // 逆序
j = idx - 1;
while (i < j) {
char tmp = retarr[i];
retarr[i] = retarr[j];
retarr[j] = tmp;
i++;
j--;
}
return retarr;
}
/*
一道简单题,居然代码写了有一会,菜
1. 申请字符数组而不是int数组
2. 运算前需要-'0',运算后需要+'0'
3. 当a[i]+b[i]这种需要减两次'0'
4. 申请的长度 len + 2,记得初始化
5. 逆转操作需要熟记,而不是还要思考一会才能写
*/
边栏推荐
- Use of callable
- 【冷冻电镜】Relion4.0——subtomogram教程
- 吴恩达老师机器学习课程笔记 04 多元线性回归
- Unity exploration plot access design analysis & process + code specific implementation
- Overview of database system
- 模拟卷Leetcode【普通】061. 旋转链表
- Ping principle
- DBAsql面试题
- Teacher Wu Enda's machine learning course notes 03 review of linear algebra
- Relative date used by filter in salesforce
猜你喜欢
【讲座笔记】如何在稀烂的数据中做深度学习?
C language memory stack and heap usage
Unity探索地块通路设计分析 & 流程+代码具体实现
剑指 Offer II 115:重建序列
网上传说软件测试培训真的那么黑心吗?都是骗局?
【flask入门系列】Flask-SQLAlchemy的安装与配置
Sword finger offer II 115: reconstruction sequence
vim文本编辑器的一些使用小技巧
Why does 5g N2 interface control plane use SCTP protocol?
Teacher Wu Enda's machine learning course notes 02 univariate linear regression
随机推荐
Instruction rearrangement under multithreading concurrency
【讲座笔记】如何在稀烂的数据中做深度学习?
Cvpr2022oral special series (I): low light enhancement
mysql查询区分大小写
The difference between pairs and ipairs
吴恩达老师机器学习课程笔记 04 多元线性回归
线程 - 线程安全 - 线程优化
vscode通过remotessh结合xdebug远程调试php解决方案
【解决方案】ERROR: lib/bridge_generated.dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
Talk about tcp/ip protocol? And the role of each layer?
王树尧老师运筹学课程笔记 00 写在前面
Teacher wangshuyao's notes on operations research 03 KKT theorem
分享一些你代码更好的小建议,流畅编码提搞效率
Shallow reading of reentrantlock source code of abstractqueuedsynchronizer (AQS)
量子机器学习中的安全性问题
崔雪婷老师最优化理论与方法课程笔记 00 写在前面
Apisik health check test
NLP word segmentation
Thread synchronization - producers and consumers, tortoise and rabbit race, dual thread printing
Implementation of DDP cluster distributed training under pytoch multi GPU conditions (brief introduction - from scratch)