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 ...
随机推荐
- Android编译安装失败解决办法
今天用AndroidStudio开发了一个手机App玩玩,但是偶然遇到一个问题,自己手机上测试得劲的很,分享给朋友做测试,但是nie,意外出现了.... 两个人都给我说个安装失败,这个就比较尴尬了,找 ...
- SVN提交时显示:Path is not a working copy directory
说明你地址没有checkout啊 先checkout,才能add和commi. 要是在一个已有的项目出现这个错误,就是包含这个地址的文件夹没添加进去,去上一层再试一次. 总之,养成在项目根目录提交的习 ...
- ubuntu远程桌面连接命令rdesktop连接windows远程桌面详解
sudo apt-get install rdesktoprdesktop 124.42.120.174:1433 呵呵,连接成功了. -f 全屏-a 16位色默认端口是3389(linux 22 s ...
- Vue 波纹按钮组件
代码链接:https://github.com/zhangKunUserGit/vue-component 效果图: 大家可以在线运行: https://zhangkunusergit.github. ...
- VSCode 插件推荐
vscode-icons 用于项目中文件类型显示对应的图标,提高文件定位的效率. vscode-tslint 用于 TS 的规范检测 Path Intellisense 用于提示导入文件时候的路 ...
- Java内存泄漏分析系列之三:jstat命令的使用及VM Thread分析
原文地址:http://www.javatang.com 使用jstat命令 当服务器CPU100%的时候,通过定位占用资源最大的线程定位到 VM Thread: "VM Thread&qu ...
- Hibernate之SchemaExport的使用
@Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); SchemaExport ...
- Unity3D核心技术详解
在这里将多年游戏研发经验的积累写成一本书奉献给读者,目前已经开始预售,网址: http://www.broadview.com.cn/article/70 该书主要是将游戏中经常使用的技术给大家做了一 ...
- Erlang的常驻模块与功能模块
Erlang的常驻模块与功能模块Residence moduleThe module where a process has its tail-recursive loop function(s).I ...
- Team Foundation Server 2015 Update 2.1 发布日志
微软在 2016年5月5日发布了Visual Studio Team Foundation Server 2015 update 2.1. 下面我们来看看Update2.1中给我们带来了哪些新功能. ...