179:第N个数字

LeetCode 400 https://leetcode.cn/problems/nth-digit/description/ 难度:中等 高频面试题汇总:https://www.yuweihung.com/posts/2025/lc-hot/ 已知 x 位数共有 9×10^x−1 个,所有 x 位数的位数之和是 x × 9×10^x−1。使用 d 和 count 分别表示当前遍历到的位数和当前位数下的所有整数的位数之和,初始时 d=1,count=9。每次将 n 减去 d×count,然后将 d 加 1,将 count 乘以 10,直到 n≤d×count,此时的 d 是目标数字所在整数的位数,n 是所有 d 位数中从第一位到目标数字的位数。 为了方便计算目标数字,使用目标数字在所有 d 位数中的下标进行计算,下标从 0 开始计数。令 index=n−1,则 index 即为目标数字在所有 d 位数中的下标,index 的最小可能取值是 0。 时间复杂度:O(log10 n)。用 d 表示第 n 位数字所在整数的位数,循环需要遍历 d 次,由于 d=O(log10 n),因此时间复杂度是 O(log10 n)。 空间复杂度:O(1)。 ...

七月 15, 2025 · Cassius

172:回文数

LeetCode 9 https://leetcode.cn/problems/palindrome-number/description/ 难度:简单 高频面试题汇总:https://www.yuweihung.com/posts/2025/lc-hot/ 反转前一半数字。特判 x<0 的情况,此时 x 一定不是回文数。特判 x > 0 且 x 个位数是 0 的情况,由于 x 的最高位一定不是 0,所以 x 一定不是回文数。 时间复杂度:如果 x≤0 则时间复杂度为 O(1),否则时间复杂度为 O(logx)。 空间复杂度:O(1)。 ...

七月 15, 2025 · Cassius

160:不同的二叉搜索树

LeetCode 96 https://leetcode.cn/problems/unique-binary-search-trees/description/ 难度:中等 高频面试题汇总:https://www.yuweihung.com/posts/2025/lc-hot/ 二叉搜索树的个数为卡特兰数。 \[ C_0 =1 \] \[ C_{n + 1} = \frac{2(2n + 1)}{n + 2}C_n \] ...

七月 15, 2025 · Cassius

快速幂算法

LeetCode 50 实现 pow(x, n) ,即计算 x 的整数 n 次幂函数(即,\(x^n\) )。 ...

二月 7, 2024 · Cassius