当前位置:网站首页>LeetCode_ 67 (binary sum)
LeetCode_ 67 (binary sum)
2022-07-05 14:13:00 【***】
Real warriors , Dare to face blasting algorithm
Title Description :
Here are two binary strings , Returns the sum of them ( In binary ).
Input is Non empty String and contains only numbers 1 and 0.
Example 1:
Input : a = “11”, b = “1”
Output : “100”
Example 2:
Input : a = “1010”, b = “1011”
Output : “10101”
Tips :
Each string consists only of characters ‘0’ or ‘1’ form .
1 <= a.length, b.length <= 10^4
If the string is not “0” , No leading zeros .
class Solution {
public String addBinary(String a, String b) {
String res="";
StringBuilder sb=new StringBuilder();
StringBuilder sbb=new StringBuilder();
String s;
if(a.length()<b.length()){
s=a;
a=b;
b=s;
}
else s=b;
int len=a.length()-b.length();
if(len<0)len=-len;
for(int k=0;k<len;k++){
sbb.append("0");
}
s=sbb.toString();
b=s+b;
List<Character> list=new ArrayList<>();
int[] flag=new int[a.length()];
char cur='0';
int i=a.length()-1,j=b.length()-1;
while(i>=0&&j>=0){
if(i==a.length()-1){
// Determine the last bit operation
if(a.charAt(i)==b.charAt(j)){
// If equal
list.add('0'); // Direct addition 0
if(a.charAt(i)=='1')flag[i]=1; // If it is one , Means to carry
else flag[i]=0; // Otherwise, it will not carry
i--;j--;
// Enter the next calculation
}else if(a.charAt(i)!=b.charAt(j)){
// If you don't want to wait
list.add('1'); // For one
flag[i]=0; // No carry
i--;j--;
}
}
else{
if(a.charAt(i)!=b.charAt(j)){
// If it's not equal
cur='1'; // For one
flag[i]=0; // No carry
}
else{
// equal
cur='0'; // zero
if(a.charAt(i)=='0'){
// If two numbers are zero
flag[i]=0; // Then it doesn't carry
}else {
// Otherwise, it will be one
flag[i]=1; // carry
}
}
if(flag[i+1]==1){
// If the previous carry , Then change
if(cur=='0')cur='1';
else{
cur='0';
flag[i]=1;
}
}
list.add(cur);
i--;
j--;
}
}
if(flag[0]==1)list.add('1');
for(int k=list.size()-1;k>=0 ;k--){
sb.append(list.get(k));
}
res=sb.toString();
return res;
}
}
边栏推荐
- 01. Solr7.3.1 deployment and configuration of jetty under win10 platform
- Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
- 神经网络物联网未来现状和趋势及看法
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
- Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
- TiFlash 面向编译器的自动向量化加速
- 04_solr7.3之solrJ7.3的使用
- 分享 12 个最常用的正则表达式,能解决你大部分问题
- VC development of non MFC program memory leak tracking code
- 国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
猜你喜欢

LeetCode_2(两数相加)

Kunlun Taike rushes to the scientific innovation board: the annual revenue is 130million, and it plans to raise 500million. CETC Taiji holds 40% of the shares

SAS接口有什么优势特点

Introduction, installation, introduction and detailed introduction to postman!

The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million

神经网络物联网未来发展趋势怎么样

金融壹账通香港上市:市值63亿港元 叶望春称守正笃实,久久为功

Tiflash compiler oriented automatic vectorization acceleration

Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module

牛客网:拦截导弹
随机推荐
VC开发非MFC程序内存泄漏跟踪代码
Linked list (simple)
分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
神经网络物联网未来发展趋势怎么样
2022 machine fitter (Advanced) test question simulation test question bank simulation test platform operation
区间 - 左闭右开
R语言ggplot2可视化:使用ggplot2可视化散点图、使用labs参数自定义X轴的轴标签文本(customize X axis labels)
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
Why do mechanical engineers I know complain about low wages?
Tiflash compiler oriented automatic vectorization acceleration
Some ideas about Apache mesos
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
常见问题之PHP——Fatal error: Allowed memory size of 314572800 bytes exhausted...
Recommendation number | what are interesting people looking at?
故障分析 | MySQL 耗尽主机内存一例分析
Show strength. In this way, the mobile phone will not be difficult to move forward
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
[buuctf.reverse] 152-154
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收