Add Two Numbers
- 博客分类:
- OJ_leetcode
这题不难 直接上递归就行
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { // Start typing your Java solution below // DO NOT write main() function if (l1 != null && l2 != null){ int val = l1.val + l2.val; ListNode l3; if (val < 10){ l3 = new ListNode(val); l3.next = addTwoNumbers(l1.next, l2.next); } else { l3 = new ListNode(val - 10); l3.next = addTwoNumbers(l1.next, l2.next); ListNode l4 = new ListNode(1); l3.next = addTwoNumbers(l3.next, l4); } return l3; }else{ if (l1 != null){ return l1; } else if (l2 != null) return l2; else return null; } } }
发表评论
-
Insert Interval
2012-11-11 01:33 543各种条件真复杂,不仅是边界条件,而且还要分很多种情况讨论 而且 ... -
Implement strStr()
2012-11-07 15:44 584唉 终于到了要记算法的时候了 KMP。。。还没写完 回去再写。 ... -
Flatten Binary Tree to Linked List && Generate Parentheses && Gray Code
2012-11-07 00:08 1094Flatten太简单了 递归 一遍过 oh yeah = = ... -
First Missing Positive
2012-11-06 22:50 590唉 想了很久都没想出来 后来还是看了网上的答案 >_&l ... -
Edit Distance
2012-11-06 00:27 673动规 就是递推。。。比较难想 然后数组长度设置比字符串长度多一 ... -
Divide Two Integers
2012-11-05 00:12 714自己实现除法 太太太恶心了。。。。 就是用位移代替了乘法,然后 ... -
Distinct Subsequences
2012-11-04 21:44 659动规,从前到后用T的每一个字符i,扫描S的每一个字符j。维护一 ... -
Count and Say
2012-11-04 18:46 693public class Solution { ... -
Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Sear
2012-11-04 17:36 827/** * Definition for binary ... -
Container With Most Water
2012-11-04 00:25 698本来以为是个简单的题目,直接二重循环,结果小测试过了,大测试超 ... -
Construct Binary Tree from Inorder and Postorder Traversal
2012-11-03 23:40 748不知道为什么错了。。。eclipse上明明是正确的啊 leet ... -
Combinations
2012-11-03 22:19 607全排列 按理说很简单,可是用递归写,边界条件就还是难想清楚,s ... -
Combination Sum I && II
2012-11-03 21:41 751还是递归 但是边界条件以及边界上的处理不容易搞清楚(一开始我就 ... -
climbing stairs
2012-11-03 17:18 611一开始觉得是简单的组合数学题,但是写完之后发现,首先组合数不是 ... -
Binary Tree Inorder Traversal
2012-11-02 23:51 615I简单 直接递归就好 addAll函数很好用 /** ... -
Best Time to Buy and Sell Stock I & II
2012-11-02 22:05 1045啊 第一次直接过small和big测试 好爽!虽然主要是以前知 ... -
Balanced Binary Tree
2012-11-01 23:38 611/** * Definition for binary ... -
Anagrams
2012-10-31 00:33 590这题实在是没懂它的意思。。。囧啊 import java. ... -
Add Binary
2012-10-29 00:07 602public String addBinary(Strin ... -
4Sum
2012-10-27 22:49 736本来以为只要在3Sum外面再包一层循环就好了,可是。。。在Ju ...
相关推荐
You are given two non-empty linked lists ... 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. java AC版本
leetcode:Add Two Numbers(java)
手绘算法力扣 2 两数相加(Add Two Numbers)
python python_leetcode面试题解之两数相加AddTwoNumbers
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;...
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. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...
给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...
js代码-2. Add Two Numbers
leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;
leetcode 2 和 c 2021-LeetCode-02_Add_...addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { guard l1 != nil && l2 != nil else { return nil } var resultTail = ListNode() let resultHead = resu
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None #Use listnode to get a decimal number class GetNum: snode = None num = 0 ...
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. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...
2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum ...
Add two numbers without using arithmetic operators Chapter 10. Given an array of integers where all the numbers are appearing twice find the only number which appears once Chapter 11. Given an array ...
挑战:01-addOne 难度:基本Swift的: 编写一个名... 例子: addTwoNumbers(5,10)-> 15 addTwoNumbers(10,-2)-> 8 addTwoNumbers(0,0)-> 0 addTwoNumbers('Hello',5)-> NaN function addTwoNumbers ( num1
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
add two numbers in java
Java Program To Add Two Numbers
节点jsat ... * @deprecated addTwoNumbers() will be removed in v2.0.0; please use add() */ exports . addTwoNumbers ( firstNumber , secondNumber ) { return firstNumber + secondNumber ; }