leadecode 2 Add Two Numbers
package leadcode; /**
* 2. Add Two Numbers
* Medium
* 4067
* 953
*
*
* 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 do not contain any leading zero, except the number 0 itself.
*
* Example:
*
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 0 -> 8
* Explanation: 342 + 465 = 807.
*/ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* } class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { }
}
*/
public class L2 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode cur = head;
int carry = 0;
while (l1!=null || l2!=null){
int x = (l1!=null)?l1.val:0;
int y = (l2!=null)?l2.val:0;
int sum = x+y+carry;
carry = sum/10;
cur.next = new ListNode(sum%10);
cur = cur.next;
if(l1!=null){
l1=l1.next;
}
if(l2!=null){
l2=l2.next;
}
}
if(carry!=0){
cur.next=new ListNode(carry);
}
return head.next;
} public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
} String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
} public static ListNode stringToListNode(String input) {
// Generate array from the input
int[] nodeValues = stringToIntegerArray(input); // Now convert that list into linked list
ListNode dummyRoot = new ListNode(0);
ListNode ptr = dummyRoot;
for(int item : nodeValues) {
ptr.next = new ListNode(item);
ptr = ptr.next;
}
return dummyRoot.next;
} public static String listNodeToString(ListNode node) {
if (node == null) {
return "[]";
} String result = "";
while (node != null) {
result += Integer.toString(node.val) + ", ";
node = node.next;
}
return "[" + result.substring(0, result.length() - 2) + "]";
} public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
ListNode l1 = stringToListNode(line);
line = in.readLine();
ListNode l2 = stringToListNode(line); ListNode ret = new L2().addTwoNumbers(l1, l2); String out = listNodeToString(ret); System.out.print(out);
}
}
} class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
[CareerCup] 2.5 Add Two Numbers 两个数字相加
Suppose the digits are stored in forward order. Repeat the above problem.
EXAMPLE
Input: (6 -> 1 -> 7) + (2 -> 9 -> 5).That is, 617 + 295.
Output: 9 -> 1 -> 2.That is, 912.
leadecode 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 ...
随机推荐
- Cookie、Session详解
讲解的很全面 https://www.cnblogs.com/andy-zhou/p/5360107.html
- Servlet请求转发RequestDispatcher接口
在Servlet中,利用RequestDispatcher对象,可以将请求转发给另外一个Servlet或JSP页面,甚至是HTML页面,来处理对请求的响应. 一,RequestDispatcher接口 ...
- 一款基于jQuery的热点新闻Tab选项卡插件
今天要分享的jQuery焦点图插件非常适合展示热点新闻,之前我们分享过好多基于jQuery的焦点图插件,效果都还很不错.它可以在图片上方展示文字标题,并且焦点图的切换按钮时tab风格的,图片切换也十分 ...
- pentestbox使用教程
pentestbox工具列表:https://tools.pentestbox.org/ freebuf入门实战:http://www.freebuf.com/sectool/125881.html ...
- js跨域问题(2)
前两天碰到一个跨域问题的处理,使用jsonp可以解决.(http://www.cnblogs.com/xtechnet/p/4123210.html) 最近再整理了一下: 非CORS 1.jsonp. ...
- 实现cell显示一个删除button
假设想实现滑动cell时,cell右边就能显示一个删除button,则要实现tableview 下边方法: - (void)tableView:(UITableView *)tableView com ...
- KMP + 求最小循环节 --- HDU 1358 Period
Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度 ...
- 如何远程备份sql server数据库
方法一(不使用SQLDMO): /// ///备份方法 /// SqlConnection conn = new SqlConnection("Server=.;Database=mas ...
- 将数据写入TXT文件中,file_put_contents与fwrite
<?php header("content-type:text/html;charset=utf-8"); $file = './aa.txt'; ###判断是不是文件 if ...
- 说说JSON和JSONP 也许你会豁然开朗
说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决 前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只 ...