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#.NET中Dns类的常用方法及说明
IP是一种普遍应用于因特网.允许不同主机能够相互找到对方的寻址协议.IP地址由4个十进制的数字号码所组成,而每一个号码的值介于0~255之间,它虽然解决了网络上计算机的识别问题,但是IP地址确不容易记 ...
- 914D Bash and a Tough Math Puzzle
传送门 分析 用线段树维护区间gcd,每次查询找到第一个不是x倍数的点,如果这之后还有gcd不能被x整除的区间则这个区间不合法 代码 #include<iostream> #include ...
- swing重绘按钮为任意形状图案的方法
swing重绘按钮为任意形状图案的方法 摘自https://www.jb51.net/article/131290.htm 转载 更新时间:2017年12月22日 13:43:00 作者:_Th ...
- 关于Java中hashCode方法的实现源码
首先来看一下String中hashCode方法的实现源码. public int hashCode() { int h = hash; if (h == 0 && value.leng ...
- Bootstrap 的 Tooltip 和 Popover
简介 Tooltip 指提示框,Popover 指弹出框. Tooltip 默认 Tooltip 功能是关闭的,使用前要手动开启. $(function () { $('[data-toggle=&q ...
- 解决eclipse的source not found change at.
eclise快捷键F3跳转到类的实现方法,出现如图所示问题:source not found change atttached source.点击下图红圈,Change Attached Sou ...
- 6678 emif norflash加载
终于搞定,纪念一下.CCS6很不好用,还是换回CCS5.5吧!
- undefined reference to `clock_gettime
下面这个错误通常是因为链接选项里漏了-lrt,但有时发现即使加了-lrt仍出现这个问题,使用nm命令一直,会发现-lrt最终指向的文件没有包含任何symbol,这个时候,可以找相应的静态库版本libr ...
- 高效配置Linux代理服务器 Squid介绍
作为一种免费的网络操作系统,Linux越来越受到广大网络爱好者的欢迎,目前Internet上运行的主机有相当一部分采用的就是Linux,而且中国已经把Linux作为政府上网的指定网络操作系统.种种迹象 ...
- MySQL数据库简介
数据库就是数据的集合. 关系数据库是一种特殊的数据库,它将数据组织成表,并表示为表之间的关系. 数据库系统往往是大型项目的核心数据内容,如银行的用户账户信息.腾讯的QQ用户账户信息.股市的各种交易信息 ...