LeeCode(No2 - Add Two Numbers)
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
LeeCode(No2 - Add Two Numbers)的更多相关文章
- Java解法-两数相加(Add Two Numbers)
问题 给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. ...
- 链表相加(Add Two Numbers)
描述: 给定两个非空的链表,表示两个非负整数.数字以相反的顺序存储,每个节点包含一个数字.添加两个数字并将其作为链表返回. 您可以假设两个数字不包含任何前导零,除了数字0本身. 输入:(2 - > ...
- LeetCode(Add Two Numbers)
一.题目要求 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- LeetCode 2. 两数相加(Add Two Numbers)
题目描述 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入: ...
- LeetCode(193. Valid Phone Numbers)(sed用法)
193. Valid Phone Numbers Given a text file file.txt that contains list of phone numbers (one per lin ...
- 大数定律(Law of Large Numbers)
大数定律:每次从总体中随机抽取1个样本,这样抽取很多次后,样本的均值会趋近于总体的期望.也可以理解为:从总体中抽取容量为n的样本,样本容量n越大,样本的均值越趋近于总体的期望.当样本容量极大时,样本均 ...
- LeeCode(5. Longest Palindromic Substring)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...
- 遗传编程(GA,genetic programming)算法初探,以及用遗传编程自动生成符合题解的正则表达式的实践
1. 遗传编程简介 0x1:什么是遗传编程算法,和传统机器学习算法有什么区别 传统上,我们接触的机器学习算法,都是被设计为解决某一个某一类问题的确定性算法.对于这些机器学习算法来说,唯一的灵活性体现在 ...
随机推荐
- C++——explicit
explicit构造函数是用来防止隐式转换的.请看下面的代码: class Test1 { public: Test1(int n) { num=n; }//普通构造函数 private: int n ...
- Elasticsearch - glossary
From http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/glossary.html glossary of ...
- 算法Sedgewick第四版-第1章基础-007一用两个栈实现简单的编译器
1. package algorithms.util; import algorithms.ADT.Stack; /****************************************** ...
- Swing窗口Linux下不支持最大化问题
Swing窗口Linux下不支持最大化问题 摘自:https://www.linuxidc.com/Linux/2009-06/20519.htm [日期:2009-06-17] 来源:www.qua ...
- 巧用 git rebase 合并多个 commit。
一.为什么需要合并多个 commit 呢? 有时候,我们开发一个功能. 修修补补 commit 了很多次,过多的 commit 会显得很复杂. 不够直观,不能比较清晰查看那些 commit 是对应 ...
- rf常用关键字
上传文件choose file 用法:choose file 元素定位 文件路径\\文件名 此处注意:复制的路径是/,需全部替换成\\ 清除Clear Element Tex ...
- php+mysql入门
mysql+php+apache可以快速的架构动态网站. 首先,为什么php容易搞mysql,因为与mysql交互成为了php语言的一种特性. 一.mysql入门 mysql是一种开源的关系型数据库. ...
- pthread中如何追踪stack over flow
通常在程序挂掉的时候我们会catch 他们挂掉的signal,然后在signal中打印出当时的一个stack,来方便问题调查, 但是在stack overflow的情况发生时,会没有拿到stack.s ...
- GetResponse() 基础连接已经关闭:服务器关闭了本应保持活动状态的连接
1.原因: (1)KeepAlive默认为true,与internet保持持续连接 ,服务器关闭了连接,使用HttpWebResponse.GetResponse()出错 (2)HttpWebRequ ...
- Join方法把阵列转为字符串
string有一个方法,就是Join,可以把阵列转换为字符串: 下面是Join方法重载,可以指定那个元素开始,连续取几个元素: 可以传入一个IEnumerable<T>参数: