当前位置:网站首页>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;
}
}
边栏推荐
- Jetpack compose introduction to mastery
- Scientific running robot pancakeswap clip robot latest detailed tutorial
- When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
- Address book (linked list implementation)
- ETCD数据库源码分析——集群间网络层客户端peerRt
- 通讯录(链表实现)
- Programmer growth Chapter 8: do a good job of testing
- [public class preview]: basis and practice of video quality evaluation
- Liste des liens (simple)
- ::ffff:192.168.31.101 是一个什么地址?
猜你喜欢
[js] basic syntax - for loop
运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution
Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
What happened to the communication industry in the first half of this year?
Data Lake (VII): Iceberg concept and review what is a data Lake
These 18 websites can make your page background cool
嵌入式软件架构设计-消息交互
The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
内网穿透工具 netapp
随机推荐
Primary code audit [no dolls (modification)] assessment
Elk enterprise log analysis system
【 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)
Idea set method annotation and class annotation
Source code analysis of etcd database -- peer RT of inter cluster network layer client
53. 最大子数组和:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
Assembly language - Beginner's introduction
Aspx simple user login
Network security HSRP protocol
链表(简单)
不知道这4种缓存模式,敢说懂缓存吗?
Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
Jetpack Compose入门到精通
2022建筑焊工(建筑特殊工种)特种作业证考试题库及在线模拟考试
::ffff:192.168.31.101 是一个什么地址?
MySQL get time
53. Maximum subarray sum: give you an integer array num, please find a continuous subarray with the maximum sum (the subarray contains at least one element) and return its maximum sum.
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
How to divide a large 'tar' archive file into multiple files of a specific size
PHP character capture notes 2020-09-14