
在实际编程中,我们经常会遇到需要从结构化数据中提取特定模式的需求。本教程关注的是一个具体场景:给定一个数组,其中每个元素要么是一个表示“未完成”的特定标记(例如字符串"i"),要么是一个表示“已完成”的数字。我们的目标是找出数组中最长的连续数字序列的长度。
示例: 假设输入数组为 [4, 15, 9, "I", 30, 2, "I", 20]。
解决此问题的挑战在于:
为了高效地解决这个问题,我们可以采用一种简洁的单次遍历算法。该算法的核心思想是维护两个变量:一个用于跟踪当前连续数字序列的长度,另一个用于记录迄今为止发现的最长连续数字序列的长度。
public class LongestConsecutiveSequence {
/**
* 查找对象数组中最长连续数字序列的长度。
* 数组元素可以是表示数字的Integer对象,或表示中断的字符串"I"。
*
* @param items 包含数字(Integer)或标记("I")的Object数组。
* @return 最长连续数字序列的长度。
*/
public static int findLongestConsecutiveNumberSequenceLength(Object[] items) {
int res = 0; // 存储迄今为止找到的最长连续序列长度
int cur = 0; // 存储当前正在计数的连续序列长度
// 遍历数组中的每一个元素
for (int i = 0; i < items.length; i++) {
// 判断当前元素是否为字符串"I"
// 如果是"I",表示数字序列中断,当前序列长度重置为0
// 如果不是"I"(例如,它是一个Integer对象),则表示当前元素是数字,当前序列长度加1
// 这种比较方式对于Object[]数组中包含String "I"和Integer对象的情况是有效的,
// 因为Integer对象的equals()方法与String "I"比较会返回false。
cur = "I".equals(items[i]) ? 0 : cur + 1;
// 每次更新cur后,都将其与res(最长序列长度)进行比较,
// 确保res始终保持迄今为止的最大值
res = Math.max(res, cur);
}
// 循环结束后,res即为所求的最长连续数字序列长度
return res;
}
public static void main(String[] args) {
// 示例数组1:混合类型
Object[] example1 = {4, 15, 9, "I", 30, 2, "I", 20};
System.out.println("Example 1: " + findLongestConsecutiveNumberSequenceLength(example1)); // Output: 3
// 示例数组2:全数字
Object[] example2 = {1, 2, 3, 4, 5};
System.out.println("Example 2: " + findLongestConsecutiveNumberSequenceLength(example2)); // Output: 5
// 示例数组3:全"I"
Object[] example3 = {"I", "I", "I"};
System.out.println("Example 3: " + findLongestConsecutiveNumberSequenceLength(example3)); // Output: 0
// 示例数组4:空数组
Object[] example4 = {};
System.out.println("Example 4: " + findLongestConsecutiveNumberSequenceLength(example4)); // Output: 0
// 示例数组5:开头和结尾是"I"
Object[] example5 = {"I", 10, 20, "I", 30, 40, 50, "I"};
System.out.println("Example 5: " + findLongestConsecutiveNumberSequenceLength(example5)); // Output: 3
}
}变量初始化:
遍历数组:
立即学习“Java免费学习笔记(深入)”;
核心逻辑判断:
更新最长序列:
返回结果:
为了更好地理解算法的执行过程,我们以示例数组 Object[] items = {4, 15, 9, "I", 30, 2, "I", 20}; 为例,逐步跟踪 cur 和 res 的值变化。
| 步骤 (i) | items[i] | items[i].equals("I") | cur (更新后) | res (更新后) | 说明 |
|---|---|---|---|---|---|
| 初始值 | 0 | 0 | |||
| 0 | 4 | false | 0 + 1 = 1 | max(0, 1) = 1 | 遇到数字,cur 增加 |
| 1 | 15 | false | 1 + 1 = 2 | max(1, 2) = 2 | 遇到数字,cur 增加 |
| 2 | 9 | false | 2 + 1 = 3 | max(2, 3) = 3 | 遇到数字,cur 增加,此时 res 达到最大 |
| 3 | "I" | true | 0 | max(3, 0) = 3 | 遇到"I",cur 重置为0,res 不变 |
| 4 | 30 | false | 0 + 1 = 1 | max(3, 1) = 3 | 遇到数字,cur 增加 |
| 5 | 2 | false | 1 + 1 = 2 | max(3, 2) = 3 | 遇到数字,cur 增加 |
| 6 | "I" | true | 0 | max(3, 0) = 3 | 遇到"I",cur 重置为0,res 不变 |
| 7 | 20 | false | 0 + 1 = 1 | max(3, 1) = 3 | 遇到数字,cur 增加 |
最终,循环结束,方法返回 res 的值,即 3,这与预期结果一致。
输入类型考量:
// 示例:如果输入是 String[] 且数字为字符串
// public static int findLongestConsecutiveNumberSequenceLength(String[] items) {
// int res = 0;
// int cur = 0;
// for (String item : items) {
// try {
// Integer.parseInt(item); // 尝试解析为数字
// cur++;
// } catch (NumberFormatException e) {
// // 解析失败,或者当前字符串就是"I"
// cur = 0;
// }
// res = Math.max(res, cur);
// }
// return res;
// }时间复杂度:
空间复杂度:
**
以上就是Java中查找对象数组中最长连续数字序列的方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号