|
| 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> </p> |
| 19 | +<p><strong class="example">Example 1:</strong></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"] |
| 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 = ["1313579440F2036","2921522980M5644"] |
| 31 | +<strong>Output:</strong> 0 |
| 32 | +<strong>Explanation:</strong> None of the passengers are older than 60. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p> </p> |
| 36 | +<p><strong>Constraints:</strong></p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li><code>1 <= details.length <= 100</code></li> |
| 40 | + <li><code>details[i].length == 15</code></li> |
| 41 | + <li><code>details[i] consists of digits from '0' to '9'.</code></li> |
| 42 | + <li><code>details[i][10] is either 'M' or 'F' or 'O'.</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 --> |
0 commit comments