简单的两数之和再次乱入<< Add Two Numbers >>
- 请看题目描述:
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
- //Definition for singly-linked list.
- class ListNode
- {
- public int val;
- public ListNode next;
- public ListNode(int x)
- {
- val = x;
- }
- 10 }
- 分析:
这个两数相加不难,只是两个正整数都放在了链表里面了而已。还有一点是,整数的每一位数字都是放过来存放的,那么这样就明显降低了难度了,实现起来就像我们小学的时候列树式计算两个数的和,从低位加起,和与10的余数放在本位,和与10的商产生高位进位。在本题只需从两个链表的表头开始相加,用一个存放计算结果的链表的对应位置存放两个链表对应位置的整数和进位和除以10的余数,并用临时变量存放进位,用于加入下一次的两个整数之和中。直到两个链表中长的链表也遍历完了,那么结果链表也就出来了。结果链表的整数的各位数字也是反序的。- 从前面的分析来看我们可以使用递归和非递归两种算法实现。
C#参考实现如下:
- 递归实现:
- public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
- {
- return AddTwoNumbers(l1, l2, );
- }
- public ListNode AddTwoNumbers(ListNode l1, ListNode l2 , int carry)
- {
- if(l1 == null && l2 == null && carry ==)
- {
- return null;
- }
- ListNode result = new ListNode();
- int sum = (l1 == null ? : l1.val) + (l2 == null ? : l2.val) + carry;
- result.val = sum % ;
- ListNode more = AddTwoNumbers( l1 == null ? null : l1.next ,
- l2 == null ? null :l2.next,
- sum >= ? : );
- result.next = more;
- return result;
- }
- 非递归实现:
- public ListNode AddTwoNumbers2(ListNode l1, ListNode l2)
- {
- ListNode node = new ListNode();
- ListNode result = node;
- int carry = ;
- while(l1 != null || l2 != null || carry != )
- {
- int sum =( l1 ==null ? : l1.val )+( l2 == null ? : l2.val )+ carry;
- l1 =( l1 ==null ? null : l1.next);
- l2 =( l2 == null ? null : l2.next);
- node.next = new ListNode(sum % );
- node = node.next;
- carry = sum / ;
- }
- return result.next;
- }
- 总结:这个题目不难只要静下心来就可以想出来,涉及的细节问题也不多。那么如果把题目改一下,改成两个链表中存放正整数的每一位数字是正向存放的,又该如何实现呢?注意:链表是单向的。这时就要注意两个整数的位数长短了。我们可以这样实现:a)先比较两个链表的长度并用零填充较短的链表 b)递归结果加到首部
简单的两数之和再次乱入<< Add Two Numbers >>的更多相关文章
- leetcode刷题--两数之和(简单)
一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...
- 【Leetcode】【简单】【1. 两数之和】【JavaScript】
题目描述 1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能 ...
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
- LeetCode题解001:两数之和
两数之和 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...
- LeetCode1——两数之和
最近在家拧三阶魔方,把初级的玩法掌握了,也就是可以还原六个面了,速度不快,但是也很兴奋.三阶魔方的初级玩法按照套路拧就可以了,每一步需要完成的任务,该步骤转动的方法基本都是固定的,而且变化也并不是特别 ...
- 653. 两数之和 IV - 输入 BST + HashSet
653. 两数之和 IV - 输入 BST 题目描述 题解分析 最简单的方法就是遍历整棵树,找出所有可能的组合,判断是否存在和为 kk 的一对节点.现在在此基础上做一些改进. 如果存在两个元素之和为 ...
- leetcode算法1.两数之和
哈喽!大家好,我是[学无止境小奇],一位热爱分享各种技术的博主! [学无止境小奇]的创作宗旨:每一条命令都亲自执行过,每一行代码都实际运行过,每一种方法都真实实践过,每一篇文章都良心制作过. [学无止 ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
随机推荐
- 熟练使用git命令
git工作流程图: 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下: Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remo ...
- JSON对象如何转化为字符串?
序列化 定义 指将 JavaScript 值转化为 JSON 字符串的过程. JSON.stringify() 能够将 JavaScript 值转换成 JSON 字符串.JSON.stringify( ...
- VC++编译zlib
目录 第1章简介 1 第2章版本1.2.3 2 2.1 编译汇编代码 2 2.1.1 32位汇编 2 2.1.2 64位汇编 5 2.2 Visual C++ 6.0 ...
- sessionStorage
for(i=0;i<tkt.length;i++){ if(tkt[i].redEnvelopeType==1){ full1+="<div class='pic_01' id= ...
- H5网页播放器播不了服务器上的mp4视频文件
打开IIS,在功能视图里找到MIME类型菜单,打开该菜单后鼠标右键添加.mp4扩展名的MIME类型video/mp4 其他视频文件播放不了估计也得在IIS里添加对应的MIME类型(从服务器下载文件时也 ...
- addEventListener,attachEvent
addEventListener是js填加事件:用法如下: target.addEventListener(type,listener,useCapture) target: 文档节点.documen ...
- JavaWeb基础: Tomcat
Tomcat目录层次结构 Tomcat本质上是一个Java应用,要启动Tomcat服务需要安装JRE并配置JRE的Path,才能通过Tomcat_HOME/bin/startup脚本正常启动Tomca ...
- 父类中“this” 指向问题
“this.字段”如果出现在父类代码中,指的就是父类属性. “this.方法”不管出现在父类还是子类代码中,指的都是子类方法. “this.字段”如果出现在子类代码中,指的就是子类属性. 在程序的时候 ...
- Search for a Range [LeetCode]
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- javascript function new this
1. 首先,我们这里把function直接调用时将这个function当做方法来看待,而new function是将function当做类来看待 2. 当把function作为类来使用时,functi ...