当前位置:网站首页>Leetcode67. 二进制求和
Leetcode67. 二进制求和
2022-07-29 12:52:00 【Java全栈研发大联盟】
题目传送地址: https://leetcode.cn/problems/add-binary/
运行效率
代码如下:
class Solution {
public static String addBinary(String a, String b) {
StringBuilder res = new StringBuilder();
int left = a.length() - 1;
int right = b.length() - 1;
int carry = 0;// 进位
while ((left >= 0 && right >= 0) || carry == 1) {
int i = 0;
if (left >= 0) {
i = a.charAt(left) - '0';
}
int i1 = 0;
if (right >= 0) {
i1 = b.charAt(right) - '0';
}
int sum = i + i1 + carry;
if (sum >= 2) {
carry = 1;
} else {
carry = 0;
}
res.insert(0, sum % 2);
left--;
right--;
}
if (left >= 0) {
String substring = a.substring(0, left + 1);
res.insert(0, substring);
}
if (right >= 0) {
String substring = b.substring(0, right + 1);
res.insert(0, substring);
}
return res.toString();
}
}
边栏推荐
- sleep()方法和wait()方法的区别?安全
- C language game ------ greedy snake ---- for Xiaobai
- 【MySQL视图】视图的概念、创建、查看、删除和修改
- 【云原生】-Docker容器迁移Oracle到MySQL
- C语言小游戏------贪吃蛇----小白专用
- Framework common annotation explanation
- IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一
- [Numpy] np.where
- MySQL database installation (detailed)
- MLX90640 infrared thermal imaging temperature measuring sensor module development notes (9)
猜你喜欢
![[WeChat applet] WXSS and global, page configuration](/img/ff/fe4c827bc887b8afefa72dc9b230af.jpg)
[WeChat applet] WXSS and global, page configuration
![[Numpy] np.select](/img/d6/5dfa767ad24dab3f7289d861011d00.jpg)
[Numpy] np.select

轻松学Pytorch-Pytorch可视化

MySQL基础篇(三)-- 数据类型

mysql数据库安装(详细)

The interviewer was stunned by the self-growth of 4 mainstream database IDs in one breath

别再问我如何制作甘特图了!

何享健“A拆A”又败一局,美的旗下美智光电终止创业板IPO
![[MySQL view] View concept, create, view, delete and modify](/img/dc/436dbaa0419b76cdab02a57436a782.png)
[MySQL view] View concept, create, view, delete and modify

AutoAlignV2:多模态3D目标检测新SOTA!(ECCV2022)
随机推荐
人脸合成效果媲美StyleGAN,而它是个自编码器
Sentinel 2A data preprocessing and calculation of six common vegetation indices in snap software
MySQL八股文背诵版
SIP system composition format
C# 1秒跑一个数字的展示,主要练习 事件相关内容
JUC阻塞队列-ArrayBlockingQueue
浅谈防勒索病毒方案之主机加固
Legendary version adds npc modification, adds npc method and configuration parameter tutorial
用支持LaTex的Markdown语句编辑一个数学公式
企业代码安全防护分类
IDEA 数据库插件Database Navigator 插件
Bika LIMS - SENAITE using open source LIMS set (users, roles and departments)
【云原生】微服务之Feign的介绍与使用
xxl-job源码解析(技术分享)
BGP联邦综合实验
The interviewer was stunned by the self-growth of 4 mainstream database IDs in one breath
一口气说出4种主流数据库ID自增长,面试官懵了
Container is changed | deploy MySQL cluster in the Rancher
Go-Excelize API源码阅读(七)—— CopySheet(from, to int)
【微信小程序】一文解决button、input、image组件