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:什么是遗传编程算法,和传统机器学习算法有什么区别 传统上,我们接触的机器学习算法,都是被设计为解决某一个某一类问题的确定性算法.对于这些机器学习算法来说,唯一的灵活性体现在 ...
随机推荐
- springmvc urlpattern配置详解
静态资源无法访问问题的解决方案: 1.使用Tomcat默认的Servlet解决:在web.xml中加以下代码
- IDEA创建Maven Web 项目
前提:安装过maven并且配置了maven的环境变量,这里就不演示了.转载了别人一篇maven详解,不了解的可以先看一下这个 链接 图文讲解: 创建项目 选择Maven 选择创建webapp项目 指定 ...
- Using JConsole
Using JConsole 转自 https://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html Th ...
- 【总结整理】关于ArcGIS中拓扑的理解
空间拓扑: https://www.baidu.com/link?url=f8fd1d75GhwtT1JuyPDZydZlWCgEXB9DeuTzDqGQIIRpq0bM-8t3MlC5tXYvEwQ ...
- p4068 [SDOI2016]数字配对
传送门 分析 我们考虑对所有a[i]质因数分解,然后记cnt[i]为a[i]是由几个质数相乘得到的 然后我们建一个二分图,左面为所有cnt[i]为奇数的点,右面是为偶数的点 我们从源点向左面点连容量b ...
- 查看类属性和方法---structure
- Java Iterable类
查看java源代码 /* * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE ...
- sequoiadb的c++应用开发1
使用sequoiadb开发c++应用时需要使用BSON对象,本篇主要讲下BSON的操作方面的东西 1:构建一个_id的BSON对象 BSON的c++驱动给我提供了一个宏BSON,使用该对象可以很方便的 ...
- CodeForces 404D Minesweeper 1D (DP)
题意:给定一个序列,*表示雷,1表示它旁边有一个雷,2表示它旁边有两个雷,0表示旁边没有雷,?表示未知,求有多少情况. 析:dp[i][j] 表示第 i 个放 j 状态,有多少种情况,然后很简单的DP ...
- 解释器模式Interpreter详解
原文链接:https://www.cnblogs.com/java-my-life/archive/2012/06/19/2552617.html 在阎宏博士的<JAVA与模式>一书中开头 ...