leetcode | Add Binary
来源:互联网 发布:java 特殊字符转义 编辑:程序博客网 时间:2024/05/17 04:10
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) { StringBuilder sp = new StringBuilder(); int aindex = a.length()-1; int bindex = b.length()-1; int carry = 0; int sum = 0; while(aindex>=0 || bindex>=0) { int x = aindex >=0? a.charAt(aindex) - '0':0; int y = bindex >=0? b.charAt(bindex) - '0':0; sum = x ^ y ^ carry; carry = x + y + carry > 1? 1 : 0; sp.insert(0,sum); aindex--; bindex--; } if(carry==1) sp.insert(0,1); return sp.toString(); }}
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 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
- leetcode:Add Binary
- 单例模式与单例工厂
- json 动态生成
- JavaScript中作用域的学习笔记
- Java基础学习总结----数据类型和运算符
- Converting Drive Letters to MS-DOS INT 13H Disk Drive Numbers
- leetcode | Add Binary
- mac下redis的安装
- Slidemenu(侧滑)的使用(三种使用方式)及属性详解
- Avd Manager在创建AVD时,使用的究竟是哪里的img?
- Android SharedPreferences Storage
- Git 创建版本库
- C++ 学习记录之静态变量static
- 数据库概论小结
- error C2653: “CXXXX”: 不是类或命名空间名称