当前位置:网站首页>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;
}
}
边栏推荐
- UE source code reading [1]--- starting with problems delayed rendering in UE
- 无密码身份验证如何保障用户隐私安全?
- [buuctf.reverse] 152-154
- Assembly language
- 金融壹账通香港上市:市值63亿港元 叶望春称守正笃实,久久为功
- openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
- matlab学习2022.7.4
- Why do I support bat to dismantle "AI research institute"
- R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c
- How to introduce devsecops into enterprises?
猜你喜欢
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
upload (1-6)
The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
Introduction, installation, introduction and detailed introduction to postman!
神经网络物联网未来发展趋势怎么样
为什么我认识的机械工程师都抱怨工资低?
无密码身份验证如何保障用户隐私安全?
TDengine 社区问题双周精选 | 第三期
Tiflash compiler oriented automatic vectorization acceleration
SAS接口有什么优势特点
随机推荐
POI set the data format of the column (valid)
TiCDC 6.0原理之Sorter演进
2022 driller (drilling) examination question bank and simulation examination
04_ Use of solrj7.3 of solr7.3
R语言ggplot2可视化:可视化折线图、使用theme函数中的legend.position参数自定义图例的位置
广发期货排名多少?网上办理广发期货开户安全可靠吗?
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
C - Divisors of the Divisors of An Integer Gym - 102040C
不相交集
Xampp configuring multiple items
无密码身份验证如何保障用户隐私安全?
WebRTC的学习(二)
分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
一网打尽异步神器CompletableFuture
Introduction, installation, introduction and detailed introduction to postman!
The simplest way to open more functions without certificates
Routing in laravel framework
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
鏈錶(簡單)
动态规划