LeetCode 2 Add Two Numbers 模拟,读题 难度:0
https://leetcode.com/problems/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.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* res = new ListNode(0);
int tmp = 0,cnt = 0;
ListNode * result = res;
while(l1||l2||cnt){
tmp = cnt;
tmp += (l1?l1->val:0) + (l2?l2->val:0);
cnt = tmp / 10;
tmp %= 10;
if(l1)l1=l1->next;
if(l2)l2=l2->next;
res->val = tmp;
if(l1||l2||cnt){
res->next = new ListNode(0);
res = res->next;
}
}
return result;
}
};
LeetCode 2 Add Two Numbers 模拟,读题 难度:0的更多相关文章
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
随机推荐
- php请求返回GeoJSON格式的数据
<?php /* * Following code will list all the products */ // array for JSON response $response = ar ...
- mysql查询一个小知识点,查询结果是空与查询出错是不一样的
$conn = new mysqli(....); $sql = ""; $query = $conn->query($sql); 这里,如果查询正常,有数据返回,那么$qu ...
- SublimeText个性化快捷键设置
一.光标跳出括号 在编写js函数的时候,输入函数名和括号的时候,要想光标跳出括号还得手动的按left键.离两个手的区域比较远,可自行配置 preferences - keys bindings - u ...
- word 文档 一次性设置多张图片大小
1.打开WORD文档,插入多张图片. 2.在word中按alt+f11组合键,进入VBA模式. 3.在左边的工程资源管理器中找到你的word文档,在其上右键/添加/模块 4.复制以下代码 Sub Ma ...
- 【转】CSS:table-cell详解
table-cell这个家伙在国外的网站中偶有露头,天朝由于IE6.7这两个货泛滥成灾,难有发挥,那么,这个家伙到底能干些什么呢?先让我们来研究下table,那些年曾经使用的table布局为何如此辉煌 ...
- div居中方法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- godep 包管理工具
godep是解决包依赖的管理工具 安装 go get github.com/tools/godep 成功安装后,在GOPATH的bin目录下会有一个godep可执行的二进制文件,后面执行的命令都是用这 ...
- js 对多sheet Excel赋值操作
function ExpExcel(){ var tempStr = ""; var filePath ="" var excelname=ReportFile ...
- 解决iphone5s,iphone6不能使用luajit及luac的问题
做手游有小段时间了,感觉坤哥给这么多的机会.一直都比较忙项目的事,比较没时间去写点东西做点记录.想想还是写点开发问题记录比较好,可以很简短,也可以很有用. 我们项目采用的cocos2d-x的引擎,之前 ...
- Strus2学习:基础(一)
Strus2基础: Sturs2起源以及背景: 在起源很早(2002年左右)的 strus1 和 webWork 基础上进行扩展,并且兼容这两大框架!总之很好用啦,随着学习的深入,应该会有更好的诠释的 ...