惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
IT之家
IT之家
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
H
Help Net Security
V
V2EX
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
博客园 - 叶小钗
J
Java Code Geeks
博客园 - 【当耐特】
月光博客
月光博客
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件

博客园 - Fanny123

LeetCode统计好子数组 LeetCode边界与内部和相等的稳定子数组 三段式数组II 变为活跃状态的最小时间 平衡装运的最大数量 三段式数组 I 相邻字符串之间的最长公共前缀 分割字符串 找出数组中的所有 K 近邻下标 使叶子路径成本相等的最小增量 硬币面值还原 检查元素频次是否为质数 等积子集的划分方案 统计一个数组中好对子的数目 LeetCode 1482. 制作 m 束花所需的最少天数 C# 基础(更新中) 圆形靶内的最大飞镖数量 丑数 验证栈序列 BST的中序后继
LeetCode最大数字范围的整数之和
Fanny123 · 2026-07-05 · via 博客园 - Fanny123

LeetCode第509场周赛Q1最大数字范围的整数之和

date:2026-07-05

题目

给你一个整数数组 nums。

一个整数的 数字范围 定义为其 最大 数字与 最小 数字之间的差。

例如,5724 的数字范围为 7 - 2 = 5。

返回 nums 中所有 数字范围 等于数组中 最大数字范围 的整数之和。

示例 1:

输入: nums = [5724,111,350]

输出: 6074

解释:
最大数字范围为 5。数字范围为 5 的整数是 5724 和 350,因此答案为 5724 + 350 = 6074。

示例 2:

输入: nums = [90,900]

输出: 990

解释:
最大数字范围为 9。两个整数的数字范围都是 9 ,因此答案为 90 + 900 = 990。

提示:

1 <= nums.length <= 100
10 <= nums[i] <= 105©leetcode

题解

利用char比较

直接遍历数组,找到最大数字范围的整数,并把它们加到结果中。
求数字范围:数字转化成string,然后找到里面的最大char和最小char,得到数字范围。

class Solution {
    public int maxDigitRange(int[] nums) {
        int res = 0;
        int maxR = 0;
        for (int v : nums) {
            int range = getRange(v);
            if (range > maxR) {
                maxR = range;
                res = v;
            } else if (range == maxR) {
                res += v;
            }
        }

        return res;
    }

    private int getRange(int v) {
        String s = "" + v;
        char max = '0';
        char min = '9';
        for (char ch : s.toCharArray()) {
            if (ch > max) {
                max = ch;
            }

            if (ch < min) {
                min = ch;
            }
        }

        return max - min;
    }
}©leetcode

直接用除法找数字

   private int getRange(int v) {
        int max = 0;
        int min = 9;
        while (v != 0) {
            int d = v % 10;
            v /= 10;
            if (max < d) {
                max = d;
            }
            if (min > d) {
                min = d;
            }
        }

        return max - min;
    }©leetcode