当前位置:网站首页>[C language brush leetcode] 67. binary sum (E)
[C language brush leetcode] 67. binary sum (E)
2022-07-29 07:04:00 【kinbo88】
【
Here are two binary strings , Returns the sum of them ( In binary ).
Input is Non empty String and contains only numbers 1 and 0.
Example 1:
Input : a = "11", b = "1"
Output : "100"
Example 2:
Input : a = "1010", b = "1011"
Output : "10101"
Tips :
Each string consists only of characters '0' or '1' form .
1 <= a.length, b.length <= 10^4
If the string is not "0" , No leading zeros .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/add-binary
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
】
A simple question , Unexpectedly, the code was written for a while , It's so delicious
1. Apply for a character array instead of int Array
2. Before operation -'0', Required after operation +'0'
3. When a[i]+b[i] This needs to be reduced twice '0'
4. Length of application len + 2, Remember to initialize
5. Reverse operation needs to be memorized , Instead of thinking for a while before writing
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; // The reverse
j = idx - 1;
while (i < j) {
char tmp = retarr[i];
retarr[i] = retarr[j];
retarr[j] = tmp;
i++;
j--;
}
return retarr;
}
/*
A simple question , Unexpectedly, the code was written for a while , food
1. Apply for a character array instead of int Array
2. Before operation -'0', Required after operation +'0'
3. When a[i]+b[i] This needs to be reduced twice '0'
4. Length of application len + 2, Remember to initialize
5. Reverse operation needs to be memorized , Instead of thinking for a while before writing
*/边栏推荐
猜你喜欢
随机推荐
模拟卷Leetcode【普通】172. 阶乘后的零
【flask入门系列】Flask-SQLAlchemy的安装与配置
太空射击第17课: Game Over (結束)
上采样之反卷积操作
Not so simple singleton mode
SSH免密登录-两台虚拟机建立免密通道 双向信任
10 frequently asked JVM questions in interviews
2022年SQL经典面试题总结(带解析)
线程 - 线程安全 - 线程优化
吴恩达老师机器学习课程笔记 01 引言
吴恩达老师机器学习课程笔记 04 多元线性回归
Implementation of DDP cluster distributed training under pytoch multi GPU conditions (brief introduction - from scratch)
Teacher Wu Enda machine learning course notes 01 introduction
SS command details
模拟卷Leetcode【普通】150. 逆波兰表达式求值
建木持续集成平台v2.5.2发布
Improved pillar with fine grained feature for 3D object detection paper notes
IDEA中实现Mapper接口到映射文件xml的跳转
【解决方案】ERROR: lib/bridge_generated.dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
Decompilation of wechat applet









