Leetcode_67_Add Binary
本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- 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 ...
- [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 ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- CAP原理和BASE思想和ACID模型
问题的解读 对于上面三个例子,相信大家一定看出来了,我们的终端用户在使用不同的计算机产品时对于数据一致性的需求是不一样的: 1.有些系统,既要快速地响应用户,同时还要保证系统的数据对于任意客户端都是真 ...
- tomcat7+jdk的keytool生成证书 配置https
目前只会使用jdk的keytool来生成证书.本文仅介绍这种方法. 1Windows下: 1.1 生成keystore文件及导出证书 打开控制台: 运行: %JAVA_HOME%\bin\keytoo ...
- 81. Search in Rotated Sorted Array II (中等)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- SignalR Progress
Server public class ServerHub : Hub { public async Task<string> ALongTimeTask() { var p = new ...
- IP地址段遍历
#region 搜索ftp服务器地址 /// <summary> /// 搜索ftp服务器 /// </summary> public void SearchFtpServer ...
- ubuntu15.10 安装 virtualbox5.0
首先安装依赖包.ubuntu15.01安装的时候会出现这个错误: virtualbox-); however: Package libvpx1 is not installed 而且sudo apt- ...
- 代码之间-论文修改助手v1.0版本发布
论文查重,是每个毕业生都要面临的一个令人头疼的问题,如果写论文不认真,很可能导致查重红一大片. 之前有帮助一些朋友修改论文降低重复率,做了一些工作后发现,国内的查重机构,如知网.维普等,大多数是基于关 ...
- Java内存泄漏分析系列之四:jstack生成的Thread Dump日志线程状态
原文地址:http://www.javatang.com Thread Dump日志的线程信息 以下面的日志为例: "resin-22129" daemon prio=10 tid ...
- Android超精准计步器开发-Dylan计步
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/52868803 本文出自[DylanAndroid的博客] Android超精准 ...
- Bootstrap3 排版-列表
无序列表 排列顺序无关紧要的一列元素. <ul> <li>...</li> </ul> 有序列表 顺序至关重要的一组元素. <ol> < ...