Java for LeetCode 067 Add Binary
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的更多相关文章
- 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 ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- 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 ...
- 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 ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
- 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 ...
- 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 ...
- LeetCode 67. Add Binary (二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
随机推荐
- python学习笔记4(对象/引用;多范式; 上下文管理器)
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 21. 动态类型:对象/引用 对象和引用: 对象是储存在内存中的实体,对象名只是指向这一对象的引用(refere ...
- 德州扑克AI WEB版
继续之前的德州扑克话题,上次的DOS界面确实没法看,我女朋友说这是什么鬼.哈哈,估计只有自己能玩了 这两天重构了一下界面,基于web服务器和浏览器来交互. 服务器和客户端之间用websocket通信, ...
- vbox下安装arch
http://tieba.baidu.com/p/2663744019 安装介质: archlinux-2013.10.01-dual.iso 准备存储设备: 警告: 磁盘分区有时会毁掉原分区内的数据 ...
- python TypeError: 'str' object does not support item assignment”
想替换string里的空格,遍历替换提示如题错误,查询得知string类型不可更改 import string s = "2013/2/12" b = s.replace('/', ...
- POJ2309BST(树状数组)
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9182 Accepted: 5613 Description C ...
- Spring学习8-用MyEclipse搭建SSH框架 Struts Spring Hibernate
1.new一个web project. 2.右键项目,为项目添加Struts支持. 点击Finish.src目录下多了struts.xml配置文件. 3.使用MyEclipse DataBase Ex ...
- Linux目录结构及常用命令(转载)
一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/bin,或/usr/sbin目录下吗?例如,less命令位于/usr/bin目录下.为什么没在/bin中,或 ...
- iOS快速单例宏
// 单例 #define DECLARE_SHARED_INSTANCE(className) \ + (className *)sharedInstance; #define IMPLEMENT_ ...
- Windows 2008远程多用户登录的配置方法(转载)
在使用Windows2008远程登录功能时,如果需要进行多用户登录,可以采用以下配置方法: 首先要启用远程桌面这一功能:右击“我的电脑”→属性→远程配置→远程桌面,就可以配置相应的远程桌面功能了.下 ...
- 配置 Apache+php多端口多站点(转载)
配置httpd.conf监听多个端口 #增加监听端口 等以下内容都设置以后,可以通过 netstat -n -a查看端口是否开启 开启虚拟站点 # Virtual hosts#Include conf ...