{ "data": { "question": { "questionId": "3792", "questionFrontendId": "3451", "categoryTitle": "Database", "boundTopicId": 3073058, "title": "Find Invalid IP Addresses", "titleSlug": "find-invalid-ip-addresses", "content": "

Table: logs

\n\n
\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| log_id      | int     |\n| ip          | varchar |\n| status_code | int     |\n+-------------+---------+\nlog_id is the unique key for this table.\nEach row contains server access log information including IP address and HTTP status code.\n
\n\n

Write a solution to find invalid IP addresses. An IPv4 address is invalid if it meets any of these conditions:

\n\n\n\n

Return the result table ordered by invalid_countip in descending order respectively

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

logs table:

\n\n
\n+--------+---------------+-------------+\n| log_id | ip            | status_code | \n+--------+---------------+-------------+\n| 1      | 192.168.1.1   | 200         | \n| 2      | 256.1.2.3     | 404         | \n| 3      | 192.168.001.1 | 200         | \n| 4      | 192.168.1.1   | 200         | \n| 5      | 192.168.1     | 500         | \n| 6      | 256.1.2.3     | 404         | \n| 7      | 192.168.001.1 | 200         | \n+--------+---------------+-------------+\n
\n\n

Output:

\n\n
\n+---------------+--------------+\n| ip            | invalid_count|\n+---------------+--------------+\n| 256.1.2.3     | 2            |\n| 192.168.001.1 | 2            |\n| 192.168.1     | 1            |\n+---------------+--------------+\n
\n\n

Explanation:

\n\n\n\n

The output table is ordered by invalid_count, ip in descending order respectively.

\n
\n", "translatedTitle": "查找无效的 IP 地址", "translatedContent": "

表:logs

\n\n
\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| log_id      | int     |\n| ip          | varchar |\n| status_code | int     |\n+-------------+---------+\nlog_id 是这张表的唯一主键。\n每一行包含服务器访问日志信息,包括 IP 地址和 HTTP 状态码。\n
\n\n

编写一个解决方案来查找 无效的 IP 地址。一个 IPv4 地址如果满足以下任何条件之一,则无效:

\n\n\n\n

返回结果表分别以 invalid_countip 降序 排序。

\n\n

结果格式如下所示。

\n\n

 

\n\n

示例:

\n\n
\n

输入:

\n\n

logs 表:

\n\n
\n+--------+---------------+-------------+\n| log_id | ip            | status_code | \n+--------+---------------+-------------+\n| 1      | 192.168.1.1   | 200         | \n| 2      | 256.1.2.3     | 404         | \n| 3      | 192.168.001.1 | 200         | \n| 4      | 192.168.1.1   | 200         | \n| 5      | 192.168.1     | 500         | \n| 6      | 256.1.2.3     | 404         | \n| 7      | 192.168.001.1 | 200         | \n+--------+---------------+-------------+\n
\n\n

输出:

\n\n
\n+---------------+--------------+\n| ip            | invalid_count|\n+---------------+--------------+\n| 256.1.2.3     | 2            |\n| 192.168.001.1 | 2            |\n| 192.168.1     | 1            |\n+---------------+--------------+\n
\n\n

解释:

\n\n\n\n

输出表分别以 invalid_countip 降序排序。

\n
\n", "isPaidOnly": false, "difficulty": "Hard", "likes": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef find_invalid_ips(logs: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"114\", \"totalSubmission\": \"181\", \"totalAcceptedRaw\": 114, \"totalSubmissionRaw\": 181, \"acRate\": \"63.0%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"logs\":[\"log_id\",\"ip\",\"status_code\"]},\"rows\":{\"logs\":[[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]]}}", "metaData": "{\"mysql\":[\"CREATE TABLE logs (\\n log_id INT,\\n ip VARCHAR(255),\\n status_code INT\\n)\\n\"],\"mssql\":[\"CREATE TABLE logs (\\n log_id INT,\\n ip VARCHAR(255),\\n status_code INT\\n)\\n\"],\"oraclesql\":[\"CREATE TABLE logs (\\n log_id NUMBER,\\n ip VARCHAR2(255),\\n status_code NUMBER\\n)\\n\"],\"database\":true,\"name\":\"find_invalid_ips\",\"postgresql\":[\"CREATE TABLE logs (\\n log_id INTEGER,\\n ip VARCHAR(255), \\n status_code INTEGER \\n);\"],\"pythondata\":[\"logs = pd.DataFrame(columns=[\\\"log_id\\\", \\\"ip\\\", \\\"status_code\\\"]).astype({\\\"log_id\\\": \\\"Int64\\\", \\\"ip\\\": \\\"string\\\", \\\"status_code\\\": \\\"Int64\\\"})\\n\"],\"database_schema\":{\"logs\":{\"log_id\":\"INT\",\"ip\":\"VARCHAR(255)\",\"status_code\":\"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "CREATE TABLE logs (\n log_id INT,\n ip VARCHAR(255),\n status_code INT\n)\n", "Truncate table logs", "insert into logs (log_id, ip, status_code) values ('1', '192.168.1.1', '200')", "insert into logs (log_id, ip, status_code) values ('2', '256.1.2.3', '404')", "insert into logs (log_id, ip, status_code) values ('3', '192.168.001.1', '200')", "insert into logs (log_id, ip, status_code) values ('4', '192.168.1.1', '200')", "insert into logs (log_id, ip, status_code) values ('5', '192.168.1', '500')", "insert into logs (log_id, ip, status_code) values ('6', '256.1.2.3', '404')", "insert into logs (log_id, ip, status_code) values ('7', '192.168.001.1', '200')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"

Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"

Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"

PostgreSQL 16<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "{\"headers\":{\"logs\":[\"log_id\",\"ip\",\"status_code\"]},\"rows\":{\"logs\":[[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]]}}", "__typename": "QuestionNode" } } }