Add Binary Leetcode java
题目:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
题解:
二进制加法都是从最低位(从右加到左)。所以对两个字符串要从最后一位开始加,如果遇见长度不一的情况,就把短的字符串高位补0.
每轮计算要加上进位,最后跳出循环后要坚持进位是否为1,以便更新结果。
代码如下(from discussion):
1 public String addBinary(String a, String b) {
2 int m = a.length();
3 int n = b.length();
4 int carry = 0;
5 String res = "";
6 // the final length of the result depends on the bigger length between a and b,
7 // (also the value of carry, if carry = 1, add "1" at the head of result, otherwise)
8 int maxLen = Math.max(m, n);
9 for (int i = 0; i < maxLen; i++) {
// start from last char of a and b
// notice that left side is int and right side is char
// so we need to minus the decimal value of '0'
int p=0,q=0;
if(i<m)
p = a.charAt(m-1-i) - '0';
else
p = 0;
if(i<n)
q = b.charAt(n-1-i)-'0';
else
q = 0;
int tmp = p + q + carry;
carry = tmp / 2;
res += tmp % 2;
}
return (carry == 0) ? res : "1" + res;
}
Add Binary Leetcode java的更多相关文章
- LeetCode算法题-Add Binary(Java实现)
这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...
- Add Binary <leetcode>
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
随机推荐
- 网络设备Web登录检测工具device-phamer
网络设备Web登录检测工具device-phamer 为了便于管理和维护,大部分网络都提供了Web管理接口.Kali Linux提供了一款专用检测工具device-phamer.该工具可以批量检测 ...
- 洛谷 P1135 奇怪的电梯
题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N).电梯只有四个按钮:开 ...
- CentOS 7 yum 安装subversion
yum list installed | grep subversionyum install subversioncd /var/wwwsvn -hsvn co svn://121.196.226. ...
- Dynamic-Link Library Redirection
Dynamic-Link Library Redirection Applications can depend on a specific version of a shared DLL and s ...
- Go 语言中的 new() 和 make()的区别
本文是看了文章之后的心得. 在此感谢. 概述 Go 语言中的 new 和 make 一直是新手比较容易混淆的东西,咋一看很相似.不过解释两者之间的不同也非常容易. 他们所做的事情,和应用的类型也不相同 ...
- SQL 中的语法顺序与执行顺序(转)
很多程序员都很抵触SQL.其实SQL是一整为数不多的声明性语言,只是它的运行方式完全不同于我们所熟知的命令行语言.面向对象的程序语言.甚至是函数语言. 今天大家共同学习下SQL的语法顺序与执行顺序.( ...
- smartsvn学习(一)Xcode下svn客户端使用指南
http://smartsvn.com/features 说明 场景 执行步骤 创建新项目 一,二,三,四 下载项目 一,二,四 代码提交 五 代码更新 六 一,打开SCM 在xcode中,点击菜单: ...
- 使PropertyGrid控件的属性值可以显示多行的方法
第一步:重写UITypeEditor的GetEditStyle方法: 第二部:重写UITypeEditor的EditValue方法: 具体实现如下: using System; using Syste ...
- 架构:The Onion Architecture : part 2(洋葱架构:第二篇)(转载)
原位地址:http://jeffreypalermo.com/blog/the-onion-architecture-part-2/. In part 1, I introduced an archi ...
- python测试开发django-37.外键(ForeignKey)查询
前言 前面在admin后台页面通过设置外键,可以选择下拉框的选项,本篇主要讲解关于外键(ForeignKey)的查询 models设计 在上一篇的基础上新增一个BankName表,Card表通过外键关 ...