当前位置:网站首页>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;
}
}
边栏推荐
- 如何把大的‘tar‘存档文件分割成特定大小的多个文件
- kafaka 日志收集
- 53. 最大子数组和:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
- [machine learning notes] how to solve over fitting and under fitting
- Matlab paper chart standard format output (dry goods)
- Convolutional Neural Networks简述
- Scientific running robot pancakeswap clip robot latest detailed tutorial
- When there are too many input boxes such as input transmitted at one time in the form, the post data is intercepted
- Zhubo Huangyu: these spot gold investment skills are not really bad
- NFT value and white paper acquisition
猜你喜欢

Win10 - lightweight gadget

Set up a website with a sense of ceremony, and post it to the public 2/2 through the intranet

Intranet penetration tool NetApp

Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications

搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2

Idea remote debugging agent

Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?

Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!

我为什么支持 BAT 拆掉「AI 研究院」

redis6事务和锁机制
随机推荐
jasypt配置文件加密|快速入门|实战
Redis6 data type and operation summary
Log4j utilization correlation
aspx 简单的用户登录
Binder communication process and servicemanager creation process
PHP character capture notes 2020-09-14
How to apply the updated fluent 3.0 to applet development
[js] basic syntax - for loop
Multi person cooperation project to see how many lines of code each person has written
These 18 websites can make your page background cool
如何把大的‘tar‘存档文件分割成特定大小的多个文件
Parsing XML using Dom4j
Set up a website with a sense of ceremony, and post it to the public 2/2 through the intranet
Network security - Novice introduction
TortoiseSVN使用情形、安装与使用
Assembly language - Beginner's introduction
leetcode 10. Regular expression matching regular expression matching (difficult)
Xampp configuring multiple items
Attack and defense world crypto WP
Liste des liens (simple)