leetcode67:Add Binary
来源:互联网 发布:opengl编程指南 第9版 编辑:程序博客网 时间:2024/06/05 18:44
package com.leetcode;public class Solution67 { public static String addBinary(String a, String b) { if( a == null || a.isEmpty() ){ return b; } if( b == null || b.isEmpty() ){ return a; } char[ ] aArray = a.toCharArray(); char[ ] bArray = b.toCharArray(); StringBuilder std = new StringBuilder(); int i = aArray.length - 1; int j = bArray.length - 1; int flag = 0; while( i > -1 || j> -1 || flag == 1 ){ int aByte = (i > -1) ? Character.getNumericValue( aArray[ i-- ] ) : 0 ; int bByte = (j > -1) ? Character.getNumericValue( bArray[ j-- ] ) : 0 ; int result = aByte ^ bByte^ flag; flag = aByte + bByte + flag >=2 ? 1 : 0 ; std.append(result); } return std.reverse().toString(); }public static void main(String[] args) {System.out.println(Solution67.addBinary("1", "1"));}}
0 0
- LeetCode67. Add Binary
- leetcode67:Add Binary
- LeetCode67. Add Binary
- LeetCode67. Add Binary
- LeetCode67. Add Binary
- Leetcode67. Add Binary
- LeetCode67 Add Binary
- leetcode67-Add Binary
- LeetCode67——Add Binary
- LeetCode67——Add Binary
- leetcode67---Add Binary(二进制加法)
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Add Binary
- Android自定义dialog
- const用法
- 文章标题
- php字符串操作
- 陶哲轩实分析 附录 A 习题解答
- leetcode67:Add Binary
- MyEclipse中开发web项目遇到的一些问题
- LeetCode-58.Length of Last Word
- 关于回调函数的一些理解
- Maven在IntelliJ IDEA中的学习笔记(2)-----配置maven私服
- POJ 3041 Asteroids(最小点覆盖)
- MFC tab控件使用
- WebSocket不同版本的三种握手方式以及一个Netty实现JAVA类
- STM32+freertos+lwip