[leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
注意:
StringBuilder.insert(int offset, char c) 表示将char放入offset这个偏移量
Character.forDigit(int digit, int radix) 返回radix进制下,该digit所代表的char
代码
class Solution {
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder(); // new a StringBuilder because it will be easy to append
char[] arrayA = a.toCharArray();// convert char to array
char[] arrayB = b.toCharArray();// convert char to array
int i = a.length()-1;
int j = b.length()-1;
int carry = 0;
while(i>=0 || j>=0 || carry >0){
int updateA = i>=0 ? arrayA[i--]-'0':0;
int updateB = j>=0 ? arrayB[j--]-'0':0;
int sum = updateA + updateB + carry;
sb.insert(0, Character.forDigit(sum%2, 10)); // I convert this sum to binary then insert to sb
carry = sum/2; // coz it is binary, carry would be sum/2
}
return sb.toString(); // convert StringBuilder to String
}
}
[leetcode]67. Add Binary 二进制加法的更多相关文章
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- Leetcode 67 Add Binary 大数加法+字符串处理
题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- 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). For example,a = "11"b = ...
- 【LeetCode每天一题】Add Binary(二进制加法)
Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...
- [Leetcode] add binary 二进制加法
Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...
- LeetCode 67 Add Binary(二进制相加)(*)
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
随机推荐
- Excel中的数据与DataSet的互换
using System;using System.Collections.Generic;using System.Data;using System.Drawing;using System.IO ...
- HttpWebRequest post 请求超时问题
在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步, 发送一个请求, 包含一个Expect:100-continue, ...
- javascript将浮点数转换成整数
Summary 临时我就想到3个方法而已.假设读者想到其它好用方法,也能够交流一下 parseInt 位运算符 Math.floor Math.ceil Description 一.parseInt ...
- java应用:向用户注册的邮箱发送邮件
实现功能 忘记密码,注册成功等向用户发送验证码信息或注册信息. 业务流程 忘记密码: 1.验证邮箱是否注册过: 2.向邮箱发送验证码: 3.验证验证码是否正确: 4.重新设置密码: 我这里着重介绍发送 ...
- LOJ 2292 「THUSC 2016」成绩单——区间DP
题目:https://loj.ac/problem/2292 直接 DP 很难做,主要是有那种 “一个区间内部有很多个别的区间” 的情况. 自己想了一番枚举 max-min 的最大限制,然后在该基础上 ...
- centos 7.x开放端口
1. 查看已打开的端口 # netstat -anp 2. 查看想开的端口是否已开 # firewall-cmd --query-port=666/tcp 若此提示 FirewallD is not ...
- php执行系统命令的四个函数shell_exec, exec, passthru, system分别的使用场景
shell_exec() 通过 shell 环境执行命令,并且将完整的输出以字符串的方式返回.也就是说, PHP先运行一个shell环境, 然后让shell进程运行你的命令, 并且把所有输出已字符串形 ...
- git 一个分支完全覆盖另一个分支
1,git push origin develop:master -f就可以把本地的develop分支强制(-f)推送到远程master 2,git checkout master // 切换到旧的分 ...
- Code::Blocks 导入Makefile工程
1)“File -> New -> Project”,选择“Empty Project”并创建. 2)选中 Project,右键,选择“Add files”,将 c/c++ 和 head ...
- redis高可用(主从复制)
熟练掌握redis需要从 reids如何操作5种基本数据类型,redis如何集群,reids主从复制,redis哨兵机制redis持久化 reids主从复制 的作用可以:实现数据备份,读写分离,集群, ...