二进制加法

输入2个字符串,字符串内由0和1组成;计算二者之和,返回字符串

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

 package com.rust.TestString;

 public class AddBinary {
     public static String addBinary(String a, String b) {
         int alen = a.length() - 1;
         int blen = b.length() - 1;
         int carry = 0;
         String res = "";
         while (alen >=0 || blen >= 0 || carry == 1){
             int delta = (alen < 0)? 0 : a.charAt(alen--) - '0';/* 得到int */
             int beta = (blen < 0)? 0 : b.charAt(blen--) - '0';
             res = (char)('0' + delta ^ beta ^ carry) + res;/* 异或处理得到当前位 */
             carry = (delta + beta + carry) >> 1;/* 移位处理得到进位 */
         }
         return res;
     }

     public static void main(String args[]){
         String text = "0101010";
         String atext =    "111";
         String btext =    "010";
         System.out.println(addBinary(atext, text));
         System.out.println(addBinary(atext, btext));
         System.out.println(addBinary(btext, text));
     }
 }

输出:

0110001
1001
0101100

AddBinary的更多相关文章

  1. leetcode — add-binary

    /** * Source : https://oj.leetcode.com/problems/add-binary/ * * * Given two binary strings, return t ...

  2. add-binary 字符串操作,二进制字符串相加

    Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...

  3. [LeetCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  4. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  5. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  6. 全部leetcode题目解答(不含带锁)

    (记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.)   88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...

  7. 【leetcode】Add Binary

    题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...

  8. leetcode刷题全纪录(持续更新)

    2.Add Two Numbers 原题链接https://leetcode.com/problems/add-two-numbers/ AC解: public ListNode addTwoNumb ...

  9. LeetCode---String

    Count and Say 思路:递归求出n - 1时的字符串,然后双指针算出每个字符的次数,拼接在结果后面 public String countAndSay(int n) { if(n == 1) ...

随机推荐

  1. 连接Oracle数据库的时候报了“Got minus one from a read call”

    (转) 出现这种问题基本上就以下几种原因,可以查一下系统日志看看:1:数据库连接满了,扩大数据库连接池2:所登录的机子IP不在sqlnet.ora内,加入后重启listerner即可3:数据库负载均衡 ...

  2. Java中设计模式之生产者消费者模式-4

    引言 生产者-消费者(producer-consumer)问题,也称作有界缓冲区(bounded-buffer)问题,两个进程共享一个公共的固定大小的缓冲区.其中一个是生产者,用于将消息放入缓冲区:另 ...

  3. dotnet 命令实战

    以下用实例串起dotnet所有命令,带你玩转dotnet命令. 1.创建(dotnet new) 首先我们创建一个项目,这里我们创建控制台程序,命令如下图所示. dotnet new dotnet n ...

  4. cpp(第七章)

    1.c++提供了3种表示c—风格字符串方法:字符数组,字符串常量,字符串指针.其中字符数组并不一定是字符串,以空值字符'\0'来结束的字符数组时字符串. 2.函数参数为数组时,虽然减少了时间和内存的使 ...

  5. java环境变量最佳配置

    1.打开我的电脑--属性--高级--环境变量  2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:C:\Program Files\Java\jdk1.7. ...

  6. 安装nginx+lua开发环境

    一.安装nginx及搭建本地测试环境 1.创建安装目录:    /data/nginx2.安装make:        yum-y install gcc automake autoconf libt ...

  7. JavaScript事件与例子(三)

    两个例子,好友选中效果和左侧右侧子菜单 一.好友选中效果 可以通过设置属性的方式判断当前是否被选中,也可以通过获取当前元素的颜色从而得知当前元素状态是否被选中,从而进行操作 1.通过设置属性的方式判断 ...

  8. 文件存储B+树

    文件存储要选用B+树这样的数据结构 “文件存储要选用B+树这样的数据结构”——没记错的话,这是严蔚敏那本数据结构书上的一句结论.不知道是我没细看还是她没细讲,反正当时纯粹应试地记了这么个结论.不求甚解 ...

  9. 一起学习c++11——c++11中的新语法

    c++11新语法1: auto关键字 c++11 添加的最有用的一个特性应该就是auto关键字. 不知道大家有没有写过这样的代码: std::map<std::string, std::vect ...

  10. java怎么处理json数据

    json = new JSONObject(data); int which = json.optInt("which", -1); String label = json.opt ...