当前位置:网站首页>67. binary sum
67. binary sum
2022-06-11 21:57:00 【Code loving students】
Topic introduction :
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 :
Input : a = "11", b = "1" Output : "100"
Then we have two solutions to this problem :
1. The string a ,b The binary represented in is converted into numbers , Add the two to get the sum , Finally, the binary of the sum is stored in a return array .
2. We do a similar addition operation for two strings , Last result returned .
about 1 Although the method is the easiest to understand , But it also faces the problem of overflow in different situations , So here we use the second method to realize the problem .
The code is as follows :
char * addBinary(char * a, char * b){
int Base = 0; // carry
int length = (strlen(a)>strlen(b)? strlen(a)+2:strlen(b)+2);
//+2 The reason for this is that the two binaries add up to one bit , The other is used to fill in '\0'
char* result = (char*)malloc(sizeof(char)*length); // Open up space
result[length-1] = '\0'; // The last person to open up space is '\0' It means the ending
for(int i = strlen(a)-1,j = strlen(b)-1,k = length -2; (i >=0)||(j >= 0); i--,j--,k--)
{
int sum = Base;
sum += (i >= 0? a[i]-'0':0);
sum += (j >= 0? b[j]-'0':0);
Base = sum /2;
result[k] = '0'+ sum % 2;
}
if(Base == 0) // No carry , Go straight back to
return result+1; // Last +1 Then skip the address without carry , But it may lead to cross-border
result[0] = '1'; // Have carry , Make up for the highest position
return result;
}边栏推荐
- 行而不辍,未来可期|云扩科技入选上海市专精特新企业
- 继承的所有特征
- 238.除自身以外数组的乘积
- Leetcode-129- sum of numbers from root node to leaf node
- R language book learning 03 "in simple terms R language data analysis" - Chapter 12 support vector machine Chapter 13 neural network
- Builder pattern
- Redis transaction
- 2022-02-28(1)
- Servlet get form data
- 每日一题-删除有序数组的重复项
猜你喜欢

Flink error: multiple tasks are started, and only one task is executed

Collection of articles and literatures related to R language (continuously updated)

206. reverse linked list

类和对象(2)

189. rotation array

「大模型」之所短,「知识图谱」之所长

超标量处理器设计 姚永斌 第2章 Cache --2.4 小节摘录

win10字体模糊怎么调节
![[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)](/img/73/2ec957d58616d692e571a70826787f.jpg)
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)

【学术相关】申请审核制下,到双一流大学读博的难度有多大?
随机推荐
In the post epidemic era, how can enterprise CIOs improve enterprise production efficiency through distance
多态的所有特征
超标量处理器设计 姚永斌 第2章 Cache --2.3 小节摘录
JVM | virtual machine stack (local variable table; operand stack; dynamic link; method binding mechanism; method call; method return address)
揭秘爆款的小程序,为何一黑到底
69. x的平方根
In the future, cloud expansion technology is expected to be selected as a specialized, special and new enterprise in Shanghai
判断大小端存储两种办法
建造者模式
科普 | NFT的类型有哪些(上)
Huawei equipment configuration h-vpn
2022-02-28(1)
Usage of esp32c3 Arduino Library
ESP32C3 Arduino库使用方法
[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?
快速排序的优化
Carry and walk with you. Have you ever seen a "palm sized" weather station?
R language book learning 03 "in simple terms R language data analysis" - Chapter 7 linear regression model
二分查找 - 学习
如何查看win系统的安装日期