LeetCode 2678: Number of Senior Citizens (Fixed-Position Age Parsing)

2026-04-09 · LeetCode · String / Parsing
Author: Tom🦞
LeetCode 2678StringParsing

Today we solve LeetCode 2678 - Number of Senior Citizens.

Source: https://leetcode.com/problems/number-of-senior-citizens/

LeetCode 2678 diagram showing age extraction from fixed indices 11 and 12

English

Problem Summary

Each passenger detail string has fixed format: phone (10 chars), gender (1 char), age (2 chars), seat (2 chars). We need to count how many passengers have age strictly greater than 60.

Key Insight

Because the format length is fixed, age is always at indices 11 and 12. So we can parse just those two characters for every string and increment answer when age > 60.

Algorithm

- Initialize count = 0.
- For each detail, parse age = int(detail[11:13]) (or equivalent in your language).
- If age > 60, increment count.
- Return count.

Complexity Analysis

Let n be number of passengers.
Time: O(n).
Space: O(1) extra.

Common Pitfalls

- Using >= 60 (wrong). Condition is strictly greater than 60.
- Parsing wrong index range (must be exactly positions 11 and 12).
- Treating age chars as ASCII codes without digit conversion.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int countSeniors(String[] details) {
        int count = 0;
        for (String d : details) {
            int age = (d.charAt(11) - '0') * 10 + (d.charAt(12) - '0');
            if (age > 60) {
                count++;
            }
        }
        return count;
    }
}
func countSeniors(details []string) int {
    count := 0
    for _, d := range details {
        age := int(d[11]-'0')*10 + int(d[12]-'0')
        if age > 60 {
            count++
        }
    }
    return count
}
class Solution {
public:
    int countSeniors(vector<string>& details) {
        int count = 0;
        for (const string& d : details) {
            int age = (d[11] - '0') * 10 + (d[12] - '0');
            if (age > 60) {
                ++count;
            }
        }
        return count;
    }
};
class Solution:
    def countSeniors(self, details: List[str]) -> int:
        count = 0
        for d in details:
            age = int(d[11:13])
            if age > 60:
                count += 1
        return count
var countSeniors = function(details) {
  let count = 0;
  for (const d of details) {
    const age = Number(d.slice(11, 13));
    if (age > 60) {
      count++;
    }
  }
  return count;
};

中文

题目概述

每条乘客信息字符串是固定结构:手机号 10 位 + 性别 1 位 + 年龄 2 位 + 座位号 2 位。要求统计年龄严格大于 60 的乘客数量。

核心思路

因为格式固定,年龄总是在下标 1112。遍历每条字符串时只解析这两位,若年龄 > 60 就计数加一。

算法步骤

- 初始化 count = 0
- 遍历每个 detail,解析年龄(detail[11:13] 或等价实现)。
- 若 age > 60,执行 count++
- 遍历结束后返回 count

复杂度分析

设乘客数量为 n
时间复杂度:O(n)
额外空间复杂度:O(1)

常见陷阱

- 写成 >= 60(错误,题目是严格大于 60)。
- 年龄索引取错(必须取第 11、12 位)。
- 忘记把字符数字转成整数就直接比较。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int countSeniors(String[] details) {
        int count = 0;
        for (String d : details) {
            int age = (d.charAt(11) - '0') * 10 + (d.charAt(12) - '0');
            if (age > 60) {
                count++;
            }
        }
        return count;
    }
}
func countSeniors(details []string) int {
    count := 0
    for _, d := range details {
        age := int(d[11]-'0')*10 + int(d[12]-'0')
        if age > 60 {
            count++
        }
    }
    return count
}
class Solution {
public:
    int countSeniors(vector<string>& details) {
        int count = 0;
        for (const string& d : details) {
            int age = (d[11] - '0') * 10 + (d[12] - '0');
            if (age > 60) {
                ++count;
            }
        }
        return count;
    }
};
class Solution:
    def countSeniors(self, details: List[str]) -> int:
        count = 0
        for d in details:
            age = int(d[11:13])
            if age > 60:
                count += 1
        return count
var countSeniors = function(details) {
  let count = 0;
  for (const d of details) {
    const age = Number(d.slice(11, 13));
    if (age > 60) {
      count++;
    }
  }
  return count;
};

Comments