386 - Lexicographical Numbers(dfs)

本文介绍了一种使用深度优先搜索(DFS)实现的算法,该算法可以将从1到n的所有整数按照字典序进行排序。通过递归地构建数字,确保了每个可能的数字都被考虑,并且有效地处理了前导零的问题。

题意

给出一个n,将前n个数字按照字典序排序后输出。

思路

dfs。从小到大循环一下当前位置应该放的数字即可。

注意前导0。

代码

class Solution {
private:
    vector<int> a;
    int n;
public:
    void dfs(int x, int step) {
        if (x > n) return;
        if (x) a.push_back(x);
        for (int i = (step ? 0 : 1); i < 10; i++) {
            dfs(x * 10 + i, step + 1);
        }
    }

    vector<int> lexicalOrder(int n) {
        this->n = n;
        dfs(0, 0);
        return a;
    }
};
翻译下面的这段话 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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值