leetcode67.AddBinary
来源:互联网 发布:数据分析相关书籍 编辑:程序博客网 时间:2024/06/15 15:43
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) { int flag=0; int indexa=a.size()-1; int indexb=b.size()-1; string c; while(indexa >=0 && indexb >= 0) { int num=(a[indexa]-'0')+(b[indexb]-'0')+flag; flag=num/2; num=num%2; c = (char)(num + '0') + c; indexa--; indexb--; } while(indexa>=0) { int num=(a[indexa]-'0')+flag; flag=num/2; num=num%2; c=(char)(num + '0') + c; indexa--; } while(indexb>=0) { int num=(b[indexb]-'0')+flag; flag=num/2; num=num%2; c=(char)(num +'0')+c; indexb--; } if(flag>0) { c=char(flag+'0')+c; } return c; }};
0 0
- leetcode67.AddBinary
- addbinary
- AddBinary
- LeetCode|AddBinary
- LeetCode AddBinary
- AddBinary[LeetCode]
- AddBinary(leetcode)
- leetCode JavaScript addBinary解析
- LeedCode 之 AddBinary
- leecode-easy-addbinary
- Leetcode67: First Bad Version
- LeetCode67. Add Binary
- leetcode67:Add Binary
- LeetCode67. Add Binary
- LeetCode67. Add Binary
- leetcode67 二进制加法
- LeetCode67. Add Binary
- Leetcode67. Add Binary
- ACM路上的一大失误
- QTCreator 生成可执行程序不能运行的问题
- Hdu 1076 An Easy Task【水】
- CSS3:有雪花的导航栏实例
- hdu 1286找新朋友 (欧拉函数筛法)
- leetcode67.AddBinary
- uva 11609 组合数学
- 每天几个Linux命令01_ls命令
- linux同步(二)---等待队列
- jquery与checkbox的checked属性的问题
- poj1061青蛙的约会 (扩展欧几里德)
- java项目——mysql简单操作
- 执行力就要“多快好省”
- 关于java的访问权限控制,以关于继承的this和super小结