我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetbook/ ` 2.Add Two Numbers [M] Add Two Numbers M 题目 思路 代码 更精巧的代码 题目 You are given two linked lists representing two non-negative numbers. The…
题目: 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 num…
题目大意:使用链表表示的两个整数,计算出其和,以同样的形式返回. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 觉得做OJ最大的问题是有时看不懂题目. 其实是这样的,一个链表代表一个非负整数数,是从右往左看的: (2 -> 4 -> 3) = 342: (5 -> 6 -> 4)=465: 342+465=7 -> 0 -> 8=807: 逆序其实是很大很大的便利!!…
1.题目描述 2.分析 首先将链表翻转,然后做加法. 最后将结果链表翻转. 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* L1 = reverseList( l1 ); ListNode* L2 = reverseList( l2 ); ListNode* head = ); ListNode* result = head; ListNode* p1 = L1; ListNode* p2 = L2; ;…
1.题目描述 2.题目描述 题目思路可以参考合并单链表的思路,定义一个全局 进位标志,如果两个数值相加得到需要进位,则将进位标志置为1 . 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* head = ) ; ListNode* result = head; ListNode* p1 = l1; ListNode* p2 = l2; ; while( p1 != NULL && p2 != NULL…
链接:http://leetcode.com/onlinejudge 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 lin…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 题目地址:https://leetcode.com/problems/add-two-numbers-ii/description/ 题目描述 You are given two non-empty linked lists representing two non-negative intege…
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse orderand each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assu…
题意 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 numb…
https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. class Solution { public: string multiply(stri…
一天一道leetcode系列 (一)题目: 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 ->…
题目: 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 num…
链表相加 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 -…
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…
题目: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 -&g…
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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…
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 ->…
题目: 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 -&…
Description: 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 th…
一道比较基本的LinkedList的题目.题目要求是这样,现在有两个LinkedList,(2  -> 4 -> 3)和(5 -> 6 -> 4),然后从头开始,把每个node的value相加,最终输出的结果应该是:(7 ->0 ->8).这里需要注意的边界条件有两个,一个是两个链表长度不一样的问题,另一个是进位的问题,注意了这两个问题以后,问题就比较好解决了. 具体的代码如下: /** * Definition for singly-linked list. * pu…
首先想到的是走到其中一个链表的尽头,然后把剩余的链表中的值放入写的链表,返回,但是自己写的代码好长. struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { ,stmp; struct ListNode *pl,*ptmp,*pi; pl=NULL; while(l1&&l2) { g=l1->val+l2->val; g+=s; stmp=g/; g%=; struct ListNo…
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. 您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 分析思路:输入两个链表,输出一个链表.则需要新建一个链表,把输入的两…
1.题目描述 2.问题分析 使的 A[i] = i+1 ,最后检查不满足这个条件的i+1 .即为缺失的值. 3.代码 vector<int> findDisappearedNumbers(vector<int>& nums) { ; i < nums.size() ; ++i ){ ] ){ swap( nums[i] , nums[nums[i] - ] ); i--; } } vector<int> result ; ; i < nums.siz…
1.问题描述 2.问题分析 循环拆分数字,然求和判断. 3.代码 int addDigits(int num) { ) return num; int result = num; do{ vector<int> r = splitnum( result ); result = ; for(auto & n : r ){ result += n; } } ); return result ; } vector<int> splitnum( int num ){ vector&…
1.题目描述 2.问题分析 直接按照加法运算规则运算即可,注意进位问题. 3.代码 string addStrings(string num1, string num2) { if( num1.empty() ) return num2; if( num2.empty() ) return num1; string::reverse_iterator it1 = num1.rbegin() ; string::reverse_iterator it2 = num2.rbegin() ; stri…
1.题目描述 2.题目分析 使用string 的逆向指针,做二进制加法,注意进位问题就可以. 3.代码 string addBinary(string a, string b) { string::reverse_iterator it_a = a.rbegin() ; string::reverse_iterator it_b = b.rbegin() ; string s; "; "; ; while( it_a != a.rend() && it_b != b.r…
1.题目描述 2.题目分析 简单题目,只要挨个判断该数是不是满足条件即可. 3.代码 vector<int> selfDividingNumbers(int left, int right) { vector<int> ans; for(int i = left; i <= right; i++) { if( isDividingNumber(i) ) ans.push_back(i); } return ans; } bool isDividingNumber( int n…
题目描述:输入两个非空单链表,链表的每个结点的值是一个1位数整数,两个链表都是一个大整数每一位的逆序排序,求这两个链表代表的整数的和的链表值: 思路: 分别遍历两个链表,转化成相应的整数,求和后把结果每一位转化成单链表即可: class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode ""…
Add Two Numbers: 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 assum…
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return…