当前位置:网站首页>Leetcode topic analysis add binary
Leetcode topic analysis add binary
2022-06-23 05:49:00 【ruochen】
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null && l2 == null) {
return null;
}
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
ListNode p1 = l1;
ListNode p2 = l2;
int carry = 0;
ListNode head = new ListNode(0);
ListNode result = head;
while (carry != 0 || p1 != null || p2 != null) {
int v1 = 0;
if (p1 != null) {
v1 = p1.val;
p1 = p1.next;
}
int v2 = 0;
if (p2 != null) {
v2 = p2.val;
p2 = p2.next;
}
int tmp = v1 + v2 + carry;
carry = tmp / 10;
head.next = new ListNode(tmp % 10);
head = head.next;
}
return result.next;
}边栏推荐
- IP6809三线圈15W无线充电发射端方案ic英集芯
- 【数据库备份】通过定时任务完成MySQL数据库的备份
- Wechat applet: unfashionable love talk
- MySQL面试真题(二十七)——RFM分析法对用户进行分类
- MySQL面试真题(二十九)——案例-找到爱看的电影
- Wechat applet: Puzzle toolbox
- node中操作mongoDB
- Advanced Mathematics (Seventh Edition) Tongji University exercises 1-8 personal solutions
- [OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project
- runc 符号链接挂载与容器逃逸漏洞预警(CVE-2021-30465)
猜你喜欢

The performance of nonstandard sprintf code in different platforms

Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators

MySQL面试真题(三十)——贝壳-房产订单分析

Skill self check | do you know these 6 skills if you want to be a test leader?

MDM数据清洗功能开发说明

MySQL面试真题(二十五)——常见的分组比较场景

visdom画多条动态损失曲线

What benefits have digital collections enabled the real industry to release?

MySQL面试真题(二十四)——行列互换

Raspberry pie assert preliminary exercise
随机推荐
How does win11 enable mobile hotspot? How to enable mobile hotspot in win11
使用链表实现两个多项式相加和相乘
Real MySQL interview questions (XXVI) -- didi 2020 written examination questions
树莓派assert初步使用练习
The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
The digital collection market has just begun
阿里云对象存储oss+picgo+typora实现构建图床
Opportunities and challenges of digital collections from the perspective of technology development team
MySQL面试真题(二十三)——拼多多-球赛分析
JS面试题----防抖函数
【斯坦福计网CS144项目】Lab2: TCPReceiver
Common wireless charging and transmitting IC chips
A bit of knowledge - folding forging and Damascus steel
Skill self check | do you know these 6 skills if you want to be a test leader?
高等数学(第七版)同济大学 习题1-9 个人解答
Genetic engineering of AI art? Use # artbreeder to change any shape of the image
PAT 乙等 1019 C语言
Heimdall database proxy scale out 20 times
Heimdall Database Proxy横向扩展提高20倍
[image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code