代码拉取完成,页面将自动刷新
同步操作将从 doocs/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
给你一个下标从 0 开始的字符串 details
。details
中每个元素都是一位乘客的信息,信息用长度为 15
的字符串表示,表示方式如下:
请你返回乘客中年龄 严格大于 60 岁 的人数。
示例 1:
输入:details = ["7868190130M7522","5303914400F9211","9273338290F4010"] 输出:2 解释:下标为 0 ,1 和 2 的乘客年龄分别为 75 ,92 和 40 。所以有 2 人年龄大于 60 岁。
示例 2:
输入:details = ["1313579440F2036","2921522980M5644"] 输出:0 解释:没有乘客的年龄大于 60 岁。
提示:
1 <= details.length <= 100
details[i].length == 15
details[i]
中的数字只包含 '0'
到 '9'
。details[i][10]
是 'M'
,'F'
或者 'O'
之一。我们可以遍历 details
中的每个字符串 $x$,并将 $x$ 的第 $12$ 和第 $13$ 个字符(下标为 $11$, $12$)转换为整数,判断是否大于 $60$,如果是则将答案加一。
遍历结束后,返回答案即可。
时间复杂度 $O(n)$,其中 $n$ 是 details
的长度。空间复杂度 $O(1)$。
class Solution:
def countSeniors(self, details: List[str]) -> int:
return sum(int(x[11:13]) > 60 for x in details)
class Solution {
public int countSeniors(String[] details) {
int ans = 0;
for (var x : details) {
int age = Integer.parseInt(x.substring(11, 13));
if (age > 60) {
++ans;
}
}
return ans;
}
}
class Solution {
public:
int countSeniors(vector<string>& details) {
int ans = 0;
for (auto& x : details) {
int age = stoi(x.substr(11, 2));
ans += age > 60;
}
return ans;
}
};
func countSeniors(details []string) (ans int) {
for _, x := range details {
age, _ := strconv.Atoi(x[11:13])
if age > 60 {
ans++
}
}
return
}
function countSeniors(details: string[]): number {
let ans = 0;
for (const x of details) {
const age = parseInt(x.slice(11, 13));
if (age > 60) {
++ans;
}
}
return ans;
}
impl Solution {
pub fn count_seniors(details: Vec<String>) -> i32 {
let mut ans = 0;
for s in details.iter() {
if let Ok(age) = s[11..13].parse::<i32>() {
if age > 60 {
ans += 1;
}
}
}
ans
}
}
function countSeniors(details: string[]): number {
return details.filter(v => parseInt(v.slice(11, 13)) > 60).length;
}
impl Solution {
pub fn count_seniors(details: Vec<String>) -> i32 {
details
.iter()
.filter_map(|s| s[11..13].parse::<i32>().ok())
.filter(|&age| age > 60)
.count() as i32
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。