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 ...
随机推荐
- 用 Google 挖掘赚钱思路
为程序员,如果学了一堆技术却没有用武之地,实在可惜,如何把自己积累的技术利用起来?通俗一点,程序员有哪些赚钱的门路? 比较常见的一种方式是接私活,不过私活的复杂度不一,沟通成本会非常高,另一方面,私活 ...
- Git恢复指定文件
Git恢复指定文件 修改这个文件的commit有哪些? git log -- <文件路径> 猜测需要还原的commit. 这个文件作了哪些更改? git diff <需要还原的com ...
- 也谈TDD,以及三层架构、设计模式、ORM……:没有免费的午餐
想在园子里写点东西已经很久了,但一直没有落笔,忙着做 一起帮 的开发直播,还有些软文做推广,还要做奶爸带孩子,还要……好吧,我承认,真正的原因是: 太特么的难写了! 但再难写也要写啊,要等到“能写好了 ...
- Configure: error: freetype.h not found. 的解决办法
出现 Configure: error: freetype.h not found. 的解决办法 CentOS yum install freetype-devel Debian apt-get in ...
- Python之编写登陆接口
1.输入用户名密码: 2.认证成功后显示欢迎信息: 3.错误三次后,账号被锁定. 账号文件:user.txt 锁定文件:locked.txt 流程图如下: # -*- coding:utf-8 -*- ...
- Wireshark网络端点和会话
如果想让网络进行正常通信,你必须至少拥有两台设备进行数据流交互.端点(endpoint)就是指网络上能够发送和接受数据的一台设备.举例来说,在TCP/IP的通信中就有两个断电:接收和发送数据系统的IP ...
- 反编译Unity3D手机游戏
[旧博客转移 - 2015年11月17日 10:08] 现在大部分U3D手游都没有进行加密处理,要反编译其实很简单 APK是安卓的安装包,安卓是基于Linux的,Linux的安装包一般都是zip,所以 ...
- WAMPServer多站点配置方法
WAMPServer多站点配置方法:1.在C:\wamp\www 新建文件夹test01,在里面新建index.php,内容为 "Hello Test01". 2.C:\wamp\ ...
- P3390 【模板】矩阵快速幂
题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格式: 输出A^k ...
- 谷歌是如何做代码审查的 | 外刊IT评论 - Google Chrome
谷歌是如何做代码审查的 本文的作者 Mark CC 在上一篇文章中提到过,我已经不在Google工作了.我还没有想清楚应该去哪里-有两三个非常好的工作机会摆在我面前.因为在这段做决 ...