主要思路:将二进制转化为十进制,然后进行十进制加法,最后再将加法所得的结果转化为二进制

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)的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. 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 ...

  4. 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 ...

  5. 【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 ...

  6. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  7. [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 ...

  8. 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. ...

  9. 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 ...

随机推荐

  1. php倒计时防刷新

    <?php //php的时间是以秒算.js的时间以毫秒算 date_default_timezone_set("Asia/Hong_Kong");//地区 //配置每天的活动 ...

  2. 【Android Developers Training】 41. 向另一台设备发送文件

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. setTimeout与setInterval参数之String

    今天无意中给某网友解答了一些setTimeout的问题,发现一个有趣的东西. 以前我总认为setTimeout的第一个参数只能function,后面发现string也能执行.那问题来了,String做 ...

  4. 基于vue2.0的网易云音乐 (实时更新)

    本人在自学vue,之后想在学习过程中加以实践.由于之前有做过jquery的播放器效果,ui仿照网易云,地址 www.daiwei.org/music 于是就想做vue 的网易云播放器,网上也有类似的项 ...

  5. [图形学] 习题8.12 NLN二维线段裁剪算法实现

    Nicholl-Lee-Nicholl二维线段裁剪算法相对于Cohen-Sutherland和Liang-Barsky算法来说,在求交点之前进行了线段端点相对于几个区域的判断,可以确切的知道要求交点的 ...

  6. 算法——蛮力法之选择排序和冒泡排序c++实现

    这次实现的是蛮力法中的两个例子,选择排序法和冒泡排序法,使用的编译环境是vs2013,下面对这两个算法做一个简单介绍,然后是两个算法的c++实现代码. 选择排序法比较的范围是整个列表,每次扫描结束找出 ...

  7. 常见MD5加密解密值及免费解密网站

    常用的MD5解密 MD5(admin,16)    = 7a57a5a743  MD5(admin,16)    = 7a57a5a743894a0e  MD5(admin888,16) = 469e ...

  8. Go语言string,int,int64 ,float转换

    (1)int转string s := strconv.Itoa(i)等价于s := strconv.FormatInt(int64(i), 10) (2)int64转string i := int64 ...

  9. Python运维开发基础-概述-hello world

    Hello World 任何一门计算机语言几乎都是从hello world开始的,为了遵从这一优秀的传统习惯,我们也从打印hello world开始. linux系统进入Python环境,直接打Pyt ...

  10. Jenkins: 执行 PowerShell 命令

    Jenkins 默认是不支持执行 PowerShell 命令的,需要安装插件才能完成这样的任务.本文将介绍 Jenkins PoserShell 插件的基本用法和常见问题. 安装 PowerShell ...