2 Add Two Numbers
// Java
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
return add(l1, l2, false);
}
private ListNode add(ListNode l1, ListNode l2, boolean step) {
if (l1 == null && l2 == null && !step)
return null;
int i = l1 != null ? l1.val : 0;
int j = l2 != null ? l2.val : 0;
ListNode result;
if (step) {
result = new ListNode((i + j + 1) % 10);
step = i + j + 1 >= 10;
} else {
result = new ListNode((i + j) % 10);
step = i + j >= 10;
}
result.next = add(l1 == null ? null : l1.next,
l2 == null ? null : l2.next, step);
return result;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
2 Add Two Numbers的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- [LeetCode_2] Add Two Numbers
LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...
- Two Sum & Add Two Numbers
Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...
- No.002 Add Two Numbers
Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...
随机推荐
- Modelsim-altera 仿真 顶层原理图的解决办法
解决办法:首先需要将.bdf原理图文件转换为Verilog HDL等第三方EDA工具所支持的标准描述文件.在Quartus下,保持*.bdf为活动窗口状态,运行[File]/[Create/Updat ...
- 十二、shapes
1. The control points are attributes on the shape which are usually arrays of points. Control points ...
- /etc/passwd&/etc/shadow文件分析
/etc/passwd该目录存储的是操作系统用户信息,该文件为所有用户可见.给linux系统添加一个帐号:useradd -g mysql -d /home/test -m test(:新建一个用户t ...
- redhat7下配置tomcat7,更改端口8080后无法访问页面
搞了一下午,居然是防火墙的事情,redhat7设置方法如下 sudo firewall-cmd --add-port=8081/tcp
- (windows)mongoDB3X+Robomongo的安装与基础配置
一开始安装的时候还觉得很简单真正装了时候发现网上的资料参差不齐再次整理一份安装方法 ---------------------------------------------------------- ...
- border在IE6设置transparent无效
在ie6下给border设置transparent是无效的,解决办法如下: _border-color:tomato; /*For IE6-*/ _filter:chroma(color=tomato ...
- IIS 出现如下错误:PageHandlerFactory-Integrated”
原因: vs2010默认的是4.0框架,4.0的框架是独立的CLR,和2.0的不同,如果想运行4.0的网站,需要用aspnet_regiis注册4.0框架,然后用4.0的Class池,就可以运行4.0 ...
- 答:SQLServer DBA 三十问之一: char、varchar、nvarchar之间的区别(包括用途和空间占用);xml类型查找某个节点的数据有哪些方法,哪个效率高;使用存储 过程和使用T-SQL查询数据有啥不一样;
http://www.cnblogs.com/fygh/archive/2011/10/18/2216166.html 1. char.varchar.nvarchar之间的区别(包括用途和空间占用) ...
- Processing与Java混编初探
Processing其实是由Java开发出的轻量级JAVA开发语言,主要用于做原型,有setup.draw两个主接口和几个消息相应接口 Processing和Java混编很简单...在Java中引入外 ...
- ffmpeg基础
背景知识ffmpeg是一款领先的流媒体处理框架,支持编码,解码,转码等功能并可以在linux, Mac OS X, Microsoft Windows编译运行,用它做播放器的有:ffplay,射手播放 ...