[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 ...
随机推荐
- Java_初入IO流_字符流_Write-Read_小笔记
package IO; import java.io.FileWriter; import java.io.IOException; class File_Writer { public static ...
- PHP的 preg_match_all
语法:int preg_match_all ( string pattern, string subject, array &matches [, int flags] ) 这个函数的返回值是 ...
- because there was insufficient free space available after evicting expired cache entries
Tomcat运行的时候哗哗哗的报警告 版本是Tomcat 8.5.15 告警信息关键字如下 because there was insufficient free space available af ...
- Java面向对象 第4节 类的多态性
一.多态的概念 在面向对象语言中,多态是指在一棵继承树中的类中可以有多个同名但不同方法体及不同形参的方法.通常有两种途径实现多态:方法的重载和覆盖. 多态性允许以统一的风格处理已存在的变量及相关的类. ...
- Spring IOC(四)总结升华篇
本系列目录 Spring IOC(一)概览 Spring IOC(二)容器初始化 Spring IOC(三)依赖注入 Spring IOC(四)总结升华 =============正文分割线===== ...
- fullCalendar使用经验总结
fullCalendar日历控件官方网址: https://fullcalendar.io/ 1.需要引入的文件 <link href="~/assets/fullcalendar-3 ...
- linux nodejs
scp node-v8.15.0-linux-x64.tar.gz root@10.88.248.231:/lngoa tar -zxvf node-v8.15.0-linux-x64.tar.gz ...
- Linux下部署 apache+jdk+tomcat
1.输入下面的命令安装apache2: ubuntu@VM-164-86-ubuntu:sudo apt-get install apache2 当提示“您希望继续执行吗?”时 输入 Y 然后等待安 ...
- 编译CDH的spark1.5.2
手动安装mvn大于3.3.3版本 下载解压,修改~/.bash_rc export MAVEN_HOME=/usr/local/apache-maven-3.3.9 export PATH=$MAVE ...
- C#将对象序列化成JSON字符串
C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...