本文是在学习中的总结,欢迎转载但请注明出处: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. bootmgr is missing 开机无法进系统怎么办

    认识 bootmgr: 启动管理器.Bootmgr是Boot Manager的缩写,是在Windows Vista和Windows 7中使用的新的启动管理器,以代替Windows xp中的启动管理器- ...

  2. electron应用以管理员权限启动

    最近在用electron开发PC桌面应用,其中有个需求就是整个应用以管理员权限启动.很头痛,各种google,baidu. 最后终于解决了,可以分为三个步骤,做个总结分享. 一.如果没有manifes ...

  3. 毕业回馈-89c51之定时器/计数器(Timer/Count)

    今天分享的是89c51系列单片机的内部资源定时器/计数器,在所有的嵌入式系统中都包含这两个内部功能. 首先先了解几个定时器/计数器相关的概念: •时钟周期:时钟周期 T 是时序中最小的时间单位,具体计 ...

  4. delphi 组件安装教程详解

    学习安装组件的最好方法,就是自己编写一个组件并安装一遍,然后就真正明白其中的原理了.   本例,编写了两个BPL, dclSimpleEdit.bpl 与 SimpleLabel.bpl ,其中,dc ...

  5. POJ 2135 最小费用最大流

    题目链接 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18961   Accepted: 7326 D ...

  6. python打造一个分析网站SQL注入的脚本

    前言: 昨天晚上其实就已经写完代码.只不过向FB投稿了,打算延迟一晚上在写博客 所有才到今天早上写.好了,接下来进入正题. 思路: 1.从网站源码中爬取那些类适于:http://xxx.com/xx. ...

  7. page1

    1.1 常用的客户端技术:HTML. CSS. 客户端脚本技术 1.2 常用的服务器端技术:CGI .ASP .PHP (一种开发动态网页技术).ASP.NET(是一种建立动态web应用程序的技术,是 ...

  8. Node.js 加密

    稳定性: 2 - 不稳定; 正在讨论未来版本的 API 改进,会尽量减少重大变化.详见后文. 使用 require('crypto') 来访问这个模块. 加密模块提供了 HTTP 或 HTTPS 连接 ...

  9. POSIX 消息队列相关问题

    一.查看和删除消息队列要想看到创建的posix消息队列,需要在root用户下执行以下操作:# mkdir /dev/mqueue# mount -t mqueue none /dev/mqueue删除 ...

  10. 到底什么是集群&分布式

    对于楼主这样工作一年的菜鸟,偶尔会看到一些文章标题带有"分布式""集群"关键字,然后就懵逼了.最近对这些概念进行了一定的了解,整理了一下思路,在这里分享给各位猿 ...