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

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. python list有关remove的问题

    在python 中进行一次简单的列表循环,当用到remove时出现了一个很有趣的现象, 代码如下: a=range(30) for i in a : if i%4!=0: a.remove(i) 这段 ...

  2. sublime 设置字体

    通过菜单Preferences/Settings - User,添加下面这行配置就可以修改字体: "font_face": "Courier New", &qu ...

  3. 不支持placeholder浏览器下对placeholder进行处理

    if(document.createElement('input').placeholder !== '') { $('[placeholder]').focus(function() { var i ...

  4. linux发行版和内核的关系

    转自:http://m.blog.csdn.net/article/details?id=50595230 Linux内核是计算机操作系统的核心.一个完整的 Linux发行版包括了内核与一些其他与文件 ...

  5. [leetcode-474-Ones and Zeroes]

    In the computer world, use restricted resource you have to generate maximum benefit is what we alway ...

  6. 如何为一个eclipse安装android环境

    据说android已经不再支持android adt-bundle的开发环境了,所以如果继续使用的话,会不再更新 使用eclipse来安装android环境或者使用android studio 但是以 ...

  7. CentOS6.4虚拟机设置固定IP、安装JDK、Tomcat、Redis并部署web项目

    一.CentOS设置固定IP 1.直接修改配置文件的方式,原文地址:http://www.cnblogs.com/zhja/p/3964159.html (1)首先获取你的GATEWAY 方便后面在c ...

  8. java基础系列--Calendar类

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7136575.html 1.Calendar概述 Java官方推荐使用Calendar来替换 ...

  9. (转载)Java内部类的使用小结

    原文摘自:http://android.blog.51cto.com/268543/384844/   内部类是指在一个外部类的内部再定义一个类.类名不需要和文件夹相同. *内部类可以是静态stati ...

  10. vijos1046题解

    题目: 学校里面有N个景点.两个景点之间可能直接有道路相连,用Dist[I,J]表示它的长度:否则它们之间没有直接的道路相连.这里所说的道路是没有规定方向的,也就是说,如果从I到J有直接的道路,那么从 ...