乘风破浪:LeetCode真题_002_Add Two Numbers
乘风破浪:LeetCode真题_002_Add Two Numbers
一、前言
这次的题目是关于链表方面的题目,把两个链表对应节点相加,还要保证进位,每个节点都必须是十进制的0~9。因此主要涉及到链表,指针方面的知识,以及活学活用的编程能力。
二、LeetCode真题_002_Add Two Numbers
2.1 问题介绍
2.2 分析与解决
看到这样的问题,我们首先要分析清题意,之后画出一个原理图,然后就便于解决了。可以看到主要是包括了进位的问题,因此我们每一次相加的时候需要考虑到进位,并且得到的结果需要取余数作为该节点的结果,并且取整数作为下一位的进位,这里要非常注意,因为是十以内的两位数相加,因此进位最多为9+9+1=19,取整之后为1,因此没有进位则进位为零,有进位,则进位为1。
- 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;
- }
下面看看我们的算法:
- public class ListNode {
- int val;
- ListNode next;
- ListNode(int val) {
- this.val = val;
- }
- }
使用上面的数据结构进行的算法:
- public class Solution {
- public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
- if (l1 == null) {
- return l2;
- }
- if (l2 == null) {
- return l1;
- }
- ListNode p1 = l1;
- ListNode p2 = l2;
- ListNode root = new ListNode(0); // 头结点
- ListNode r = root;
- root.next = l1;
- int carry = 0; // 初始进位
- int sum;
- while (p1 != null && p2 != null) {
- sum = p1.val + p2.val + carry;
- p1.val = sum % 10; // 本位的结果
- carry = sum / 10; // 本次进位
- r.next = p1;
- r = p1; // 指向下一个相加的结点
- p1 = p1.next;
- p2 = p2.next;
- }
- if (p1 == null) {
- r.next = p2;
- } else {
- r.next = p1;
- }
- // 最后一次相加还有进位
- if (carry == 1) {
- // 开始时r.next是第一个要相加的结点
- while (r.next != null) {
- sum = r.next.val + carry;
- r.next.val = sum % 10;
- carry = sum / 10;
- r = r.next;
- }
- // 都加完了还有进位,就要创建一个新的结点
- if (carry == 1) {
- r.next = new ListNode(1);
- }
- }
- return root.next;
- }
- }
三、总结
系统给出的算法是比较不错的,不但简单而且整洁,便于理解,并且考虑到了所有的可能情况,值得我们去学习和模仿。
乘风破浪:LeetCode真题_002_Add Two Numbers的更多相关文章
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_038_Count and Say
乘风破浪:LeetCode真题_038_Count and Say 一.前言 这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势. ...
- 乘风破浪:LeetCode真题_037_Sudoku Solver
乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决 这道题 ...
- 乘风破浪:LeetCode真题_036_Valid Sudoku
乘风破浪:LeetCode真题_036_Valid Sudoku 一.前言 有的时候对于一些基础知识的掌握,对我们是至关重要的,比如ASCII重要字符的表示,比如一些基本类型的长度. 二.Valid ...
- 乘风破浪:LeetCode真题_035_Search Insert Position
乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- 乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array
乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array 一.前言 将传统的问题进行一些稍微的变形,这个时候我们可能无所适从了,因此还是实践出真知, ...
随机推荐
- 微服务Kong(四)——添加插件
在本节中,您将学习到,如何配置使用KONG的插件来管理您的API.KONG的核心原则之一就是通过插件来实现API的扩展.插件可以使您更为简单的扩展和管理您的API. 在以下的步骤中,您将通过配置key ...
- [转]Hadoop集群_WordCount运行详解--MapReduce编程模型
Hadoop集群_WordCount运行详解--MapReduce编程模型 下面这篇文章写得非常好,有利于初学mapreduce的入门 http://www.nosqldb.cn/1369099810 ...
- ASP.NET Core 中的依赖注入
目录 什么是依赖注入 ASP .NET Core 中使用依赖注入 注册 使用 释放 替换为其它的 Ioc 容器 参考 什么是依赖注入 软件设计原则中有一个依赖倒置原则(DIP),为了更好的解耦,讲究要 ...
- 更改SQLServer实例默认字符集
转自http://www.cnblogs.com/fygh/archive/2012/05/15/2501598.html 需求 安装数据库时,将字符集安装成了“SQL_Latin1_General_ ...
- [PY3]——合并多个字典或映射(collections模块中的ChainMap 类)
问题 现在有多个字典或者映射,你想将它们从逻辑上合并为一个单一的映射后执行某些操作, 比如查找值或者检查某些键是否存在. 解决方案 使用 collections 模块中的 ChainMap 类 Cha ...
- 根据Time Protocol从NIST Internet Time Servers获取准确时间
Time Protocol(RFC-868)是一种非常简单的应用层协议:它返回一个32位的二进制数字,这个数字描述了从1900年1月1日0时0分0秒到现在的秒数,服务器在TCP的37号端口监听时间协议 ...
- Cheatsheet: 2018 08.01 ~ 2018 10.31
Other Building the Ultimate Developer PC 3.0 - The Parts List for my new computer, IronHeart Face re ...
- SublimeText3 插件的使用和本身的配置
--------------------20180109----------------------- Part1:如何设置代码字体变大变小 1.点击菜单栏 Sublime Text 中prefere ...
- Spring扩展:Spring的IoC容器(注入对象的方式和编码方式)
二.Spring的IoC容器 IoC:Inversion of Control(控制反转) DI:Dependency Injection(依赖注入) 三.依赖注入的方式 (1)构造注入 (2)set ...
- GIT 基础-基础命令
环境 centos7 1.安装 #yum install git 2.创建本地仓库 ( 这里用 /www/git) 这里里有个隐藏的文件夹 ```.git``` 为git仓库的配置文件夹, 不可随意修 ...