mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 07:21:40 +08:00
71 lines
2.2 KiB
HTML
71 lines
2.2 KiB
HTML
<p>Table: <code> logs</code></p>
|
|
|
|
<pre>
|
|
+-------------+---------+
|
|
| Column Name | Type |
|
|
+-------------+---------+
|
|
| log_id | int |
|
|
| ip | varchar |
|
|
| status_code | int |
|
|
+-------------+---------+
|
|
log_id is the unique key for this table.
|
|
Each row contains server access log information including IP address and HTTP status code.
|
|
</pre>
|
|
|
|
<p>Write a solution to find <strong>invalid IP addresses</strong>. An IPv4 address is invalid if it meets any of these conditions:</p>
|
|
|
|
<ul>
|
|
<li>Contains numbers <strong>greater than</strong> <code>255</code> in any octet</li>
|
|
<li>Has <strong>leading zeros</strong> in any octet (like <code>01.02.03.04</code>)</li>
|
|
<li>Has <strong>less or more</strong> than <code>4</code> octets</li>
|
|
</ul>
|
|
|
|
<p>Return <em>the result table </em><em>ordered by</em> <code>invalid_count</code>, <code>ip</code> <em>in <strong>descending</strong> order respectively</em>. </p>
|
|
|
|
<p>The result format is in the following example.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong></p>
|
|
|
|
<p>logs table:</p>
|
|
|
|
<pre class="example-io">
|
|
+--------+---------------+-------------+
|
|
| log_id | ip | status_code |
|
|
+--------+---------------+-------------+
|
|
| 1 | 192.168.1.1 | 200 |
|
|
| 2 | 256.1.2.3 | 404 |
|
|
| 3 | 192.168.001.1 | 200 |
|
|
| 4 | 192.168.1.1 | 200 |
|
|
| 5 | 192.168.1 | 500 |
|
|
| 6 | 256.1.2.3 | 404 |
|
|
| 7 | 192.168.001.1 | 200 |
|
|
+--------+---------------+-------------+
|
|
</pre>
|
|
|
|
<p><strong>Output:</strong></p>
|
|
|
|
<pre class="example-io">
|
|
+---------------+--------------+
|
|
| ip | invalid_count|
|
|
+---------------+--------------+
|
|
| 256.1.2.3 | 2 |
|
|
| 192.168.001.1 | 2 |
|
|
| 192.168.1 | 1 |
|
|
+---------------+--------------+
|
|
</pre>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<ul>
|
|
<li>256.1.2.3 is invalid because 256 > 255</li>
|
|
<li>192.168.001.1 is invalid because of leading zeros</li>
|
|
<li>192.168.1 is invalid because it has only 3 octets</li>
|
|
</ul>
|
|
|
|
<p>The output table is ordered by invalid_count, ip in descending order respectively.</p>
|
|
</div>
|