当前位置:网站首页>[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
*/边栏推荐
- Flink实时仓库-DWD层(kafka-关联mysql的lookup join)模板代码
- Flink实时仓库-DWD层(处理复杂数据-流和表的装换处理)模板代码
- Flink实时仓库-DWD层(下单-多张表实现join操作)模板代码
- Difference between CNAME record and a record
- Improved pillar with fine grained feature for 3D object detection paper notes
- JVM之垃圾回收机制(GC)
- LDAP brief description and unified authentication description
- Not so simple singleton mode
- Teacher Wu Enda machine learning course notes 01 introduction
- 【C语言刷LeetCode】1054. 距离相等的条形码(M)
猜你喜欢

Apisik health check test

SSH免密登录-两台虚拟机建立免密通道 双向信任

How to write controller layer code gracefully?

DM data guard cluster setup

线程同步—— 生产者与消费者、龟兔赛跑、双线程打印

猜数字//第一次使用生成随机数

Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)

新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!

LDAP brief description and unified authentication description

Thread synchronization - producers and consumers, tortoise and rabbit race, dual thread printing
随机推荐
leetcode-1331:数组序号转换
Decompilation of wechat applet
SSH免密登录-两台虚拟机建立免密通道 双向信任
SS command details
MySql基础知识(高频面试题)
IDEA中实现Mapper接口到映射文件xml的跳转
记 - 踩坑-实时数仓开发 - doris/pg/flink
vscode通过remotessh结合xdebug远程调试php解决方案
网上传说软件测试培训真的那么黑心吗?都是骗局?
Pod基本介绍
Relative date used by filter in salesforce
MySQL queries are case sensitive
Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)
SSH password free login - two virtual machines establish password free channel two-way trust
Windows 上 php 7.4 连接 oracle 配置
10 frequently asked JVM questions in interviews
新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
Federal learning backdoor attack summary (2019-2022)
线程 - 线程安全 - 线程优化
vim文本编辑器的一些使用小技巧