Algorithms—2.Add Two Numbers

本文介绍了一种使用递归算法解决链表相加问题的方法,通过布尔值传递实现进位处理,详细解释了算法逻辑和实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:递归逐个相加,进位用布尔值传递。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    	return add(l1,l2,true);
    }
    public ListNode add(ListNode l1, ListNode l2,boolean flag) {
    	boolean a=l1==null;
    	boolean b=l2==null;
    	if (a&&b&&flag) {
			return null;
		}
    	int val=(a?0:l1.val)+(b?0:l2.val)+(flag?0:1);
    	int v=val%10;
    	boolean f=(v==val);
    	ListNode answer=new ListNode(v);
    	answer.next=add(a?null:l1.next,b?null:l2.next,f);
    	return answer;
    }
}


耗时:396ms,为数不多的上游


翻译下面的这段话 Hard Autograd for Algebraic Expressions 分数 100 作者 郑友怡 单位 浙江大学 The application of automatic differentiation technology in frameworks such as torch and tensorflow has greatly facilitated people's implementation and training of deep learning algorithms based on backpropagation. Now, we hope to implement an automatic differentiation program for algebraic expressions. Input Format First, input an infix expression composed of the following symbols. Operators Type Examples Notes Bracket ( ) Power ^ Multiplication & Division * / Addition & Subtraction + - Argument separator , optional, only used as argument separators in multivariate functions The above operators are arranged in order of operator precedence from top to bottom. For example, a+b^c*d will be considered the same as a + ( (b ^ c) * d ). Mathematical Functions (bonus) Function Description ln(A) log(A, B) logarithm. ln(A) represents the natural logarithm of A, and log(A, B) represents the logarithm of B based on A. cos(A) sin(A) tan(A) basic trigonometric functions. pow(A, B) exp(A) exponential functions. pow(A, B) represents the B power of A, and exp(A) represents the natural exponential of A. Operands Type Examples Notes Literal constant 2 3 0 -5 Just consider integers consisting of pure numbers and minus signs. Variable ex cosval xy xx Considering the above "mathematical functions" as reserved words, identifiers (strings of lowercase English letters) that are not reserved words are called variables. Output Method For each variable (as defined above) that appears in the expression, describe an arithmetic expression that represents the derivative of the input algebraic expression with respect to that variable, using the operators, mathematical functions, and operands defined in the input form. The output is arranged according to the lexicographical order of the variables. For each line print two strings, which are each variable and the corresponding derivative function. Separate the two strings
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值