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. 重写UIPageControl实现自定义按钮

    有时候UIPageControl需要用到白色的背景, 那么会导致上面的点按钮看不见或不清楚,我们可以通过继承该类重写函数来更换点按钮的图片现实.实现思路如下.新建类继承UIPageControl :  ...

  2. 使用FMDB事务批量更新数据库

    今天比较闲看到大家在群里讨论关于数据库操作的问题,其中谈到了“事务”这个词,坦白讲虽然作为计算机专业的学生,在上学的时候确实知道存储过程.触发器.事务等等这些名词的概念,但是由于毕业后从事的不是服务器 ...

  3. spring全注解项目

    项目结构如下: spring配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...

  4. POJ2823Sliding Window

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 49919   Accepted: 14391 ...

  5. idea修改运行内存

    安装目录下的bin 找到idea.exe.vmoptions 最大的修改下-Xmx1024m 找到idea64.exe.vmoptions 最大的修改下-Xmx1024m

  6. vector与set区别(基础知识)

    首先,vector是序列式容器而set是关联式容器.set包含0个或多个不重复不排序的元素.也就是说set能够保证它里面所有的元素都是不重复的.另外对set容器进行插入时可以指定插入位置或者不指定插入 ...

  7. 初学JDBC,防SQL注入简单示例

    在JDBC简单封装的基础上实现 public class UserDao{ public static void testGetUser(String userName) throws Excepti ...

  8. error MSB4019: 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets”

    error MSB4019: 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\ ...

  9. include(thinkphp常用内置标签)

    变量输出使用普通标签就足够了,但是要完成其他的控制.循环和判断功能,就需要借助模板引擎的标签库功能了,系统内置标签库的所有标签无需引入标签库即可直接使用. XML标签有两种,包括闭合标签和开放标签,一 ...

  10. JNI的某些数组和字符串类型转换

    JNICC++C#Windows jbytearray转c++byte数组 jbyte * arrayBody = env->GetByteArrayElements(data,0); jsiz ...