leetcode — add-binary
/**
* Source : https://oj.leetcode.com/problems/add-binary/
*
*
* Given two binary strings, return their sum (also a binary string).
*
* For example,
* a = "11"
* b = "1"
* Return "100".
*/
public class AddBinary {
public String add (String binary1, String binary2) {
StringBuilder result = new StringBuilder();
int length = binary1.length() > binary2.length() ? binary2.length() : binary1.length();
int carry = 0;
for (int i = length - 1; i > -1; i--) {
int sum = Integer.parseInt(binary1.substring(i, i+1)) + Integer.parseInt(binary2.substring(i, i+1)) + carry;
carry = sum / 2;
result.insert(0, sum % 2);
}
String longerStr = "";
if (binary1.length() > binary2.length()) {
longerStr = binary1;
} else {
longerStr = binary2;
}
for (int i = longerStr.length() - 1; i >= length; i--) {
int sum = Integer.parseInt(longerStr.substring(i, i+1)) + carry;
carry = sum / 2;
result.insert(0, sum % 2);
}
if (carry > 0) {
result.insert(0, carry);
}
return result.toString();
}
public static void main(String[] args) {
AddBinary addBinary = new AddBinary();
System.out.println(addBinary.add("", ""));
System.out.println(addBinary.add("1", ""));
System.out.println(addBinary.add("1", "1"));
System.out.println(addBinary.add("10", "11"));
}
}
leetcode — add-binary的更多相关文章
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [leetcode]Add Binary @ Python
原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...
- Leetcode Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode——Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- LeetCode Add Binary |My Solution
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- [Leetcode] add binary 二进制加法
Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...
- LeetCode Add Binary 两个二进制数相加
class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
随机推荐
- 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(三)数字拆分
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...
- P1081 开车旅行(Not Finish)
https://www.luogu.org/problemnew/show/P1081
- RAM和ROM
RAM:随机存取存储器(英语:Random Access Memory,缩写:RAM),也叫主存,是与CPU直接交换数据的内部存储器.[1]它可以随时读写(刷新时除外,见下文),而且速度很快,通常作为 ...
- Photoshop功能组成色彩快捷键
功能 专业测评 Photoshop的专长在于图像处理,而不是图形创作.图像处理是对已有的位图图像进行编辑加工处理以及运用一些特殊效果,其重点在于对图像的处理加工:图形创作软件是按照自己的构思创意,使用 ...
- submit与execute区别
1.可以接受的任务类型 submit: execute: 可以看出: execute只能接受Runnable类型的任务 submit不管是Runnable还是Callable类型的任务都可以接受,但是 ...
- Atomic in Redis
Since Redis is single-threaded, everything is atomic.
- CSS伪类的理解
因为之前一直对css伪类没有过多的了解,在网上看到一段css代码,不能理解 a:hover span.title{ color:red; ......... } 现通过查询css手册,其实css伪类只 ...
- 项目小程序笔记-登录界面+FPGA管脚分配文件生成
声明:只是为了记录我遇到的一些问题,其中有我理解错的望勿参考. (1)qt designer设计好窗口 主窗口: 登录窗口: 关于qt designer的使用,大可以百度,很简单的,要注意的是部件的参 ...
- [转] Java 的泛型擦除和运行时泛型信息获取
原文链接 https://my.oschina.net/lifany/blog/875769 前言 现在很多程序员都会在简历中写上精通 Java.但究竟怎样才算是精通 Java 呢?我觉得不仅要熟练掌 ...
- linux 解决乱码问题
乱码分两种情况: 1.终端(纯 shell 界面)的乱码 vi /etc/profile export LC_ALL="zh_CN.GB18030:zh_CN.GB2312:zh_CN.G ...