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 ...
随机推荐
- linux 通过哪个命令可以查看某个服务及其端口、进程号
netstat/lsof netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况 -a 显示一个所有的有效连接信息列表(包括已建立的连接,也 ...
- 【POJ 2484】A Funny Game
Description Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 &l ...
- JAVA开发之Eclipse常用的快捷键
Eclipse是我们常用的java开发编辑器,它支持很多有用但又不太为人所知的快捷键组合.通过这些组合快捷键我们可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.甚至有一次笔者去参加一个IT ...
- Linux LSM(Linux Security Modules) Hook Technology
目录 . 引言 . Linux Security Module Framework Introduction . LSM Sourcecode Analysis . LSMs Hook Engine: ...
- Linux System Reinforcement、Intrusion Detection Based On syslog
目录 .文件系统及访问权限 . Linux Syslog . Linux日志审计 . 帐号安全管理 . 基础物理安全 . 系统编译环境安全 . 系统病毒.后门.rootkit安全 . 系统端口.服务安 ...
- C语言学习-01第一个C语言程序
一 C语言的历史 C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言. 尽管C语言提供 ...
- yield return 和 yield break
//yield return 返回类型必须为 IEnumerable.IEnumerable<T>.IEnumerator 或 IEnumerator<T>. static I ...
- Protocol Buffer技术详解(语言规范)
Protocol Buffer技术详解(语言规范) 该系列Blog的内容主体主要源自于Protocol Buffer的官方文档,而代码示例则抽取于当前正在开发的一个公司内部项目的Demo.这样做的目的 ...
- js通过alert查看对象或数组内容
var arr=new Array("Saab","Volvo","BMW"); for(i in arr ){ alert(i); //获 ...
- 防止ajax请求重发
debounce ajax请求,防止用户点击过快造成重发 按钮disabled处理,显示loading,防止用户失去耐心,重复点击 表单提交也可以同样处理.