当前位置:网站首页>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;
}
}
边栏推荐
- How to apply the updated fluent 3.0 to applet development
- Mmseg - Mutli view time series data inspection and visualization
- Address book (linked list implementation)
- The development of speech recognition app with uni app is simple and fast.
- Elfk deployment
- 网络安全-HSRP协议
- Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
- Prefix, infix, suffix expression "recommended collection"
- What are the private addresses
- Usage, installation and use of TortoiseSVN
猜你喜欢
These 18 websites can make your page background cool
When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
锚点导航小demo
基于微信小程序的订餐系统
Usage, installation and use of TortoiseSVN
Ordering system based on wechat applet
Liar report query collection network PHP source code
[machine learning notes] several methods of splitting data into training sets and test sets
华为推送服务内容,阅读笔记
What about data leakage? " Watson k'7 moves to eliminate security threats
随机推荐
Win10 - lightweight gadget
Zhubo Huangyu: these spot gold investment skills are not really bad
Laravel框架运行报错:No application encryption key has been specified
Xampp configuring multiple items
Those things I didn't know until I took the postgraduate entrance examination
ETCD数据库源码分析——rawnode简单封装
Primary code audit [no dolls (modification)] assessment
Jetpack Compose入门到精通
Matlab paper chart standard format output (dry goods)
redis6数据类型及操作总结
Zibll theme external chain redirection go page beautification tutorial
Multi person cooperation project to see how many lines of code each person has written
Intranet penetration tool NetApp
Source code analysis of etcd database -- peer RT of inter cluster network layer client
What about data leakage? " Watson k'7 moves to eliminate security threats
如何把大的‘tar‘存档文件分割成特定大小的多个文件
Jetpack compose introduction to mastery
鏈錶(簡單)
Integer = = the comparison will unpack automatically. This variable cannot be assigned empty
Set up a website with a sense of ceremony, and post it to the public 2/2 through the intranet