给定两个字符串数组 names 和 columns,大小都为 n。其中 names[i] 是第 i 个表的名称,columns[i] 是第 i 个表的列数。
您需要实现一个支持以下 操作 的类:
实现 SQL 类:
SQL(String[] names, int[] columns)
n 个表。bool ins(String name, String[] row)
row 插入表 name 中并返回 true。row.length 不 匹配列的预期数量,或者 name 不是 一个合法的表,不进行任何插入并返回 false。void rmv(String name, int rowId, int columnId)
name 中移除行 rowId。name 不是 一个合法的表或者没有 id 为 rowId 的行,不进行删除。String sel(String name, int rowId, int columnId)
name 中位于特定的 rowId 和 columnId 的单元格的值。(rowId, columnId) 不合法,返回 "<null>"。String[] exp(String name)
name 中出现的行。name 不是 一个合法的表,返回一个空数组。每一行以字符串表示,每个单元格的值(包括 行的 id)以 "," 分隔。示例 1:
输入:
["SQL","ins","sel","ins","exp","rmv","sel","exp"] [[["one","two","three"],[2,3,1]],["two",["first","second","third"]],["two",1,3],["two",["fourth","fifth","sixth"]],["two"],["two",1],["two",2,2],["two"]]
输出:
[null,true,"third",true,["1,first,second,third","2,fourth,fifth,sixth"],null,"fifth",["2,fourth,fifth,sixth"]]
解释:
// 创建 3 张表。
SQL sql = new SQL(["one", "two", "three"], [2, 3, 1]);
// 将 id 为 1 的行添加到表 "two"。返回 True。
sql.ins("two", ["first", "second", "third"]);
// 从表 "two" 中 id 为 1 的行
// 其中第 3 列返回值 "third"。
sql.sel("two", 1, 3);
// 将另外一个 id 为 2 的行添加到表 "two"。返回 True。
sql.ins("two", ["fourth", "fifth", "sixth"]);
// 导出表 "two" 的行。
// 目前表中有两行 id 为 1 和 2 。
sql.exp("two");
// 删除表 "two" 当中的第一行。注意第二行的 id
// 依然为 2。
sql.rmv("two", 1);
// 从表 "two" 中 id 为 2 的行
// 其中第 2 列返回值 "fifth"。
sql.sel("two", 2, 2);
// 导出表 "two" 的行。
// 目前表中有一行 id 为 2。
sql.exp("two");
示例 2:
输入:["SQL","ins","sel","ins","exp","rmv","sel","exp"] [[["one","two","three"],[2,3,1]],["two",["first","second","third"]],["two",1,3],["two",["fourth","fifth","sixth"]],["two"],["two",1],["two",2,2],["two"]]输出:
[null,true,"third",true,["1,first,second,third","2,fourth,fifth,sixth"],null,"fifth",["2,fourth,fifth,sixth"]]解释:
// 创建 3 张表
SQL sQL = new SQL(["one", "two", "three"], [2, 3, 1]);
// 将 id 为 1 的行添加到表 "two"。返回 True。
sQL.ins("two", ["first", "second", "third"]);
// 从表 "two" 中 id 为 1 的行
// 其中第 3 列返回值 "third"。
sQL.sel("two", 1, 3);
// 删除表 "two" 的第一行。
sQL.rmv("two", 1);
// 返回 "<null>" 因为 id 为 1 的单元格
// 已经从表 "two" 中删除。
sQL.sel("two", 1, 2);
// 返回 False 因为列的数量不正确。
sQL.ins("two", ["fourth", "fifth"]);
// 将 id 为 2 的行添加到表 "two"。返回 True。
sQL.ins("two", ["fourth", "fifth", "sixth"]);
提示:
n == names.length == columns.length1 <= n <= 1041 <= names[i].length, row[i].length, name.length <= 10names[i], row[i], name 由小写英文字母组成。1 <= columns[i] <= 101 <= row.length <= 10names[i] 都是 不同 的。ins 和 rmv 2000 次。sel 104 次。exp 500 次。进阶:如果表因多次删除而变得稀疏,您会选择哪种方法?为什么?考虑对内存使用和性能的影响。