当前位置:网站首页>LeetCode_67(二进制求和)
LeetCode_67(二进制求和)
2022-07-05 13:51:00 【***】
真正的勇士,敢于直面爆破算法
题目描述:
给你两个二进制字符串,返回它们的和(用二进制表示)。
输入为 非空 字符串且只包含数字 1 和 0。
示例 1:
输入: a = “11”, b = “1”
输出: “100”
示例 2:
输入: a = “1010”, b = “1011”
输出: “10101”
提示:
每个字符串仅由字符 ‘0’ 或 ‘1’ 组成。
1 <= a.length, b.length <= 10^4
字符串如果不是 “0” ,就都不含前导零。
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){
//判断最后一位运算
if(a.charAt(i)==b.charAt(j)){
//如果相等
list.add('0'); //直接加0
if(a.charAt(i)=='1')flag[i]=1; //如果为一,则表示要进位
else flag[i]=0; //否则不进位
i--;j--;
//进入下一位计算
}else if(a.charAt(i)!=b.charAt(j)){
//如果不想等
list.add('1'); //为一
flag[i]=0; //不进位
i--;j--;
}
}
else{
if(a.charAt(i)!=b.charAt(j)){
//如果不相等
cur='1'; //为一
flag[i]=0; //不进位
}
else{
//相等
cur='0'; //为零
if(a.charAt(i)=='0'){
//若两数为零
flag[i]=0; //则不进位
}else {
//否则就为一
flag[i]=1; //进位
}
}
if(flag[i+1]==1){
//如果前一位进位,则改变
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;
}
}
边栏推荐
- 不知道这4种缓存模式,敢说懂缓存吗?
- Network security - Novice introduction
- Personal component - message prompt
- 【华南理工大学】考研初试复试资料分享
- When there are too many input boxes such as input transmitted at one time in the form, the post data is intercepted
- What are the private addresses
- "Baidu Cup" CTF competition in September, web:upload
- Apicloud studio3 WiFi real machine synchronization and WiFi real machine preview instructions
- 【 script secret pour l'utilisation de MySQL 】 un jeu en ligne sur l'heure et le type de date de MySQL et les fonctions d'exploitation connexes (3)
- 【云资源】云资源安全管理用什么软件好?为什么?
猜你喜欢
嵌入式软件架构设计-消息交互
Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
Idea set method annotation and class annotation
Huawei push service content, read notes
[js] basic syntax - for loop
[server data recovery] a case of RAID5 data recovery stored in a brand of server
Laravel框架运行报错:No application encryption key has been specified
Attack and defense world web WP
随机推荐
Mmseg - Mutli view time series data inspection and visualization
stm32逆向入门
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
Idea设置方法注释和类注释
asp.net 读取txt文件
PHP basic syntax
French scholars: the explicability of counter attack under optimal transmission theory
【 script secret pour l'utilisation de MySQL 】 un jeu en ligne sur l'heure et le type de date de MySQL et les fonctions d'exploitation connexes (3)
如何把大的‘tar‘存档文件分割成特定大小的多个文件
The development of speech recognition app with uni app is simple and fast.
基于微信小程序的订餐系统
The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
那些考研后才知道的事
[public class preview]: basis and practice of video quality evaluation
What happened to the communication industry in the first half of this year?
What are the private addresses
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
Godson 2nd generation burn PMON and reload system
Primary code audit [no dolls (modification)] assessment
Require, require in PHP_ once、include、include_ Detailed explanation of the efficiency of repeated introduction of once class library