Skip to content

Commit 2188f93

Browse files
committed
feat: add solutions to lc problems: No.2678~2681
* No.2678.Number of Senior Citizens * No.2679.Sum in a Matrix * No.2680.Maximum OR * No.2681.Power of Heroes
1 parent 3b57254 commit 2188f93

31 files changed

+1330
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# [2678. 老人的数目](https://leetcode.cn/problems/number-of-senior-citizens)
2+
3+
[English Version](/solution/2600-2699/2678.Number%20of%20Senior%20Citizens/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的字符串&nbsp;<code>details</code>&nbsp;。<code>details</code>&nbsp;中每个元素都是一位乘客的信息,信息用长度为 <code>15</code>&nbsp;的字符串表示,表示方式如下:</p>
10+
11+
<ul>
12+
<li>前十个字符是乘客的手机号码。</li>
13+
<li>接下来的一个字符是乘客的性别。</li>
14+
<li>接下来两个字符是乘客的年龄。</li>
15+
<li>最后两个字符是乘客的座位号。</li>
16+
</ul>
17+
18+
<p>请你返回乘客中年龄 <strong>严格大于 60 岁</strong>&nbsp;的人数。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>示例 1:</strong></p>
23+
24+
<pre>
25+
<b>输入:</b>details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
26+
<b>输出:</b>2
27+
<b>解释:</b>下标为 0 ,1 和 2 的乘客年龄分别为 75 ,92 和 40 。所以有 2 人年龄大于 60 岁。
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre>
33+
<b>输入:</b>details = ["1313579440F2036","2921522980M5644"]
34+
<b>输出:</b>0
35+
<b>解释:</b>没有乘客的年龄大于 60 岁。
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= details.length &lt;= 100</code></li>
44+
<li><code>details[i].length == 15</code></li>
45+
<li><code>details[i]</code>&nbsp;中的数字只包含&nbsp;<code>'0'</code>&nbsp;到&nbsp;<code>'9'</code>&nbsp;。</li>
46+
<li><code>details[i][10]</code>&nbsp;是 <code>'M'</code>&nbsp;,<code>'F'</code>&nbsp;或者&nbsp;<code>'O'</code>&nbsp;之一。</li>
47+
<li>所有乘客的手机号码和座位号互不相同。</li>
48+
</ul>
49+
50+
## 解法
51+
52+
<!-- 这里可写通用的实现逻辑 -->
53+
54+
**方法一:遍历计数**
55+
56+
我们可以遍历 `details` 中的每个字符串 $x$,并将 $x$ 的第 $12$ 和第 $13$ 个字符(下标为 $11$, $12$)转换为整数,判断是否大于 $60$,如果是则将答案加一。
57+
58+
遍历结束后,返回答案即可。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是 `details` 的长度。
61+
62+
<!-- tabs:start -->
63+
64+
### **Python3**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```python
69+
class Solution:
70+
def countSeniors(self, details: List[str]) -> int:
71+
return sum(int(x[11:13]) > 60 for x in details)
72+
```
73+
74+
### **Java**
75+
76+
<!-- 这里可写当前语言的特殊实现逻辑 -->
77+
78+
```java
79+
class Solution {
80+
public int countSeniors(String[] details) {
81+
int ans = 0;
82+
for (var x : details) {
83+
int age = Integer.parseInt(x.substring(11, 13));
84+
if (age > 60) {
85+
++ans;
86+
}
87+
}
88+
return ans;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
int countSeniors(vector<string>& details) {
99+
int ans = 0;
100+
for (auto& x : details) {
101+
int age = stoi(x.substr(11, 2));
102+
ans += age > 60;
103+
}
104+
return ans;
105+
}
106+
};
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
func countSeniors(details []string) (ans int) {
113+
for _, x := range details {
114+
age, _ := strconv.Atoi(x[11:13])
115+
if age > 60 {
116+
ans++
117+
}
118+
}
119+
return
120+
}
121+
```
122+
123+
### **...**
124+
125+
```
126+
127+
```
128+
129+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# [2678. Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens)
2+
3+
[中文文档](/solution/2600-2699/2678.Number%20of%20Senior%20Citizens/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
8+
9+
<ul>
10+
<li>The first ten characters consist of the phone number of passengers.</li>
11+
<li>The next character denotes the gender of the person.</li>
12+
<li>The following two characters are used to indicate the age of the person.</li>
13+
<li>The last two characters determine the seat allotted to that person.</li>
14+
</ul>
15+
16+
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> details = [&quot;7868190130M7522&quot;,&quot;5303914400F9211&quot;,&quot;9273338290F4010&quot;]
23+
<strong>Output:</strong> 2
24+
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> details = [&quot;1313579440F2036&quot;,&quot;2921522980M5644&quot;]
31+
<strong>Output:</strong> 0
32+
<strong>Explanation:</strong> None of the passengers are older than 60.
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>1 &lt;= details.length &lt;= 100</code></li>
40+
<li><code>details[i].length == 15</code></li>
41+
<li><code>details[i] consists of digits from &#39;0&#39; to &#39;9&#39;.</code></li>
42+
<li><code>details[i][10] is either &#39;M&#39; or &#39;F&#39; or &#39;O&#39;.</code></li>
43+
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
44+
</ul>
45+
46+
## Solutions
47+
48+
<!-- tabs:start -->
49+
50+
### **Python3**
51+
52+
```python
53+
class Solution:
54+
def countSeniors(self, details: List[str]) -> int:
55+
return sum(int(x[11:13]) > 60 for x in details)
56+
```
57+
58+
### **Java**
59+
60+
```java
61+
class Solution {
62+
public int countSeniors(String[] details) {
63+
int ans = 0;
64+
for (var x : details) {
65+
int age = Integer.parseInt(x.substring(11, 13));
66+
if (age > 60) {
67+
++ans;
68+
}
69+
}
70+
return ans;
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
int countSeniors(vector<string>& details) {
81+
int ans = 0;
82+
for (auto& x : details) {
83+
int age = stoi(x.substr(11, 2));
84+
ans += age > 60;
85+
}
86+
return ans;
87+
}
88+
};
89+
```
90+
91+
### **Go**
92+
93+
```go
94+
func countSeniors(details []string) (ans int) {
95+
for _, x := range details {
96+
age, _ := strconv.Atoi(x[11:13])
97+
if age > 60 {
98+
ans++
99+
}
100+
}
101+
return
102+
}
103+
```
104+
105+
### **...**
106+
107+
```
108+
109+
```
110+
111+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int countSeniors(vector<string>& details) {
4+
int ans = 0;
5+
for (auto& x : details) {
6+
int age = stoi(x.substr(11, 2));
7+
ans += age > 60;
8+
}
9+
return ans;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func countSeniors(details []string) (ans int) {
2+
for _, x := range details {
3+
age, _ := strconv.Atoi(x[11:13])
4+
if age > 60 {
5+
ans++
6+
}
7+
}
8+
return
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int countSeniors(String[] details) {
3+
int ans = 0;
4+
for (var x : details) {
5+
int age = Integer.parseInt(x.substring(11, 13));
6+
if (age > 60) {
7+
++ans;
8+
}
9+
}
10+
return ans;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def countSeniors(self, details: List[str]) -> int:
3+
return sum(int(x[11:13]) > 60 for x in details)

0 commit comments

Comments
 (0)