当前位置:网站首页>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;
}边栏推荐
- APP SHA1获取程序 百度地图 高德地图获取SHA1值的简单程序
- 编址和编址单位
- Software design and Development Notes 2: serial port debugging tool based on QT design
- Management system of borrowed books based on SSM framework
- 新课上线 | 每次 5 分钟,轻松玩转阿里云容器服务!
- Wechat applet: elderly blessing short video
- Adnroid activity截屏 保存显示到相册 View显示图片 动画消失
- jvm: 方法重载时,具体调用哪个方法,是由传入参数的静态类型来决定的,而不是由参数的实际类型来决定
- Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
- 啊哈C语言 第7章 有了它你能做更多的事(第27-28讲)
猜你喜欢

android Handler内存泄露 kotlin内存泄露处理

How to move the software downloaded from win11 app store to the desktop

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

高等数学(第七版)同济大学 习题1-8 个人解答

Leetcode 797: all possible paths

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

visdom的使用

编址和编址单位

C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)

MySQL面试真题(二十四)——行列互换
随机推荐
Use of visdom
Activity启动模式和生命周期实测结果
How to move the software downloaded from win11 app store to the desktop
@jsonfield annotation in fastjson
Real MySQL interview question (30) -- shell real estate order analysis
The 510000 prize pool invites you to participate in the competition -- the second Alibaba cloud ECS cloudbuild developer competition is coming
Management system of borrowed books based on SSM framework
Opencv display image
工作积累-判断GPS是否打开
树莓派assert初步使用练习
The digital collection market has just begun
MySQL面试真题(二十八)——案例-通讯运营商指标分析
FS4059A与FS5080E充电芯片的区别
啊哈C语言 第8章 游戏时间到了(第29讲)
知识点滴 - 折叠锻打和大马士革钢
PAT 乙等 1009 C语言
PAT 乙等 1026 程序运行时间
What does the English letter PC mean? What does the Internet PC mean
MySQL面试真题(三十)——贝壳-房产订单分析
英集芯ip6806无线充电方案5W过Qi认证外围精简14颗器件