Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

解题思路:

JAVA实现如下:

static public String addBinary(String a, String b) {
if (a.length() < b.length()) {
String temp = a;
a = b;
b = temp;
}
boolean carry = false;
StringBuilder sb = new StringBuilder(a);
for (int i = 0; i < b.length(); i++) {
if (b.charAt(b.length() - 1 - i) == '0') {
if (sb.charAt(a.length() - 1 - i) == '0' && carry) {
sb.replace(a.length() - 1 - i, a.length() - i, "1");
carry = false;
} else if (sb.charAt(a.length() - 1 - i) == '1' && carry)
sb.replace(a.length() - 1 - i, a.length() - i, "0");
} else {
if (sb.charAt(a.length() - 1 - i) == '0' && !carry)
sb.replace(a.length() - 1 - i, a.length() - i, "1");
else if (sb.charAt(a.length() - 1 - i) == '1' && !carry) {
sb.replace(a.length() - 1 - i, a.length() - i, "0");
carry = true;
}
}
}
if (!carry)
return sb.toString();
for (int i = a.length() - b.length() - 1; i >= 0; i--)
if (sb.charAt(i) == '0') {
sb.replace(i, i + 1, "1");
return sb.toString();
} else
sb.replace(i, i + 1, "0");
sb.insert(0, '1');
return sb.toString();
}

Java for LeetCode 067 Add Binary的更多相关文章

  1. Java for LeetCode 095 Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  2. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  3. Java for LeetCode 098 Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  4. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  5. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  6. Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

  7. Java for LeetCode 099 Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  8. LeetCode 67. Add Binary (二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  9. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

随机推荐

  1. 利用getHibernateTemplate实现简单的操作

    package org.tarena.dao; import java.sql.SQLException; import java.util.List; import javax.annotation ...

  2. 旅行(Dijkstra)问题

    问题:输入: 输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个:   接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车 ...

  3. POJ2286 The Rotation Game

    Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see ...

  4. 友盟iOS推送配置(从真机调试到推送)

    下面我来讲解一下友盟iOS的推送配置,其实友盟只是一个示例,换做其余的第三方推送服务也会适用,只是第三方的后面服务变了而已. iOS推送(包括真机调试)所需要的步骤和文件如下: 备注:这里我将省略掉一 ...

  5. 导师互选系统 Alpha版冲刺总结

    导师互选系统 Alpha版冲刺总结 一.设想和目标 我们的软件什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要是要实现导师和学生双向互选的功能.功能定义清晰明确,在软 ...

  6. 深入浅出MySQL双向复制技术

    设置MySQL数据同步(单向&双向)由于公司的业务需求,需要网通和电信的数据同步,就做了个MySQL的双向同步,记下过程,以后用得到再翻出来,也贴出来供大家参考. 一.准备服务器 由于MySQ ...

  7. thinkphp空控制器的处理

    <?php namespace Admin\Controller; use Think\Controller; class DengLuController extends Controller ...

  8. U盘中的autorun.inf

    怎么删除u盘里的autorun.inf 如果U盘中毒,刚插进机子时按住SHIFT五秒,这样就可以跳过预读,这样防止了预读时把病毒感染到机子上,在U盘盘符上点右键,看看有没有“Auto”选项: 1.如果 ...

  9. FCK编辑器漏洞总结

    1.查看编辑器版本FCKeditor/_whatsnew.html————————————————————————————————————————————————————————————— 2. Ve ...

  10. shell kill掉含同一字符的关键字的进程

    如何kill掉进程名包含某个字符串的一批进程:kill -9 $(ps -ef|grep 进程名关键字|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ') 观 ...