[LeetCode] 2408. Design SQL
You are given n
tables represented with two arrays names
and columns
, where names[i]
is the name of the ith
table and columns[i]
is the number of columns of the ith
table.
You should be able to perform the following operations:
- Insert a row in a specific table. Each row you insert has an id. The id is assigned using an auto-increment method where the id of the first inserted row is 1, and the id of each other row inserted into the same table is the id of the last inserted row (even if it was deleted) plus one.
- Delete a row from a specific table. Note that deleting a row does not affect the id of the next inserted row.
- Select a specific cell from any table and return its value.
Implement the SQL
class:
SQL(String[] names, int[] columns)
Creates then
tables.void insertRow(String name, String[] row)
Adds a row to the tablename
. It is guaranteed that the table will exist, and the size of the arrayrow
is equal to the number of columns in the table.void deleteRow(String name, int rowId)
Removes the rowrowId
from the tablename
. It is guaranteed that the table and row will exist.String selectCell(String name, int rowId, int columnId)
Returns the value of the cell in the rowrowId
and the columncolumnId
from the tablename
.
Example 1:
Input ["SQL", "insertRow", "selectCell", "insertRow", "deleteRow", "selectCell"] [[["one", "two", "three"], [2, 3, 1]], ["two", ["first", "second", "third"]], ["two", 1, 3], ["two", ["fourth", "fifth", "sixth"]], ["two", 1], ["two", 2, 2]] Output [null, null, "third", null, null, "fifth"] Explanation SQL sql = new SQL(["one", "two", "three"], [2, 3, 1]); // creates three tables. sql.insertRow("two", ["first", "second", "third"]); // adds a row to the table "two". Its id is 1. sql.selectCell("two", 1, 3); // return "third", finds the value of the third column in the row with id 1 of the table "two". sql.insertRow("two", ["fourth", "fifth", "sixth"]); // adds another row to the table "two". Its id is 2. sql.deleteRow("two", 1); // deletes the first row of the table "two". Note that the second row will still have the id 2. sql.selectCell("two", 2, 2); // return "fifth", finds the value of the second column in the row with id 2 of the table "two".
Constraints:
n == names.length == columns.length
1 <= n <= 104
1 <= names[i].length, row[i].length, name.length <= 20
names[i]
,row[i]
, andname
consist of lowercase English letters.1 <= columns[i] <= 100
- All the strings of
names
are distinct. name
exists in the arraynames
.row.length
equals the number of columns in the chosen table.rowId
andcolumnId
will be valid.- At most
250
calls will be made toinsertRow
anddeleteRow
. - At most
104
calls will be made toselectCell
.
设计 SQL。
给定 n 个表,用两个数组 names 和 columns 表示,其中 names[i] 是第 i 个表的名称,columns[i] 是第 i 个表的列数。
您能够执行以下 操作:
在特定的表中 插入 一行。插入的每一行都有一个 id。id 是使用自动递增方法分配的,其中第一个插入行的 id 为 1,插入到同一个表中的其他行的 id 为最后一个插入行的id (即使它已被删除) 加1。
从指定表中 删除 一行。注意,删除一行不会影响下一个插入行的 id。
从任何表中 查询 一个特定的单元格并返回其值。
实现 SQL 类:SQL(String[] names, int[] columns) 创造 n 个表。
void insertRow(String name, String[] row) 向表 name 中添加一行。保证 表存在,并且数组 row 的大小等于表中的列数。
void deleteRow(String name, int rowId) 从表 name 中移除行 rowId 。保证 表和行都 存在。
String selectCell(String name, int rowId, int columnId) 返回表 name 中 rowId 行和 columnId 列中的单元格值。来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/design-sql
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这是一道设计题。我提供一个 hashmap 的做法。这里我创建一个 hashmap 记录 <tableName, List<List<String>>> 记录每张表和表内的数据。注意 delete 操作是将需要删除的那一行清空而不是真的删除。
时间
insert - O(1)
delete - O(1)
select - O(1)
空间O(n)
Java实现
1 class SQL { 2 HashMap<String, List<List<String>>> map; 3 4 public SQL(List<String> names, List<Integer> columns) { 5 map = new HashMap<>(); 6 for (String name : names) { 7 map.put(name, new ArrayList<>()); 8 } 9 } 10 11 public void insertRow(String name, List<String> row) { 12 map.get(name).add(row); 13 } 14 15 public void deleteRow(String name, int rowId) { 16 map.get(name).get(rowId - 1).clear(); 17 } 18 19 public String selectCell(String name, int rowId, int columnId) { 20 return map.get(name).get(rowId - 1).get(columnId - 1); 21 } 22 } 23 24 /** 25 * Your SQL object will be instantiated and called as such: 26 * SQL obj = new SQL(names, columns); 27 * obj.insertRow(name,row); 28 * obj.deleteRow(name,rowId); 29 * String param_3 = obj.selectCell(name,rowId,columnId); 30 */