当前位置:网站首页>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;
}
};
边栏推荐
- Activiti workflow gateway
- Slow query log in MySQL
- Prometheus+blackbox exporter+grafana monitoring server port and URL address
- Postman报Json序列化错误
- [C language brush leetcode] 1604. Warn people who use the same employee card more than or equal to three times within an hour (m)
- Worthington papain - production of glycopeptides from purified proteoglycans (attached Literature)
- 1. Mx6ul core module use serial -rs485 test (x)
- 1. Mx6ul core module serial WiFi test (VIII)
- [leetcode] 32. Longest valid bracket
- Error reporting caused by local warehouse
猜你喜欢

Wechat applet decryption and unpacking to obtain source code tutorial

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

主键B+ Tree,二级索引B+ Tree及对应的查询过程分析

Worthington papain - production of glycopeptides from purified proteoglycans (attached Literature)

Composition API的优势

I.MX6UL核心模块使用连载-nand flash读写测试 (三)

I.MX6UL核心模块使用连载-eMMC读写测试 (四)

Implementation of Ti am335x industrial control module network and file system nfs

I.MX6UL核心模块使用连载-查看系统信息 (二)

National standard gb28181 protocol video platform easygbs message pop-up mode optimization
随机推荐
Dqn pytoch example
[paper reading] coat: CO scale conv attentional image transformers
LeetCode_动态规划_中等_264.丑数 II
[xxl-job] xxl-job learning
(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
[2021] [paper notes] 6G technology vision - otfs modulation technology
[C language brush leetcode] 1604. Warn people who use the same employee card more than or equal to three times within an hour (m)
Implementation of Ti am335x industrial control module network and file system nfs
I.MX6UL核心模块使用连载-Iot-6ULX核心模块简要介绍 (一)
(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
I.MX6UL核心模块使用连载-eMMC读写测试 (四)
1. Mx6ul core module serial use - touch screen calibration (IX)
i.MX6ULL SNVS电源域GPIO状态保持验证
Prometheus+blackbox exporter+grafana monitoring server port and URL address
LeetCode302场周赛第三题--裁剪数字后查询第 K 小的数字
1. Mx6ul core module use serial -rs485 test (x)
图解B+树的插入过程
Navica tool imports remote MySQL into local MySQL database
Adruino basic experimental learning (I)
(Dynamic Programming Series) sword finger offer 48. the longest substring without repeated characters