088:最大数

LeetCode 179 https://leetcode.cn/problems/largest-number/description/ 难度:中等 要得到最大数,在开头数值不同时,需要把数值大的数排在前面,那么对于有相同数字开头时,应该怎么排序呢? 只需要把两个数字转成字符串并拼接起来,看哪个前后顺序更大。 auto cmp = [](const int &a, const int &b) { return to_string(a) + to_string(b) > to_string(b) + to_string(a); }; 时间复杂度:O(nlognlogm),其中 n 是给定序列的长度,m 是 32 位整数的最大值,每个数转化为字符串后的长度是 O(logm) 的数量级。排序比较函数的时间复杂度为 O(logm),共需要进行 O(nlogn) 次比较。同时我们需要对字符串序列进行拼接,时间复杂度为 O(nlogm),在渐进意义上小于 O(nlognlogm)。 空间复杂度:O(logn),排序需要 O(logn) 的栈空间。 ...

七月 5, 2025 · Cassius

024:最长上升子序列

LeetCode 300 https://leetcode.cn/problems/longest-increasing-subsequence/description/ 难度:中等 本题求最长上升子序列(LIS)有两种做法,分别是动态规划和贪心。 ...

六月 29, 2025 · Cassius

018:买卖股票的最佳时机

LeetCode 121 https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/ 难度:简单 贪心题目,枚举第 i 天之前价格的最小值,作为买入价格。 时间复杂度:O(n),其中 n 为 prices 的长度。 空间复杂度:O(1)。仅用到若干额外变量。 ...

六月 29, 2025 · Cassius