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 ...
随机推荐
- 关于MATLAB处理大数据坐标文件201762
经过头脑风暴法想出了很多特征,目前经过筛选已经提交了两次数据,数据提交结果不尽如人意,但是收获很大. 接下来继续提取特征,特征数达到27时筛选出20条特征,并找出最佳搭配
- 开源框架GreenDao的操作
1.为什么需要GreenDao?Google原生API不方便 @1手动组拼SQL语句 @2需要自己写操作数据库代码 @3不能把数据库中的数据映射成对象 @4没有实现关联查询 2.GreenDao是什么 ...
- 在ubuntu14.04上安装mono4.4 + jexus + mvc6
0.准备工作 在/usr下建立一个文件夹,方便管理源码 cd /usr mkdir opensource cd opensource 安装vim(文本编辑器,不习惯用vim可以换成其他的) apt-g ...
- 实现一个简单的Log框架
实际上算不上框架,只是自己对日志框架的一点理解. 核心接口:Logger,供调用者完成不同等级的日志输出 package com.lichmama.log.service; public interf ...
- Java自学手记——多态
对象转型 学习多态前先明白一个叫对象转型的概念,如: class Animal{ void sleep(){ System.out.println("睡觉"); } } class ...
- Linux - iostat命令详解
简介 iostat可以提供更丰富的IO性能状态数据,iostat命令有两个用途: 输出CPU的统计信息 输出设备和分区的I/O统计信息 命令语法及参数说明 语法: iostat [ -c | -d ] ...
- java constructor 在构造子类时,一定会调用到父类的构造方法 “ 私有属性被继承了?”问题
” Error:Implicit super constructor Pet() is undefined. Must explicitly invoke another constructor “ ...
- RecyclerView-------之GridView模式加载更多
随着RecyclerView的出现,Listview.GridView的使用率相对有些减少,废话不多说,先看一下效果: 代码如下: 1.自定义的RecyclerView(根据自己的需要) public ...
- easyui点击搜索的时候获取不要文本框里面的值的问题
jsp的代码 <div id="tb"> <input id="AppID" placeholder="请根据申请人ID搜索&quo ...
- sqlserver 处理百万级以上的数据处理与优化
一处理百万级以上的数据提高查询速度的方法: 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应 ...