LeeCode是一个有意思的编程网站,主要考察程序员的算法

第二题

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

算是一个中度难度的编程题,刚开始考虑转化为整数来加和,但任何类型的整数其实都是有上限的,所以此方法舍弃。

最后自己提交的source如下,想法也是中规中矩吧

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null || l2 == null) {
return null;
}
// 为了不影响传入参数,设定游标
ListNode cursor1 = l1;
ListNode cursor2 = l2; // 结果
ListNode result = new ListNode(0);
ListNode cursorResult = result; // 进位
int carry = 0;
// 每位加和
int sum;
// 暂存游标的值
int cursorVal1 = cursor1.val;
int cursorVal2 = cursor2.val; do{
sum = cursorVal1 + cursorVal2 + carry;
// 是否有进位
if(sum >= 10) {
carry = 1;
} else {
carry = 0;
}
cursorResult.next = new ListNode(sum % 10);
cursorResult = cursorResult.next;
// 如果其中一个游标为空,也就是是说有链表已经遍历完
if(cursor1 != null && cursor1.next != null) {
cursor1 = cursor1.next;
cursorVal1 = cursor1.val;
} else {
cursor1 = null;
cursorVal1 = 0;
}
if(cursor2 != null && cursor2.next != null) {
cursor2 = cursor2.next;
cursorVal2 = cursor2.val;
} else {
cursor2 = null;
cursorVal2 = 0;
} } while (cursor1 != null || cursor2 != null); // 最后一个进位为算进去的话
if(carry == 1) {
cursorResult.next = new ListNode(1);
} // 去掉首个元素
return result.next;
}
}

提交后性能表现结果如下:

虽然击败了93%的人,但是还有7%的人性能优于我的,看了下官方给出的答案,整体思路是一样的,而且它将x,y的声明放在循环内部。这可能也是统计的不精确吧

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}

时间复杂度和空间复杂度如下:

Complexity Analysis

  • Time complexity : O(\max(m, n))O(max(m,n)). Assume that mm and nn represents the length of l1l1 and l2l2 respectively, the algorithm above iterates at most \max(m, n)max(m,n) times.

  • Space complexity : O(\max(m, n))O(max(m,n)). The length of the new list is at most \max(m,n) + 1max(m,n)+1.

参考:

https://leetcode.com/problems/add-two-numbers

最新文章

  1. OVS 中的各种网络设备 - 每天5分钟玩转 OpenStack(128)
  2. 【Telerik】<telerik:RadComboBox>导出列表数据
  3. 数据库多表连接方式介绍-HASH-JOIN
  4. iOS - Plist 数据解析
  5. android UI库
  6. 【C语言】-一维数组
  7. JS类型(1)_JS学习笔记(2016.10.02)
  8. 微型 Python Web 框架 Bottle - Heroin blog
  9. JAVA web四个属性的范围汇总
  10. JavaScript之父谈JavaScript
  11. VxWorks6.6 pcPentium BSP 使用说明(一):基本概念
  12. CSV的简单用法
  13. 【WCF系列】(一)为什么我们需要WCF
  14. vm12 安装ubuntu15.10详细图文教程 虚拟机安装ubuntu安装 ubuntu更新软件 ubuntu一直卡在下载语言怎么办?
  15. redist命令操作(一)--键key,字符串String
  16. python输出格式对齐问题
  17. this 指向问题ES5
  18. 基于贝叶斯优化的超参数tuning
  19. centos 安装glide工具(golang)笔记
  20. IDA error of " positive sp value has been found"

热门文章

  1. siciyuan开源项目观光指南
  2. GUI编程01
  3. Java-马士兵设计模式学习笔记-工厂模式-模拟Spring读取Properties文件
  4. 远程桌面--------ms12-020 漏洞复现 (死亡蓝屏)
  5. Makefile的使用
  6. C++ string操作(转载)
  7. Java50道经典习题-程序41 猴子分桃
  8. Windows下启动redis错误1067:进程意外中止
  9. C#利用phantomJS抓取AjAX动态页面
  10. C# 顺序表---增删改查--逆至--删除最小值