本文是在学习中的总结,欢迎转载但请注明出处: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. 操作系统内存管理之 内部碎片vs外部碎片

    外部碎片:因为行程持续地被载入与置换,使得可用的记忆体空间被分割成许多不连续的区块.虽然记忆体所剩空间总和足够让新行程执行,却因为空间不连续,导致程式无法载入执行.内部碎片:发生在以固定长度分割区来进 ...

  2. 41. First Missing Positive(困难, 用到 counting sort 方法)

    Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...

  3. 基于babylon3D模型研究3D骨骼动画(1)

    3D骨骼动画是实现较为复杂3D场景的重要技术,Babylon.js引擎内置了对骨骼动画的支持,但Babylon.js使用的骨骼动画的模型多是从3DsMax.Blender等3D建模工具转换而来,骨骼动 ...

  4. Nginx 配置HTTPS 与Node.js 配置HTTPS方法

    前段时间公司网站要求加上HTTPS安全CA证书,公司服务器全是阿里云服务器,并且配有负载均衡,所以选择直接在阿里云购买CA证书,阿里云有一种证书可以免费试用一年,决定申请此证书,阿里云证书需要验证,阿 ...

  5. Appium--入门demo

    Appium环境搭建已经在在博客中写出 http://www.cnblogs.com/feimaoyuzhubaobao/p/5057832.html   那么本篇博客主要介绍java版本的appiu ...

  6. Jedis分片Sentinel连接池实验

    Jedis分片Sentinel连接池实验 1.起因 众所周知,Redis官方HA工具Sentinel已经问世很久了,但令人费解的是,Jedis官方却迟迟没有更新它的连接池.到目前Maven库中最新的2 ...

  7. Redis之(七)主从同步与集群管理

    8.1 主从同步原理 像MySQL一样,Redis是支持主从同步的,而且也支持一主多从以及多级从结构. 主从结构,一是为了纯粹的冗余备份,二是为了提升读性能,比如很消耗性能的SORT就可以由从服务器来 ...

  8. Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...

  9. android studio 转为eclipse快捷键后还存在的问题汇总

    提取局部变量:Ctrl+Alt+V 提取全局变量:Ctrl+Alt+F 提取方法:Shit+Alt+M 使用android studio 出现红色下划线代表有错误产生,eclipse中的Ctrl+1( ...

  10. PGM:图模型学习概述

    http://blog.csdn.net/pipisorry/article/details/52571640 动机 前面我们讨论的问题出发点是给定一个图模型.如在独立性和推理讨论中,假定模型--结构 ...