Leetcode - Add Binary -Jan 30, 2015
来源:互联网 发布:淘宝教育怎么入驻 编辑:程序博客网 时间:2024/06/08 07:05
Leetcode - add binary String
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
For example,
a = "11"
b = "1"
Return "100".
public class Solution { public String addBinary(String a, String b) { StringBuilder str = new StringBuilder();int alen = a.length() ;int blen = b.length();int count = 0;int carry = 0;if(alen == blen) {for(int i=alen-1; i >= 0; i--) {if(a.charAt(i) == '1') {count++;}if( b.charAt(i) == '1') {count++;}if(carry == 1 ) {count++;}carry = (count > 1 ? 1: 0);str.insert(0,((count ==0 ) || count ==2)? '0': '1' );count = 0;}if( carry==1) {str.insert(0, '1');}}if(alen > blen) {b = "0" + b;System.out.println("b="+ b);return addBinary(a, b);}if(alen < blen) {a = "0" +a;return addBinary(a,b);} return str.toString(); }
0 0
- Leetcode - Add Binary -Jan 30, 2015
- LeetCode: Add Binary
- [Leetcode] Add Binary
- LeetCode Add Binary
- Leetcode: Add Binary
- [Leetcode] Add Binary
- LeetCode : Add Binary
- LeetCode Add Binary
- [LeetCode] Add Binary
- leetcode 58: Add Binary
- leetcode Add Binary
- [LeetCode] Add Binary
- 【leetcode】Add Binary
- [LeetCode]Add Binary
- [leetcode]Add Binary
- LeetCode-Add Binary
- [leetcode] Add Binary
- LeetCode - Add Binary
- EnumPrintProcessors function
- DRIVER_INFO_6 structure
- poj 1208 The Blocks Problem 模拟+vector的使用
- Android多线程断点下载文件
- Longest Substring Without Repeating Characters
- Leetcode - Add Binary -Jan 30, 2015
- poj 2160 Box 模拟
- DevCon 命令行实用工具可替代设备管理器
- HTML5 声明兼容IE的写法
- 养成定时更新内容的好习惯,提高搜索引擎时效性收录
- lv官方網 wqdx axsr jguq
- 最大子列和问题
- Two Sum Closest to K
- django提交表单提示"You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SL