leetcode-67. Add Binary
来源:互联网 发布:nginx配置几大模块 编辑:程序博客网 时间:2024/05/16 09:04
leetcode-67. Add Binary
题目:
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
public class Solution { public String addBinary(String a, String b) { String sb = ""; int i = a.length()-1, j = b.length()-1,c = 0; for(;i >= 0 && j >= 0 ; i--,j--){ int n1 = a.charAt(i)-'0'; int n2 = b.charAt(j)-'0'; n1 = n1+n2+c; c = n1/2; n1 = n1%2; sb = n1+sb; } System.out.println(i+" "+c+" "+j); while(i>=0){ int n1 = a.charAt(i)-'0'; n1 = n1 + c; c = n1/2; n1 = n1%2; sb = n1+sb; i--; } while(j>=0){ int n1 = b.charAt(j)-'0'; n1 = n1 + c; c = n1/2; n1 = n1%2; sb = n1+sb; j--; } if(c==1) sb = 1+sb; return sb.length()==0?"0":sb; }}
0 0
- [LeetCode]67.Add Binary
- LeetCode --- 67. Add Binary
- [Leetcode] 67. Add Binary
- [leetcode] 67.Add Binary
- 【leetcode】67. Add Binary
- [leetcode] 67.Add Binary
- [leetcode] 67. Add Binary
- 【Leetcode】67. Add Binary
- 67. Add Binary LeetCode
- 【LeetCode】67. Add Binary
- leetcode--67. Add Binary
- leetcode 67. Add Binary
- LeetCode 67. Add Binary
- LeetCode *** 67. Add Binary
- leetcode 67. Add Binary
- 【leetcode】67.Add Binary
- LeetCode 67. Add Binary
- LeetCode-67.Add Binary
- java-面向对象
- Android Drawable详解
- 第十六周项目4—— 英文单词的基数排序
- 常见问题记录
- 【jQuery】Jquery.form.js实现表单异步提交以及文件上传(带进度条)
- leetcode-67. Add Binary
- iOS - 实现UINavigation全屏滑动返回(一)
- TCP三次握手建立连接
- 概率是一种面积
- Volley所遇到的坑
- 多重背包二进制优化
- sparkStreaming 连接数据库 --设计模式
- [LeetCode]Reverse Bits
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(判直角)