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

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. Linux系统vi模式下显示行号

    在命令模式下输入:set nu或者:set number都可以为vi设置行号,如果要取消的话,则输入:set nonu行号的设置是vi的环境设置,不会影响文本的内容.

  2. [leetcode-495-Teemo Attacking]

    In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  3. 【LeetCode】237. Delete Node in a Linked List

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  4. 【Android Developers Training】 101. 显示快速联系人挂件(Quick Contact Badge)

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

  5. 跳跳棋(9018_1563)(BZOJ_2144)

    题目: Hzwer的跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 某一天,黄金大神和cjy用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.他们要 ...

  6. 【Unity与23种设计模式】状态模式(State)

    定义: "让一个对象的行为随着内部状态的改变而变化,而该对象也像是换了类一样" 应用场景: 角色AI:控制角色在不同状态下的AI行为 服务器连接状态:开始连线.连线中.断线等状态 ...

  7. visual studio问题集合

    1.当前断点不会命中,还没有为该文档加载任何符号 打开visual 2010 工具->选项->调试->符号: 一.手动加载 点击 加载所有符号 即可.二.自动加载 点击"指 ...

  8. hdu 2612 多终点BFS

    Find a way Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at ...

  9. IScroll那些事——内容不足时下拉刷新

    之前项目中的列表是采用的IScroll,但是在使用IScroll有一个问题就是:当内容不足全屏的时候,是木有办法往下拉的,这样就达不到刷新的目的了.[这是本人工作中遇到的,具体例子具体分析,这里只作一 ...

  10. IDEA使用01 创建java项目、创建web项目

    注意:本教程使用的开发环境是:(专业版) 1 创建javaSE项目 1.1 file -> new -> project 注意:如果是第一次使用,就需要配置 project SDK  , ...