leetcode_67 add binary
来源:互联网 发布:国债 知乎 编辑:程序博客网 时间:2024/05/01 03:35
string addBinary(string a, string b){if(a=="0" || a.empty())return b;if(b=="0" || b.empty())return a;int indexa = a.size()-1;int indexb = b.size()-1;string result;int aa,bb, rem,carry ;aa=bb=rem=carry = 0;//while(indexa>=0 || indexb >=0){//aa= indexa<0 ? 0 : a[indexa]-'0'; //转换成数字 //bb = indexb<0? 0 : b[indexb]-'0';//rem = (aa+bb+carry)%2;//carry = (aa+bb+carry)/2;//////rem = rem+'0'; //转换成字符 ////rem += rem;//result.insert(result.begin(),rem+'0'); //每次插入到string的首部,就不用翻转了 ////indexa--;//indexb--;//} for(;indexa>=0||indexb>=0; indexa--,indexb--){ aa= indexa<0 ? 0 : a[indexa]-'0'; //转换成数字 bb = indexb<0? 0 : b[indexb]-'0';rem = (aa+bb+carry)%2;carry = (aa+bb+carry)/2;result.insert(result.begin(),rem+'0'); } if(carry==1)result.insert(result.begin(),'1');cout << result << endl;//strrev((char*)result.c_str());//reverse(result.begin(),result.end());return result;}
0 0
- LeetCode_67---Add Binary
- leetcode_67 add binary
- leetcode_67. Add Binary
- leetcode_67. Add Binary 字符串形式的二进制数相加
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- C primer plus 第九章 练习5:
- input等表单项值或属性发生变化时的事件监听
- swift学习笔记之类和对象
- 173. Binary Search Tree Iterator
- 51NOD 1040 最大公约数之和(分析 + 欧拉函数)
- leetcode_67 add binary
- iOS开发沙盒机制文件操作
- sdau三 1012
- 适配器Adapter
- ZOJ-3777 Problem Arrangement(dp状态压缩)
- ID3算法实验
- C/C++除法实现方式及负数取模详解
- Android 6.0的变化之权限管理
- 根据浏览器截图