当前位置:网站首页>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;
}边栏推荐
- Ansible 使用普通用户管理被控端
- 使用链表实现两个多项式相加和相乘
- Redis cache penetration solution - bloom filter
- 高等数学(第七版)同济大学 习题1-7 个人解答
- AHA C language Chapter 8 game time is up (lesson 29)
- 104. simple chat room 7: use socket to transfer objects
- 英集芯ip6806无线充电方案5W过Qi认证外围精简14颗器件
- 51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭
- PAT 乙等 1017 C语言
- True question of MySQL interview (29) -- case - finding favorite movies
猜你喜欢

True MySQL interview question (21) - Finance - overdue loan

jvm: 方法重载时,具体调用哪个方法,是由传入参数的静态类型来决定的,而不是由参数的实际类型来决定

Win11如何开启移动热点?Win11开启移动热点的方法

Lottery DDD code

雷达图canvas

MySQL面试真题(二十九)——案例-找到爱看的电影

MySQL面试真题(二十一)——金融-贷款逾期

Special research on Intelligent upgrading of heavy trucks in China in 2022

Real MySQL interview question (30) -- shell real estate order analysis

MySQL面试真题(二十二)——表连接后的条件筛选及分组筛选
随机推荐
PAT 乙等 1020.月饼
Wechat applet: wechat can also send flash photos to create wechat applet source code download and customize flash time
【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?
MySQL面试真题(三十)——贝壳-房产订单分析
PAT 乙等 1024 科学记数法 C语言
Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
Wechat applet: future wife query generator
ssm项目搭建
sprintf 格式代码使用不规范在不同平台下的表现
Heimdall database proxy scale out 20 times
Digital collections - new investment opportunities
Use of visdom
Yingjixin ip5566 with type-C port 3A charging and discharging fast charging mobile power supply 5W wireless charging in one SOC
PAT 乙等 1010 C语言
Wechat applet: unfashionable love talk
AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
Wechat applet: elderly blessing short video
软件设计开发笔记2:基于QT设计串口调试工具
Low cost 5W wireless charger scheme fs68001b simple charging chip