LeetCode每日一题(2376. Count Special Integers)

Webpack打包到宝塔上线:前端小白也能搞定的部署保姆教程 本文为前端开发者提供了一份详尽的Webpack项目部署指南。通过宝塔面板的可视化操作,手把手教你如何将Vue3等项目从本地构建、打包,到上传服务器、配置Nginx,最终完成上线部署的全过程,有效解决了新手在项目上线环节的常见难题。 阅读详情

We call a positive integer special if all of its digits are distinct.

Given a positive integer n, return the number of special integers that belong to the interval [1, n].

Example 1:

Input: n = 20
Output: 19

Explanation: All the integers from 1 to 20, except 11, are special. Thus, there are 19 special integers.

Example 2:

Input: n = 5
Output: 5

Explanation: All the integers from 1 to 5 are special.

Example 3:

Input: n = 135
Output: 110

Explanation: There are 110 integers from 1 to 135 that are special.
Some of the integers that are not special are: 22, 114, and 131.

Constraints:

  • 1 <= n <= 2 * 109

最终答案分两部分:

  1. 位数小于 n 的位数的数字, 这部分取值不需要考虑>n 的情况, 因为位数比 n 的位数少, 所以这部分数字一定是< n 的
  2. 位数等于 n 的位数的数字, 这部分要考虑>n 的情况, 从左向右按位遍历, 当前一位的数字与对应的 n 的数字相等时,当前位就不能超过对应的 n 的数字, 比如 n = 23, 当我们考虑第一位为 1 的时候, 第二位可以取除 1 外的任何数, 当第一位为 2 的时候, 那第二位就只能考虑<=3 的数字, 此时因为 2 已经被第一位占用了, 所以可选的只有 0、1 和 3。

已经使用的数字我们用一个 mask 来保存, 每次取值只能取没有用过的数字。

最后要注意的是 0 不能在第一位




impl Solution {
    fn full(num_of_digits: i32, is_first: bool, mask: i32) -> i32 {
        if num_of_digits == 0 {
            return 0;
        }
        let mut ans = 0;
        if num_of_digits == 1 {
            for i in if is_first { 1 } else { 0 }..10 {
                if (1 << i) & mask == 0 {
                    ans += 1;
                }
            }
            return ans;
        }
        for i in if is_first { 1 } else { 0 }..10 {
            if (1 << i) & mask == 0 {
                ans += Solution::full(num_of_digits - 1, false, mask | (1 << i));
            }
        }
        ans
    }
    fn rc(digits: &Vec<i32>, i: usize, has_reach_top: bool, mask: i32) -> i32 {
        if i == digits.len() {
            return 1;
        }
        let mut ans = 0;
        if has_reach_top {
            for j in 0..10 {
                if j <= digits[i] && (mask & (1 << j as usize)) == 0 {
                    let next_mask = mask | (1 << j as usize);
                    ans += Solution::rc(digits, i + 1, digits[i] == j, next_mask);
                }
            }
        } else {
            for j in 0..10 {
                if (mask & (1 << j as usize)) == 0 {
                    let next_mask = mask | (1 << j as usize);
                    ans += Solution::rc(digits, i + 1, false, next_mask);
                }
            }
        }
        ans
    }
    pub fn count_special_numbers(mut n: i32) -> i32 {
        let mut digits = Vec::new();
        while n > 0 {
            digits.push(n % 10);
            n /= 10;
        }
        digits.reverse();
        let mut ans = 0;
        for i in 1..digits.len() {
            ans += Solution::full(i as i32, true, 0);
        }
        for i in 1..10 {
            if i <= digits[0] {
                ans += Solution::rc(&digits, 1, i == digits[0], 1 << i as usize);
            }
        }
        ans
    }
}

微信小程序协同工作和发布 阅读详情

相关推荐

SAP WM模块配置详解

1.给工厂/存储地点分配仓库编码 功能说明 配置路径 IMG>企业结构>分配>分配后勤执行>给工厂/存储地点分配仓库编码 业务示例 配置步骤 2.定义仓库号码控制参数 功能说明 配置路径 IMG>后勤执行>仓库管理>主数据>主数据>定义仓库号码控制参数 业务示例 配置步骤 3.定义编码范围 功能说明 配置路径 IMG>后勤执行>仓库...

weixin_40672823的博客 2万+

leetcode 2376. Count Special Integers(python)

这道题考察的是数位 DP ,可惜我不会做,大家直接看灵神的代码吧,我解释无非就是重复一次他的话 https://leetcode.cn/problems/count-special-integers/solution/shu-wei-dp-mo-ban-by-endlesscheng-xtgx/。根据题意,如果一个整数的所有数字都是不同的,我们称它为特殊的正整数。给定一个正整数 n ,返回属于区间 [1, n] 的特殊整数的个数。您的支持是我最大的动力。

王大呀呀的博客 716

基于XGBoost的混凝土力学性能预测系统

本文介绍了一个基于XGBoost算法的混凝土力学性能预测系统。该系统通过模拟生成3000组混凝土配合比数据(包含水灰比、水泥用量和龄期三个关键特征),利用XGBoost强大的非线性建模能力预测混凝土抗压强度。研究背景源于传统混凝土性能测试方法耗时耗力的痛点,该系统可显著减少实验次数,优化配合比设计。系统实现包括:数据生成与特征工程(模拟真实工程数据并添加噪声和异常值)、XGBoost模型构建、以及预测性能评估。XGBoost算法因其处理非线性关系能力强、鲁棒性好等优势被选用,可为智能建造和数字化施工提供技术

huanghm88的专栏 95

经典算法:不大于N的特殊数字

经典算法:不大于N的特殊数字

codename_cys的博客 439

力扣 2376. 统计特殊整数

DFS + 记忆化搜索

csdn_muxin的博客 367

LeetCode每日一题(Clone Graph)

Given a reference of a node in a connected undirected graph. Return a deep copy (clone) of the graph. Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors. class Node { public int val; public List neighbors; } Test case fo

wangjun861205的博客 7646

LeetCode每日一题(410. Split Array Largest Sum)

Given an array nums which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Example 1: Input: nums = [7,2,5,10,8], m =

wangjun861205的博客 4965

LeetCode每日一题(1734. Decode XORed Permutation)

There is an integer array perm that is a permutation of the first n positive integers, where n is always odd. It was encoded into another integer array encoded of length n - 1, such that encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2],

wangjun861205的博客 4674

LeetCode每日一题(Majority Element)

Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Example 1: Input: nums = [3,2,3] Output: 3 Example

wangjun861205的博客 4625

LeetCode每日一题(1000. Minimum Cost to Merge Stones)

j]合并为 k 个堆, 从 i…=m], 右侧为 stones[m+1…=j], 左侧合并为 k-1 个堆的成本加上右侧合并为 1 个堆的成本就是整体合并为 k 个堆的成本, dp[i][j][k] = min(dp[i][m][k-1], dp[m+1][j][1])最后一步一定是将 k 个堆合并为 1 个堆: dp[i][j][1] = dp[i][j][k] + sum(i,j)假设 dp[i][j][k]是从 stones[i]到 stones[j]合并为 k 个堆所需要的成本。

wangjun861205的博客 1706

LeetCode每日一题(909. Snakes and Ladders)

这题其实就是个计算最短路径的题目。

wangjun861205的博客 1525

LeetCode每日一题(982. Triples with Bitwise AND Equal To Zero)

因为 nums[i] < 2 ^ 16, 所以 nums[i] & nums[j] < 2 ^ 16, 我们用一个长度为 2 ^ 16 的数组 counts 来保存每个 nums[i] & nums[j]的出现频次,假设 n 为 0 到 2^16 中的每个数字, 如果 nums[i] & n == 0, 则 ans += counts[n]

wangjun861205的博客 1524

LeetCode每日一题(943. Find the Shortest Superstring)

看得官方的解法, 不想多说了, 有兴趣的看。

wangjun861205的博客 1380

LeetCode每日一题(2359. Find Closest Node to Given Two Nodes)

分别计算从 node1 到每个 node 的最短距离 dist_set_1 和 node2 到每个 node 的最短距离 dist_set_2, dist_set_1[i]是从 node1 到 nodes[i]的最短距离, dist_set_2[i]是从 node2 到 nodes[i]的最短距离, 取 max(dist_set_1[i], dist_set_2[i])的最小值, 0

wangjun861205的博客 1142

LeetCode每日一题(2538. Difference Between Maximum and Minimum Price Sum)

假设 to_leaf_sums[i]是 node i 到所有 leaf node 的 sum 的最大值, not_to_leaf_sums[i]是 node i 到所有 leaf node 的 father node 的 sum 的最大值, 那经过 node i 的最大 diff 就是 to_leaf_sums[i] + not_to_leaf_sums[i], 这里要注意, 两个值所对应的 leaf node 不能是同一个 leaf node。not_to_leaf_sums 同理如上。

wangjun861205的博客 1076

LeetCode每日一题(2364. Count Number of Bad Pairs)

bad pair 的, 这样我们可以根据 diff 来对 nums 进行分组, 在求出每一组所能组成的 pairs 的数量, 用 nums 所能组成的所有 pairs 的数量减去每组的 pairs 的数量就是最终答案。以上两条是解这题的核心, 假设 diff[i] = nums[i] - i, 那如果 diff[i] == diff[j]则 nums[i]和 nums[j]一定可以组成。bad pair, 所以任何两个有相同的 diff 的 nums 相互之间一定是可以组成。

wangjun861205的博客 982

LeetCode每日一题(798. Smallest Rotation with Highest Score)

假设 scores[i]为旋转 i 个 nums 后所能得到的 score。旋转的 num 个数从 0 到 nums.len() - 1。我们统计每个 num 对 scores[i]所做的贡献。

wangjun861205的博客 914

LeetCode每日一题(1307. Verbal Arithmetic Puzzle)

从低位到高位做加法, 最终 result 对应的位要等于 sum % 10, 如果不能符合, 直接返回 false。

wangjun861205的博客 904

LeetCode每日一题(1992. Find All Groups of Farmland)

我们可以用一个自增的序列号来为每个单元格进行标号,因为初始状态下,land 中都是 0 和 1,所以我们的序列号从 2 开始, 如果一个为 1 的单元格左侧和上方有相邻的为 1 的单元格,那该单元格的序列号就要与它的左侧或者上方的单元格的序列号相同,因为他们都是同一个 group 的,如果没有相邻为 1 的单元格,那该单元格就需要赋值为当前序列号的值。后来证明这样想是对的。

wangjun861205的博客 900

力扣(合并区间)

给定一组可能重叠或相邻的整数区间,合并所有重叠或端点相接的区间,返回不重叠的最小区间列表。例如输入 [[1,3],[2,6],[8,10],[15,18]],输出 [[1,6],[8,10],[15,18]]。核心是先按左端点升序排序,再单次线性扫描完成合并,避免暴力两两比较。

weixin_52811458的博客 298

LeetCode 155. 最小栈|Python 解法详解

这道题的关键是:使用主栈保存数据,辅助栈在每一层同步保存“截至当前位置的最小值”。理解这一点后,再结合边界条件检查,代码就能保持清晰且稳定。

a1250467048的博客 235

地级市土地出让金数据.xlsx

详细介绍及样例数据:https://blog.csdn.net/li514006030/article/details/151321805

UL 583-2025 电池电动工业卡车 Electric-Battery-Powered Industrial Trucks.rar

UL 583-2025 电池电动工业卡车 Electric-Battery-Powered Industrial Trucks.rar

上一篇: LeetCode每日一题(1835. Find XOR Sum of All Pairs Bitwise AND)
下一篇: LeetCode每日一题(980. Unique Paths III)
wangjun861205
博客等级 码龄12年 11粉丝 · 464原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值