Leetcode_Add Binary
来源:互联网 发布:数据库前置库 编辑:程序博客网 时间:2024/06/16 09:11
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
class Solution {public: string addBinary(string a, string b) { size_t na = a.size(); int nb = b.size(); int n = na > nb ? na : nb; string result(n,'0'); int ch= 0; int i= n-1,ia=na-1,ib=nb-1; for(;ia >=0 && ib >= 0; ){ int re = a[ia]-'0'+b[ib]-'0' + ch; ch=re/2; result[i]=re%2+'0'; ia--; ib--; i--; } //int re = a[0]+b[0]-'0'+'0'+ch; while(ia >=0){ int re = a[ia]-'0' +ch; ch = re/2; result[i] = re%2+'0'; ia--; i--; } while(ib >=0){ int re = b[ib]-'0'+ch; ch = re/2; ib--; result[i]= re%2+'0'; i--; } if(ch){ result.insert(0,"1"); } return result; }};
0 0
- LeetCode_Add Binary
- Leetcode_add-binary
- Leetcode_Add Binary
- LeetCode_Add Binary
- Leetcode_Add Binary
- LeetCode_Add Binary
- leetcode_Add Binary
- LeetCode_Add Digits
- Leetcode_Add Two Numbers
- leetcode_Add Two Numbers
- LeetCode_Add Two Numbers
- Leetcode_Add Two Numbers
- LeetCode_Add Two Numbers
- LeetCode_Add Two Numbers
- leetcode_add two nums
- LeetCode_Add Two Numbers(Medium)
- leetcode_add two number
- leetCode_Add and Search Word - Data structure design
- 黑马程序员_字符串
- 10个Linux Shell脚本的面试题及答案
- IOS越狱开发 root权限获取
- LoadLibraryEx(DONT_RESOLVE_DLL_REFERENCES) is fundamentally flawed
- web.xml 配置 Error Page
- Leetcode_Add Binary
- poj2056
- Pinterest用“引导式搜索”挑战谷歌?
- php 学习笔记 5
- 命令模式(CommandPattern)
- 机器学习-回归及分类问题总结
- Surprising Strings(STL)
- OC中的方法
- Acdream 1148 GCD SUM 莫比乌斯