乘风破浪:LeetCode真题_002_Add Two Numbers

一、前言

    这次的题目是关于链表方面的题目,把两个链表对应节点相加,还要保证进位,每个节点都必须是十进制的0~9。因此主要涉及到链表,指针方面的知识,以及活学活用的编程能力。

二、LeetCode真题_002_Add Two Numbers

2.1 问题介绍

2.2 分析与解决

看到这样的问题,我们首先要分析清题意,之后画出一个原理图,然后就便于解决了。可以看到主要是包括了进位的问题,因此我们每一次相加的时候需要考虑到进位,并且得到的结果需要取余数作为该节点的结果,并且取整数作为下一位的进位,这里要非常注意,因为是十以内的两位数相加,因此进位最多为9+9+1=19,取整之后为1,因此没有进位则进位为零,有进位,则进位为1。

  1. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  2. ListNode dummyHead = new ListNode(0);
  3. ListNode p = l1, q = l2, curr = dummyHead;
  4. int carry = 0;
  5. while (p != null || q != null) {
  6. int x = (p != null) ? p.val : 0;
  7. int y = (q != null) ? q.val : 0;
  8. int sum = carry + x + y;
  9. carry = sum / 10;
  10. curr.next = new ListNode(sum % 10);
  11. curr = curr.next;
  12. if (p != null) p = p.next;
  13. if (q != null) q = q.next;
  14. }
  15. if (carry > 0) {
  16. curr.next = new ListNode(carry);
  17. }
  18. return dummyHead.next;
  19. }

  下面看看我们的算法:

  1. public class ListNode {
  2. int val;
  3. ListNode next;
  4.  
  5. ListNode(int val) {
  6. this.val = val;
  7. }
  8. }

   使用上面的数据结构进行的算法:

  1. public class Solution {
  2.  
  3. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  4.  
  5. if (l1 == null) {
  6. return l2;
  7. }
  8.  
  9. if (l2 == null) {
  10. return l1;
  11. }
  12.  
  13. ListNode p1 = l1;
  14. ListNode p2 = l2;
  15. ListNode root = new ListNode(0); // 头结点
  16. ListNode r = root;
  17. root.next = l1;
  18.  
  19. int carry = 0; // 初始进位
  20. int sum;
  21. while (p1 != null && p2 != null) {
  22. sum = p1.val + p2.val + carry;
  23. p1.val = sum % 10; // 本位的结果
  24. carry = sum / 10; // 本次进位
  25.  
  26. r.next = p1;
  27. r = p1; // 指向下一个相加的结点
  28. p1 = p1.next;
  29. p2 = p2.next;
  30.  
  31. }
  32.  
  33. if (p1 == null) {
  34. r.next = p2;
  35. } else {
  36. r.next = p1;
  37. }
  38.  
  39. // 最后一次相加还有进位
  40. if (carry == 1) {
  41. // 开始时r.next是第一个要相加的结点
  42. while (r.next != null) {
  43. sum = r.next.val + carry;
  44. r.next.val = sum % 10;
  45. carry = sum / 10;
  46. r = r.next;
  47. }
  48.  
  49. // 都加完了还有进位,就要创建一个新的结点
  50. if (carry == 1) {
  51. r.next = new ListNode(1);
  52. }
  53. }
  54.  
  55. return root.next;
  56. }
  57. }

三、总结

系统给出的算法是比较不错的,不但简单而且整洁,便于理解,并且考虑到了所有的可能情况,值得我们去学习和模仿。

乘风破浪:LeetCode真题_002_Add Two Numbers的更多相关文章

  1. 乘风破浪:LeetCode真题_041_First Missing Positive

    乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...

  2. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  3. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  4. 乘风破浪:LeetCode真题_038_Count and Say

    乘风破浪:LeetCode真题_038_Count and Say 一.前言     这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势. ...

  5. 乘风破浪:LeetCode真题_037_Sudoku Solver

    乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决     这道题 ...

  6. 乘风破浪:LeetCode真题_036_Valid Sudoku

    乘风破浪:LeetCode真题_036_Valid Sudoku 一.前言 有的时候对于一些基础知识的掌握,对我们是至关重要的,比如ASCII重要字符的表示,比如一些基本类型的长度. 二.Valid ...

  7. 乘风破浪:LeetCode真题_035_Search Insert Position

    乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...

  8. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  9. 乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array

    乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array 一.前言     将传统的问题进行一些稍微的变形,这个时候我们可能无所适从了,因此还是实践出真知, ...

随机推荐

  1. 微服务Kong(四)——添加插件

    在本节中,您将学习到,如何配置使用KONG的插件来管理您的API.KONG的核心原则之一就是通过插件来实现API的扩展.插件可以使您更为简单的扩展和管理您的API. 在以下的步骤中,您将通过配置key ...

  2. [转]Hadoop集群_WordCount运行详解--MapReduce编程模型

    Hadoop集群_WordCount运行详解--MapReduce编程模型 下面这篇文章写得非常好,有利于初学mapreduce的入门 http://www.nosqldb.cn/1369099810 ...

  3. ASP.NET Core 中的依赖注入

    目录 什么是依赖注入 ASP .NET Core 中使用依赖注入 注册 使用 释放 替换为其它的 Ioc 容器 参考 什么是依赖注入 软件设计原则中有一个依赖倒置原则(DIP),为了更好的解耦,讲究要 ...

  4. 更改SQLServer实例默认字符集

    转自http://www.cnblogs.com/fygh/archive/2012/05/15/2501598.html 需求 安装数据库时,将字符集安装成了“SQL_Latin1_General_ ...

  5. [PY3]——合并多个字典或映射(collections模块中的ChainMap 类)

    问题 现在有多个字典或者映射,你想将它们从逻辑上合并为一个单一的映射后执行某些操作, 比如查找值或者检查某些键是否存在. 解决方案 使用 collections 模块中的 ChainMap 类 Cha ...

  6. 根据Time Protocol从NIST Internet Time Servers获取准确时间

    Time Protocol(RFC-868)是一种非常简单的应用层协议:它返回一个32位的二进制数字,这个数字描述了从1900年1月1日0时0分0秒到现在的秒数,服务器在TCP的37号端口监听时间协议 ...

  7. 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 ...

  8. SublimeText3 插件的使用和本身的配置

    --------------------20180109----------------------- Part1:如何设置代码字体变大变小 1.点击菜单栏 Sublime Text 中prefere ...

  9. Spring扩展:Spring的IoC容器(注入对象的方式和编码方式)

    二.Spring的IoC容器 IoC:Inversion of Control(控制反转) DI:Dependency Injection(依赖注入) 三.依赖注入的方式 (1)构造注入 (2)set ...

  10. GIT 基础-基础命令

    环境 centos7 1.安装 #yum install git 2.创建本地仓库 ( 这里用 /www/git) 这里里有个隐藏的文件夹 ```.git``` 为git仓库的配置文件夹, 不可随意修 ...