Lexicographical Numbers

本文介绍了一种高效的算法,用于生成1到n之间的整数的字典序排列。通过巧妙地利用数学特性,该算法避免了直接排序所带来的高时间和空间复杂度问题,特别适用于大规模数据处理。

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

 Lexicographical Numbers

Given an integer n, return 1 - n in lexicographical order.

For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].

Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.

Subscribe to see which companies asked this question.

解析:挺巧妙的题,刚开始使用排序的方法超时。其实按字母排序每次*10是接下来的数,如果超过n,且最后一位不是9,则加1,若果后面几位是9则除以10直到末尾不是9再加1。

代码:

class Solution {
public:
    vector<int> lexicalOrder(int n) {
       vector<int>ans;
       
       int cur=1;
       for (int i=1; i<=n; i++)
       {
           ans.push_back(cur);
           if (cur*10<=n)
           {
               cur*=10;
           }
           else if (((cur+1)<=n)&&(cur%10!=9))
           {
               cur++;
           }
           else
           {
               while(cur%10==9||cur==n)
               {
                   cur/=10;
               }
               cur++;
           }
       }
       return ans;
    }
};



翻译下面的这段话 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、付费专栏及课程。

余额充值