本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/40480151

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

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

通常情况下,我们会考虑使用现有的Interger.valueOf(String s, int radix)或者Long.valueOf(String s, int radix)直接进行转换,将二进制转换为十进制,相加后

再使用toBinaryString()方法转换为二进制数。

	private static String addBinary(String a, String b) {
		int x = Integer.valueOf(a, 2);
		int y = Integer.valueOf(b, 2);
		int z = x + y;
		String binaryString = Integer.toBinaryString(z);
		return binaryString;
	}
	private static String addBinary(String a, String b) {
		long x = Long.valueOf(a, 2);
		long y = Long.valueOf(b, 2);
		long z = x + y;
		String binaryString = Long.toBinaryString(z);
		return binaryString;
	}

但是,如果给定的字符串的长度大于int的最大值、long的最大值就会抛出java.lang.NumberFormatException异常。在给定了字符串的范围的情况下,可以考

虑使用上面的两种方法,那样效率会高一些。

如果不确定字符串的范围,最好使用下面的方法进行操作。

private static String addBinary(String a, String b) {
		/** i、j分别指向a、b的末尾字符 **/
		int i = a.length() - 1;
		int j = b.length() - 1;
		/** 进位标记 **/
		int carry = 0;
		/** 将String转为char数组 **/
		char[] achar = a.toCharArray();
		char[] bchar = b.toCharArray();
		/** 结果数组 **/
		char[] resultchar = new char[Math.max(achar.length, bchar.length) + 2];
		/** 标记结果数组位置 **/
		int resultIndex = 0;

		while (true) {
			if (i < 0 && j < 0 && carry == 0)
				break;

			int aflag = 0;
			int bflag = 0;

			if (i >= 0)
				aflag = achar[i] - '0';
			if (j >= 0)
				bflag = bchar[j] - '0';

			if (aflag + bflag + carry > 1) {
				resultchar[resultIndex] = (char) ('0' + aflag + bflag + carry - 2);
				carry = 1;
			} else {
				resultchar[resultIndex] = (char) ('0' + aflag + bflag + carry);
				carry = 0;
			}
			resultIndex++;
			i--;
			j--;
		}

		String result = new String(resultchar, 0, resultIndex);
		StringBuffer buffer = new StringBuffer(result);
		result = buffer.reverse().toString();

		return result;
	}

												

Leetcode_67_Add Binary的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  5. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  6. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  7. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  8. [LeetCode] Binary Watch 二进制表

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  9. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

随机推荐

  1. consul怎么在windows下安装

    1.去官网下载:https://www.consul.io/downloads.html 2.解压: 3.设置环境变量:path添加 E:\programfiles\consul: 4.cmd启动: ...

  2. SSH(struts2+hibernate+spring)总结

    1 前三个文章 是我对ssh的具体实现 虽然没有真的写一个ssh的例子出来 但是 意思应该传达到了 主要还是注解注入的ssh太模块化了 感觉写出来意义不大 个人水平有限 说不清 2 我一开是写的是st ...

  3. 项目管理软件系列-Linux一键安装禅道

    linux用一键安装包 简介:本文介绍如何在linux下面使用禅道一键安装包搭建禅道的运行环境. linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅 ...

  4. 3-学习GPRS_Air202(需要知道的关于Lua的一些基本的知识)

      http://www.cnblogs.com/yangfengwu/p/8948935.html 学东西一定是打破沙锅学到底,有问题就解决问题,不要试图去回避或者放弃解决当前的问题,如果总是回避或 ...

  5. 关于熊猫认证软件IOS安装步骤教程(适用于其他软件)

    IOS运行企业版应用教程 1.扫描二维码之后微信进入界面,如下图所示:点击右上角三个点   2.弹出分享界面,如图所示:点击苹果自带浏览器(sarfari)     3.进入苹果自带浏览器后如图所示, ...

  6. Do a web framework ourselves

    step 1: from wsgiref.simple_server import make_server def application(environ, start_response): star ...

  7. Activtiy完全解析(一、Activity的创建过程)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52452218 本文出自:[openXu的博客]   在Android开发过程中,我们几乎每天 ...

  8. Matplotlib Toolkits:地图绘制工具

    Matplotlib Toolkits:地图绘制工具 有没有一种可以直接在详细地图(如谷歌地图)上绘制上百万坐标点的工具???谷歌地图坐标点多了也不能绘制了. Basemap (Not distrib ...

  9. 让sublime总是在新选项卡打开新文件

    sublime的一个默认设置让人很不爽,比如现在选项卡里面已经打开了一个文件A,当你从左边side bar里面点击一个新文件B时,如果你不是快速的双击,且A没有处于编辑未保存状态,那么B就会覆盖A的选 ...

  10. Hadoop学习笔记1:伪分布式环境搭建

    在搭建Hadoop环境之前,请先阅读如下博文,把搭建Hadoop环境之前的准备工作做好,博文如下: 1.CentOS 6.7下安装JDK , 地址: http://blog.csdn.net/yule ...