当前位置:网站首页>Li Kou daily question - day 39 -67. Binary sum
Li Kou daily question - day 39 -67. Binary sum
2022-07-26 02:17:00 【Chongyou research Sen】
2022.7.18 Did you brush the questions today ?
subject :
Here are two binary strings , Returns the sum of them ( In binary ).
Input is Non empty String and contains only numbers 1 and 0.
analysis :
Two binary arrays of strings , We need to sum the two arrays , Then output according to the characters in binary form .
Ideas : Find the result , And then go . Then the following steps are required :
1. Convert string to integer
2. Integer sum
3. Sum integer to binary
4. Binary to char
5.char To string
analysis :
1. Summation ( The title does not apply , Because it sets the result very large, resulting in the sum overflow )
class Solution {
public:
string addBinary(string a, string b) {
long long res = 0;
string qwe;
string ans;
res= sum_binary(a)+ sum_binary(b);
// Get the sum result , Now turn to string
qwe=int_bin(res, ans);
cout << 1;
reverse(qwe.begin(), qwe.end());
return qwe;
}
int sum_binary(string s)
{
reverse(s.begin(), s.end());
int length = s.size();
long sum = 0;
for (int i=0;i<length;i++)
{
if (s[i] == '1')
{
sum = sum +pow(2,i);
}
}
return sum;
}
string int_bin(int num,string res)
{
int a[100];
int i = 0;
char c;
if (num == 0)
{
return "0";
}
for (; num > 0; i++)
{
a[i] = num % 2;
num = num / 2;
}
//a In the store 010101, Now it's char And insert str
for (int q = 0; q < i; q++)
{
c = a[q]+'0';
res.push_back(c);
}
return res;
}
};2 Carry method
1. First find the longest array
2.carry As a carry number , The result of each location =carry+a【i】+b【i】
3. The result of each bit is % Handle , Judge whether carry is needed ,
4. Judge the last carry Whether it is 1, yes 1 Then carry
class Solution {
public:
string addBinary(string a, string b) {
string ans;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int n = max(a.size(), b.size()), carry = 0;
for (size_t i = 0; i < n; ++i) {
carry += i < a.size() ? (a.at(i) == '1') : 0;
carry += i < b.size() ? (b.at(i) == '1') : 0;
ans.push_back((carry % 2) ? '1' : '0');
carry /= 2;
}
if (carry) {
ans.push_back('1');
}
reverse(ans.begin(), ans.end());
return ans;
}
};
边栏推荐
猜你喜欢

我来图书馆小程序签到流程分析

I came to the library applet one click sign in and one click grab location tool

1. Mx6ul core module serial -iot-6ulx core module brief introduction (I)

Prometheus + process exporter + grafana monitor the resource usage of the process

I.MX6UL核心模块使用连载-WIFI测试 (八)

1. Mx6ul core module uses serial can and buzzer test (XI)

SQL manual blind injection and error reporting injection

【2020】【论文笔记】磁控溅射法生长Bi2Te3/CoFeB双层异质结——

项目管理:精益管理法
![[2019] [paper notes] tunable THz broadband absorption based on metamaterials——](/img/ae/bbebba30d0d6f58d65531cff48b5e9.png)
[2019] [paper notes] tunable THz broadband absorption based on metamaterials——
随机推荐
【2020】【论文笔记】磁控溅射法生长Bi2Te3/CoFeB双层异质结——
还在用==0 null equal 判断空值吗,对isEmpty 和 isBlank有多少了解呢
数仓:银行业数仓的分层架构实践
Acwing 379. hide and seek problem solution (minimum path non repeating point coverage)
1205 Lock wait timeout exceeded; Try restarting transaction processing
由一个数据增量处理问题看到技术人员的意识差距
CD from grabbing the track to building a streaming media server -- a case study of "moon in the hometown of sleep"
本地仓库导致的报错
Business Intelligence BI full analysis, explore the essence and development trend of Bi
图解B+树的插入过程
Build embedded development environment and FRP penetration under win
(Dynamic Programming Series) sword finger offer 48. the longest substring without repeated characters
1. Mx6ul core module uses serial EMMC read / write test (IV)
Niuke net question brushing training (I)
Postman reported JSON serialization error
【PyQt5打包为exe】
obsidian移动端PC段同步
1. Mx6ul core module use serial -rs485 test (x)
Relationship between HTC mobile official solution, s-on/s-off and super CID
【2021】【论文笔记】6G技术愿景——OTFS调制技术