letCode-2
letCode第二题题目如下:
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
事实上,这一题考的就是节点和一点数学知识,分析题意:两个链表,存储两个逆序数,从头结点开始,个位对个位,十位对十位,长度不一定相等,使用链表逆序输出结果。
所以使用一个标志位作为进位运算,代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode * result = new ListNode();
int carry = ;
ListNode* curr = result;
//有进位,任意链表均有值
while(l1!=NULL || l2 != NULL | carry!=){
//这里分开判断
if (l1 != NULL){
carry += l1->val;
l1 = l1->next;
}
if(l2 != NULL){
carry += l2->val;
l2 = l2->next;
}
ListNode* node = new ListNode(carry%);
//向下取整
carry/=;
curr->next = node;
curr = node;
}
return result;
}
};
letCode-2的更多相关文章
- letcode刷题之两数相加
letcode里面刷题,坑还是链表不熟,(1)头结点还是有必要设置,否则返回的时候找不到位置:(2)先设置next到新节点再next到下一个节点:都是基础知识 /* * * You are given ...
- [py]letcode第一题求和
letcode第一题, tm的不好弄. 想了很久想到了一个粗蠢的解决办法. Given an array of integers, return indices of the two numbers ...
- (letcode)String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Letcode] 1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 迭代法与开根号求值(letcode 69)
p { margin-bottom: 0.25cm; line-height: 120% } 一.理论证明 p { margin-bottom: 0.25cm; line-height: 120% } ...
- 【letcode】5-LongestPalindromicSubstring
回文串 回文串(palindromic string)是指这个字符串无论从左读还是从右读,所读的顺序是一样的:简而言之,回文串是左右对称的.一般求解一个字符串的最长回文子串问题. problem:Lo ...
- letcode code]Maximum Subarray
1 题目: Find the contiguous subarray within an array (containing at least one number) which has the la ...
- letCode(771 Jewels and Stones )
问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...
- letcode刷题记录-day03-罗马转整数
题目 罗马转整数 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 ...
- letcode刷题记录-day02-回文数
回文数 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答 ...
随机推荐
- Array类型和方法
var ft = new Array(); var ft = new Array(20); var ft = new Array("1","2","3 ...
- python locust 性能测试:locust 参数化(list) ---循环取数据,数据可重复使用
from locust import TaskSet, task, HttpLocust class UserBehavior(TaskSet): def on_start(self): # 当模拟用 ...
- <转>jmeter(二十)阶梯式加压测试
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
- Python新手入门英文词汇(1-1)
英文词汇总结一.交互式环境与print输出 1.print:打印/输出2.coding:编码3.syntax:语法4.error:错误5.invalid:无效6.identifier:名称/标识符7. ...
- kettle 连接 Oracle 异常
场景重现 新安装的 kettle(pdi-ce-7.0.0.0-25) 连接 Oracle 11G R2 报错如下: 解决办法 到 Oracle 官网 JDBC Downloads 下载对应的 ojd ...
- 深入分析Parquet列式存储格式【转】
Parquet是面向分析型业务的列式存储格式,由Twitter和Cloudera合作开发,2015年5月从Apache的孵化器里毕业成为Apache顶级项目,最新的版本是1.8.0. 列式存储 列式存 ...
- vue--使用vue-cli构建项目
webpack是现在较流行的前端自动化工具,该工具可以帮助开发者打包代码,以减少需要手动的工作,可以提高开发效率. vue中提供了一个脚手架工具vue-cli,这个工具已经将webpack配置好了,使 ...
- backbone点滴
可以查看http://www.css88.com/doc/backbone/ backbone与angular http://www.infoq.com/cn/articles/backbone-vs ...
- guxh的python笔记三:装饰器
1,函数作用域 这种情况可以顺利执行: total = 0 def run(): print(total) 这种情况会报错: total = 0 def run(): print(total) tot ...
- C# 关键字base用法
1.调用基类的方法 public class A { public virtual void Hello() { Console.WiriteLine("Hello"); } } ...