Given two binary string, return their sum (also a binary string)
主要思路:将二进制转化为十进制,然后进行十进制加法,最后再将加法所得的结果转化为二进制
public class BinarySum2 {
public static void main(String[] args) {
String a = "111";
String b = "101";
int sumInt = toInt(a) + toInt(b);
StringBuffer sb = toBinary(sumInt);
for(int i = sb.length()-1; i >= 0; --i){
System.out.print(sb.charAt(i));
}
}
public static int toInt(String a) {
int sum = 0;
for(int i = 0; i < a.length(); ++i){
int d = Integer.parseInt(a.substring(i,i+1));
sum += d*(Math.pow(2, a.length()-i-1));
}
return sum;
}
public static StringBuffer toBinary(int sumInt) {
StringBuffer sb = new StringBuffer();
while(sumInt / 2 != 0){
int remain = sumInt % 2;
sb.append(remain);
sumInt = sumInt/2;
}
sb.append(1);
return sb;
}
}
Given two binary string, return their sum (also a binary string)的更多相关文章
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 26. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- [lintcode] Binary Tree Maximum Path Sum II
Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree a ...
- LintCode Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- Linux: bash script
content [toc] bash scripts equivalent bash command to rename a bash variable/command alias fire='fir ...
- 搭建带热更新功能的本地开发node server
引言 使用webpack有一段时间了,对其中的热更新的大概理解是:对某个模块做了修改,页面只做局部更新而不需要刷新整个页面来进行更新.这样就能节省因为整个页面刷新所产生开销的时间,模块热加载加快了开发 ...
- Android 4.0以后正确的获取外部sd卡存储目录
刚解决这个棘手的问题 找了很久,随笔记下. 网上搜索 android 获取外部sd卡存储目录 普遍都是: 1) Environment.getExternalStorageDirectory() 这个 ...
- java当中成员变量和局部变量的区别
1:成员变量定义在类中,整个类中都可以访问.2:局部变量定义在函数,语句,局部代码块中,只在所属的区域有效.3:成员变量存在于堆内存的对象中.4:局部变量存在于栈内存的方法中.5:成员变量随着对象的创 ...
- svn怎么切换用户
在使用svn客户端经常会保存用户及密码,方便下次直接登录,有时需要使用别的账号登录,这时该怎么切换呢? 工具/原料 苹果6 小米4 方法/步骤 在使用svn更新或提交数据时需要输入用户名和密码,在 ...
- 修改phpstorm的字体样式和大小
默认的字体实在太小,也太丑,必须修改下.就是强迫症,没错.下面截图配文字说明下 方法/步骤 首先进入设置,不解释 先设置软件界面上的字体.进入设置之后,选择(外观)Appearance.之后软 ...
- JAVAEE学习笔记
以后创建常量有三个名字:Constant SystemParas StaticValue 上限或者下限命名 max_ min_ 包含的范围命名 first l ...
- DB数据导出工具分享
一个根据数据库链接字符串,sql语句 即可将结果集导出到Excel的工具 分享,支持sqlserver,mysql. 前因 一个月前朋友找到我,让我帮忙做一个根据sql导出查询结果到Excel的工具( ...
- MySQL基础语法命令
1. 建表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 通用语法: CREATE TABLE table_name (column_name column_type); 实例: ...
- java基础(9) - 泛型解析
泛型 定义简单的泛型类 泛型方法 /** * 1.定义一个泛型类 * 在类名后添加类的泛型参数 <T> * 泛型类里面的所有T会根据创建泛型类时传入的参数确定类型 * 2.定义泛型方法 * ...